我正在尝试执行朴素贝叶斯模型并通过 classification_report() 显示结果 来自 sklearn
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
def multinomial(X_train, X_test, y_train, y_test):
v = CountVectorizer( ngram_range = (2,3))
#tfidf = TfidfVectorizer(stop_words = {'english'})
model = MultinomialNB()
X_train_set = v.fit_transform(X_train)
X_test_set= v.transform(X_test)
tf_transform = TfidfTransformer(use_idf=True).fit(X_train_set)
X_train_tf = tf_transform.transform(X_train_set)
X_test_tf = tf_transform.transform(X_test_set)
X_train_tf
y_train = y_train.astype('int')
model.fit(X_train_tf, y_train )
prediction = model.predict(X_test_tf)
b_type = ["religion","age","ethnicity","gender","not bullying"]
results = classification_report(y_test, prediction, target_names= b_type)
print('Classification Report for Naive Bayes:\n',results)
multinomial(X_train, X_test, y_train, y_test)
运行上述代码时,出现如下错误结果,
ValueError Traceback (most recent call last)
<ipython-input-51-2d2045f2f956> in <module>
23 print('Classification Report for Naive Bayes:\n',results)
24
---> 25 multinomial(X_train, X_test, y_train)
2 frames
<ipython-input-51-2d2045f2f956> in multinomial(X_train, X_test, y_train)
20 prediction = model.predict(X_test_tf)
21 b_type = ["religion","age","ethnicity","gender","not bullying"]
---> 22 results = classification_report(y_test, prediction)
23 print('Classification Report for Naive Bayes:\n',results)
24
/usr/local/lib/python3.8/dist-packages/sklearn/metrics/_classification.py in classification_report(y_true, y_pred, labels, target_names, sample_weight, digits, output_dict, zero_division)
2108 """
2109
-> 2110 y_type, y_true, y_pred = _check_targets(y_true, y_pred)
2111
2112 if labels is None:
/usr/local/lib/python3.8/dist-packages/sklearn/metrics/_classification.py in _check_targets(y_true, y_pred)
91
92 if len(y_type) > 1:
---> 93 raise ValueError(
94 "Classification metrics can't handle a mix of {0} and {1} targets".format(
95 type_true, type_pred
ValueError: Classification metrics can't handle a mix of unknown and multiclass targets
我不确定这个错误的确切原因是什么