我一直在使用sklearn进行情感分析。 我有一个包含3000个评论的csv文件,并且我正在使用其中60%的评论来训练我的分类器。 当我尝试使用CountVectorizer.transform()提供自定义评论以预测标签时,它会抛出以下错误:
Anaconda\lib\site-packages\sklearn\feature_extraction\text.py", line 864, in transform
raise ValueError("Vocabulary wasn't fitted or is empty!")
ValueError: Vocabulary wasn't fitted or is empty!
请帮助我,这是拟合训练集的代码:
def preprocess():
data,target = load_file()
count_vectorizer = CountVectorizer(binary='true',min_df=1)
data = count_vectorizer.fit_transform(data)
tfidf_data = TfidfTransformer(use_idf=False).fit_transform(data)
return tfidf_data
以下是预测自定义评论情感的代码:
def customQuestionScorer(question, clf):
X_new_tfidf = vectorizer.transform([question]).toarray()
print (clf.predict(X_new_tfidf))
q = "I really like this movie"
customQuestionScorer(q,classifier)
data is your example, or test, data
X = cv.fit_transform([' '.join(row) for row in data]) #y just distributes your data into catagories # y = ["一些随机的东西","一些其他随机的东西"] mn.fit(X,y).predict_proba(cv.transform([' '.join([test])])) - NationWidePants