Classifier.fit for oneclassSVM 抱怨浮点类型。 TypeError 浮点数是必需的
Posted
技术标签:
【中文标题】Classifier.fit for oneclassSVM 抱怨浮点类型。 TypeError 浮点数是必需的【英文标题】:Classifier.fit for oneclassSVM complaining about float Type. TypeError float is required 【发布时间】:2016-09-15 17:33:04 【问题描述】:我正在尝试将两个 One Class SVM 拟合到一小组数据中。这些数据集分别称为 m1 和 m2。 m1 和 m2 是小数列表,它们被转换为浮点 t1 和 t2 类型的 numpy 数组。 当我尝试将 oneclass SVM 拟合到这些数据集时,我看到错误说 fit 函数将只接受浮点数。有人可以帮我解决这个问题吗?
示例值:
m1 =[0.020000000000000018, 0.22799999999999998, 0.15799999999999992, 0.18999999999999995, 0.264]
m2 = [0.1279999999999999, 0.07400000000000007, 0.75, 1.0, 1.0]
代码如下:
classifier1 =sklearn.svm.OneClassSVM(kernel='linear', nu ='0.5',gamma ='auto')
classifier2 = sklearn.svm.OneClassSVM(kernel='linear', nu ='0.5',gamma='auto')
for x in xrange(len(m1)):
print" Iteration "+str(x)
t1.append(float(m1[x]))
t2.append(float(m2[x]))
tx = np.array(t1).astype(float)
ty = np.array(t2).astype(float)
t1 = np.r_[tx+1.0,tx-1.0]
t2 = np.r_[ty+1.0,ty-1.0]
print t1
print t2
clfit1 = classifier1.fit(t1.astype(float))
clfit2 = classifier2.fit(t2.astype(float))
命令行错误:
/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
DeprecationWarning)
Traceback (most recent call last):
File "normalize_data.py", line 108, in <module>
main()
File "normalize_data.py", line 15, in main
trainSVM(result1[0],yval1,result2[0],yval2,0.04)
File "normalize_data.py", line 99, in trainSVM
clfit1 = classifier1.fit(t1.astype(float))
File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/classes.py", line 1029, in fit
**params)
File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py", line 193, in fit
fit(X, y, sample_weight, solver_type, kernel, random_seed=seed)
File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py", line 251, in _dense_fit
max_iter=self.max_iter, random_seed=random_seed)
File "sklearn/svm/libsvm.pyx", line 59, in sklearn.svm.libsvm.fit (sklearn/svm/libsvm.c:1571)
TypeError: a float is required
【问题讨论】:
在t2.append
之后,是tx
以及后面的其余部分是否真的像您所展示的那样缩进了for循环?
【参考方案1】:
出错并将 nu 设置为字符串而不是浮点数。 设置 nu=0.05 可以解决问题。
【讨论】:
以上是关于Classifier.fit for oneclassSVM 抱怨浮点类型。 TypeError 浮点数是必需的的主要内容,如果未能解决你的问题,请参考以下文章
Scikit learn with GraphViz 导出空输出