我是sklearn的新用户,在装入x_train
和y_train
时遇到问题。这是带错误的代码
def naive_bayes(x_train, y_train):
clf = MultinomialNB()
clf.fit(x_train, y_train)
joblib.dump(clf, '%s/NB/naive_bayes.pkl' %model_direc)
if __name__ == "__main__":
train_df = pd.read_json('%s/train.json' %data_direc, orient='index')
y_train = train_df[['*', '**', '***']].astype(np.float64)
x_train = pd.read_json('%s/features.json' %feature_direc, orient='columns')
x_train = x_train.sort_index()
print x_train.shape
print y_train.shape
naive_bayes(x_train, y_train)
这是输出。
(80, 1500)
(80, 3)
Traceback (most recent call last):
File "src/NBtrain.py", line 50, in <module>
naive_bayes(x_train, y_train)
File "src/NBtrain.py", line 37, in naive_bayes
clf.fit(x_train, y_train)
File "/Library/Python/2.7/site-packages/sklearn/naive_bayes.py", line 474, in fit
X, y = check_X_y(X, y, 'csr')
File "/Library/Python/2.7/site-packages/sklearn/utils/validation.py", line 444, in check_X_y
y = column_or_1d(y, warn=True)
File "/Library/Python/2.7/site-packages/sklearn/utils/validation.py", line 480, in column_or_1d
raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape (80, 3)
features.json
是使用tf-idf
创建的。
x_train
的大小为(80,1500)。
y_train
的大小为(80,3)。
我不确定为什么y_train
形状不好?
答案 0 :(得分:1)
MultinomialNB
的目标值应该只有一列。通常,对于分类任务,我们只有一个目标,因此如果您有多个不同的目标,那么您将需要多个分类器。