我下面这个教程,使这个ML预测:使用Python 3.6Python脚本错误“预期的二维数组,而不是1D数组:”?
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]))
Im和我得到错误“预期二维数组,得到了一维数组,而不是:”我 觉得剧本是为旧版本,但我不知道如何将其转换为3.6版本。
已经与尝试:
X.reshape(1, -1)
哪一行产生错误? – stackoverflowuser2010
'X = X.reshape(1,-1)'。重塑不在原位。 –
@ stackoverflowuser2010:我猜猜最后一行'clf.predict()',因为'X'已经是二维的了(无用的''重塑'尽管如此)。 –