简体   繁体   English

如何使用python训练SVM?

[英]How to train SVM using python?

I'm trying to train SVM on dataset with only two column like: 我正在尝试仅使用两列在数据集上训练SVM:

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

I have tried: 我努力了:

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)

for these lines it works correctly, but the problem occurs when I import the array from dataset file, I got an error: 对于这些行,它可以正常工作,但是当我从数据集文件导入数组时出现问题,我得到了一个错误:

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

although the output and the type in the two cases are the same. 尽管两种情况下的输出和类型相同。

imported data from the dataset code is: 从数据集代码导入的数据是:

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])

any suggestion, thanks in advance. 任何建议,谢谢。

Posting the comment as answer to just close the question. 发表评论作为答案以结束问题。

The error is that there is only one type of class (label) in target. 错误是目标中只有一种类型的类(标签)。 See, in the example you posted above (x = np.array([[1],[0],[1],[1]])), there are two categories to classify (0 and 1). 请参阅在您上面发布的示例中(x = np.array([[1],[0],[1],[1]])),有两个类别可以分类(0和1)。

But when you import the dataset from file, target has only type of category for all available samples. 但是,当您从文件导入数据集时,目标对于所有可用样本仅具有类别类型。 Please check the arrays loaded from your file. 请检查从文件加载的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM