我正在遵循这个教程来进行机器学习预测:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn import svm
x = [1, 5, 1.5, 8, 1, 9]
y = [2, 8, 1.8, 8, 0.6, 11]
plt.scatter(x,y)
plt.show()
X = np.array([[1,2],
[5,8],
[1.5,1.8],
[8,8],
[1,0.6],
[9,11]])
y = [0,1,0,1,0,1]
X.reshape(1, -1)
clf = svm.SVC(kernel='linear', C = 1.0)
clf.fit(X,y)
print(clf.predict([0.58,0.76]))
我正在使用Python 3.6,但却遇到了“预期是二维数组,而实际传入的是一维数组”错误。我认为这个脚本是为旧版本编写的,但我不知道该如何将其转换为3.6版本。
已经尝试了:
X.reshape(1, -1)
X = X.reshape(1, -1)
的翻译:X = X.reshape(1, -1)
。reshape
操作不是原地修改。 - Mad Physicistclf.predict(<a-1d-thing>)
,因为X
已经是二维的了(虽然reshape
是无用的)。 - Mark DickinsonX
。 - Mad Physicist