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)