我開始學習機器學習。 於是我開始用樸素貝葉斯
我的Python腳本ValueError in sklearn.naive_bayes.GaussianNB
import numpy as np
x = np.array([[0,0],[1,1],[0,1],[1,0]])
y = np.array([0,0,1,1])
print(x)
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
x = x.reshape(1,-1)
y = y.reshape(1,-1)
clf.fit(x,y)
a = clf.predict([[1,1]])
print(a)
我的錯誤得到的是
[[0 0]
[1 1]
[0 1]
[1 0]]
Traceback (most recent call last):
File "ex.py", line 9, in <module>
clf.fit(x,y)
File "/usr/local/lib/python2.7/dist-packages/sklearn/naive_bayes.py", line 182, in fit
X, y = check_X_y(X, y)
File "/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py", line 526, in check_X_y
y = column_or_1d(y, warn=True)
File "/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py", line 562, in column_or_1d
raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape (1, 4)
我應該怎麼辦?
刪除'x = x.reshape(1,-1)',這是錯誤的原因。 x變成長度爲8的一維數組,其不能適應y(長度4)。 – Nuageux
@Nuageux之後,我也得到那個錯誤 –
我的錯誤,你也應該刪除:'y = y.reshape(1,-1)'。你爲什麼要重塑,有什麼特別的原因? – Nuageux