【问题标题】:How to get classification report from a single input value如何从单个输入值中获取分类报告
【发布时间】: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


【解决方案1】:

对于多项朴素贝叶斯,您可以使用 predict_proba() 函数来获取每个输入的分数。

【讨论】:

    【解决方案2】:

    分类报告的目的是打印精度、召回率和 F1 分数。您无法仅使用一个值来计算这些指标。

    只需使用来自predicted的值

    【讨论】:

    • 不,那不是我想要的结果。如果我的数据集包含有关时尚的内容,并且我输入 Car 作为输入,则预测值包含一个类别。但问题是,该类别是假阴性。不知何故,我需要给定输入的正确性
    • 我不确定。您想预测message4 的类别。您可以使用值为predicted 的代码来执行此操作。您想将预测类别与句子的真实类别进行比较?例如,您必须从您的数据库中获取它或创建一个变量message4_true_class = "T-shirt"。然后就做类似message4_true_class ==predicted
    • 我想从message4中预测类别。但是输入是未知的。用户可以输入任何内容。我的数据集是为时尚而训练的。但是,如果用户输入电视,或时尚未知的其他输入,它将获得一个类别。所以例如电视给出了“内衣”的类别。我的值不在数据库中。所以我可以读到它不可能匹配一个未知值 n,与我的数据集方式相匹配,这会给我一个未知输入的分数?
    【解决方案3】:

    经过反复试验终于找到了答案。

    所以基本上你有一个spam_detect_model.classes_attribute,你可以在其中看到课程。 使用predict_proba,您可以找到概率。所以现在你必须将它们附加在一起,所以你可以使用 Python 中的zip方法来做到这一点。

    所以对于其他在外面苦苦挣扎的人来说,它看起来像这样:

    bow4 = bow_transformer.transform([message4])
    tfidf4 = tfidf_transformer.transform(bow4)
    counter = 0
    predicted = spam_detect_model.predict_proba(tfidf4)
    for x in spam_detect_model.classes_: #classes_ gives you the labels,
      proba  = round(predicted[0][counter],2)
      if proba > 0.01: #only return the labels with a prob of larger then 0,10%
          print(x + ' probility '+ str(proba))
      counter +=1 ```
    

    【讨论】:

      猜你喜欢
      • 2018-05-26
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多