Classifier.fit for oneclassSVM抱怨float Type.TypeError浮点是必需的



我正在尝试将两个One class SVM放入一小组数据中。这些数据集分别被调用m1和m2m1m2是小数列表,这些小数被转换为类型为floatt1的numpy数组和t2。当我试图将oneclass SVM拟合到这些数据集时,我看到错误,说拟合函数只接受浮点值。有人能帮我解决这个问题吗?

示例值:

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

出现错误,并将nu设置为字符串而不是浮点值。设置nu=0.05解决了这个问题。

相关内容

  • 没有找到相关文章

最新更新