如何使用 python 训练 SVM?
How to train SVM using python?
我正在尝试在只有两列的数据集上训练 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])
任何建议,提前致谢。
将评论作为答案发布以关闭问题。
错误是target中只有一种类型的class(标签)。看,在您上面发布的示例中 (x = np.array([[1],[0],[1],[1]])),有两个类别要 classify (0和 1).
但是当您从文件导入数据集时,目标只有所有可用样本的类别类型。请检查从您的文件加载的数组。
我正在尝试在只有两列的数据集上训练 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])
任何建议,提前致谢。
将评论作为答案发布以关闭问题。
错误是target中只有一种类型的class(标签)。看,在您上面发布的示例中 (x = np.array([[1],[0],[1],[1]])),有两个类别要 classify (0和 1).
但是当您从文件导入数据集时,目标只有所有可用样本的类别类型。请检查从您的文件加载的数组。