如何使用python训练SVM?

时间:2017-03-30 06:26:26

标签: python scikit-learn svm

我试图在数据集上训练SVM只有两个列,如:

  • 1 4.5456436
  • 0 2.4353453
  • 1 3.5435636
  • 1 5.4235354
  • 0 1.4235345

我试过了:

x = np.array([[1],[0],[1],[1]])
y = np.array([[4.5456436],[2.4353453],[3.5435636],[5.4235354]])

clf = svm.SVC()
clf.fit(y,x)

对于这些行它可以正常工作,但是当我从数据集文件导入数组时出现问题,我收到一个错误:

ValueError: The number of classes has to be greater than one; got 1

虽然两种情况下的输出和类型是相同的。

从数据集代码导入的数据是:

def read(dir):
    x = []
    y = []
    with open(dir) as f:
        lines = f.readlines()
    for i in range(len(lines)):
        x.append(lines[i][0]);y.append(lines[i][1:])
    x = np.array([[int(i)] for i in x])
    y = np.array([[float(i)] for i in y])

任何建议,提前谢谢。

1 个答案:

答案 0 :(得分:0)

将评论作为答案发布,只关闭问题。

错误是目标中只有一种类(标签)。请参阅上面发布的示例(x = np.array([[1],[0],[1],[1]])),分类有两个类别(0和1)。

但是,当您从文件导入数据集时,目标只有所有可用样本的类别类型。请检查从您的文件加载的阵列。

相关问题