运行sklearn包中自带的朴素贝叶斯进行预测的时候提示了ValueError: dimension mismatch
def NaiveBayes():
"""
朴素贝叶斯进行文本分类
:return: None
"""
news = fetch_20newsgroups(subset='all')
print(news.data)
print(news.target)
# 进行数据分割
x_train, x_test, y_train, y_test = train_test_split(news.data, news.target, test_size=0.25)
# 对数据集进行特征抽取
tf = TfidfVectorizer()
# 以训练集当中的词的列表进行每篇文章重要性统计
x_train = tf.fit_transform(x_train)
print(tf.get_feature_names())
x_test = tf.fit_transform(x_test)
# 进行朴素贝叶斯算法的预测
mlt = MultinomialNB(alpha=1.0)
print(x_train.toarray())
mlt.fit(x_train, y_train)
y_predict = mlt.predict(x_test)
print("预测的文章类别为:", y_predict)
# 得出准确率
print("准确率为:", mlt.score(x_test, y_test))
return None
问题出在:predict的时候,测试集和训练集特征维度不同。
tf = TfidfVectorizer()
x_train = tf.fit_transform(x_train)
x_test = tf.fit_transform(x_test)
tf = TfidfVectorizer()
x_train = tf.fit_transform(x_train)
x_test = tf.transform(x_test)
tf = TfidfVectorizer()
tf.fit_tranform(X_train)
tf.tranform(X_test)