【发布时间】:2020-04-12 20:19:08
【问题描述】:
我使用 NLP 对我的数据进行分类,我已经训练了我的数据,现在我想知道单个输入值的分数。我的数据包含衣服和时尚的东西,它应该返回它所属的类别。我想从单个输入值检查分类分数。 所以我这样做:
bow4 = bow_transformer.transform([message4])
tfidf4 = tfidf_transformer.transform(bow4)
predicted = spam_detect_model.predict(tfidf4)
from sklearn.metrics import classification_report
print (classification_report(data['Category Path'], predicted))
然后我收到以下错误
“发现样本数量不一致的输入变量:”
那是因为预测值的数组大小与数据不匹配。
如何从单个预测值查看分类报告?我想这样做是因为我想创建一些用户可以输入的网络应用程序。例如,如果分类分数低于 x,那么它会给出错误。
谢谢!
我的代码看起来像这样
import pandas as pd
import seaborn as sns
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
import string
from nltk.corpus import stopwords
#open file
data = pd.read_csv('cats.csv',sep=';')
data['length'] = data['Product Name'].str.len()
#remove all puncs
def text_process(mess):
# Check characters to see if they are in punctuation
nopunc = [char for char in mess if char not in string.punctuation]
# Join the characters again to form the string.
nopunc = ''.join(nopunc)
# Now just remove any stopwords
return [word for word in nopunc.split() if word.lower() not in stopwords.words('english') if word.lower() not in stopwords.words('dutch')]
# Might take awhile...
bow_transformer = CountVectorizer(analyzer=text_process).fit(data['Product Name'])
# Print total number of vocab words
print(len(bow_transformer.vocabulary_))
messages_bow = bow_transformer.transform(data['Product Name'])
tfidf_transformer = TfidfTransformer().fit(messages_bow)
messages_tfidf = tfidf_transformer.transform(messages_bow)
from sklearn.naive_bayes import MultinomialNB
spam_detect_model = MultinomialNB().fit(messages_tfidf, data['Category Path'])
message4 = "some dummy data "
bow4 = bow_transformer.transform([message4])
tfidf4 = tfidf_transformer.transform(bow4)
predicted = spam_detect_model.predict(tfidf4)
#errors here
from sklearn.metrics import classification_report
print (classification_report(data['Category Path'], predicted))
【问题讨论】:
-
能否请您包括您的导入和模型实例化。通过这种方式,我们可以进一步了解它以提供更直接的答案。
-
嗨,我稍微修改了一下我的问题,
-
不错!我知道这不是您的问题,因为我仍在阅读代码。但是我注意到您正在加入单词,但没有提供空格字符来加入它们。对吗?
-
是的,没错!
-
您是否尝试过打印 tfidf4 并检查其长度?
标签: python machine-learning scikit-learn nlp