1
使用python lib sklearn,我尝试从训练集中提取特征并用这些数据拟合BernoulliNB分类器。TfIdf矩阵为BernoulliNB返回错误的特征数量
分类器未经训练后,我想要预测(分类)一些新的测试数据。 不幸的是我得到这个错误:
Traceback (most recent call last):
File "sentiment_analysis.py", line 45, in <module> main()
File "sentiment_analysis.py", line 41, in main
prediction = classifier.predict(tfidf_data)
File "\Python27\lib\site-packages\sklearn\naive_bayes.py", line 64, in predict
jll = self._joint_log_likelihood(X)
File "\Python27\lib\site-packages\sklearn\naive_bayes.py", line 724, in _joint_log_likelihood
% (n_features, n_features_X))
ValueError: Expected input with 4773 features, got 13006 instead
这是我的代码:
#Train the Classifier
data,target = load_file('validation/validation_set_5.csv')
tf_idf = preprocess(data)
classifier = BernoulliNB().fit(tf_idf, target)
#Predict test data
count_vectorizer = CountVectorizer(binary='true')
test = count_vectorizer.fit_transform(test)
tfidf_data = TfidfTransformer(use_idf=False).fit_transform(test)
prediction = classifier.predict(tfidf_data)