【发布时间】:2016-01-01 22:02:49
【问题描述】:
使用 python lib sklearn,我尝试从训练集中提取特征,并用这些数据拟合 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)
【问题讨论】:
标签: python scikit-learn tf-idf naivebayes