oneclassSVM的Classifier.fit抱怨float类型。 TypeError float是必需的

时间:2016-05-19 02:33:45

标签: python python-2.7 scikit-learn svm training-data

我正在尝试将两个One Class SVM放到一小组数据中。这些数据集分别为m1和m2。 m1 m2 是转换为float t1 t2 类型的numpy数组的小数列表。 当我尝试将oneclass SVM适合这些数据集时,我看到错误说fit函数只接受float。有人可以帮我解决这个问题吗?

示例值:

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

1 个答案:

答案 0 :(得分:4)

发生错误并将 nu 设置为字符串而不是浮点数。 设置nu = 0.05修复问题。