sklearn MultinomialNB 在 python 中的错误输入形状



我是 sklearn 的新手,在安装x_trainy_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状况不佳?

MultinomialNB的目标值应该只有一列。一般来说,对于分类任务,我们只有一个目标,所以如果你有多个不同的目标,那么你将需要多个分类器。

相关内容

  • 没有找到相关文章