【发布时间】:2020-09-20 11:10:10
【问题描述】:
所以我用 sklearn 朴素贝叶斯分类器构建了一个模型。 我需要知道如何用输入预测一个句子
当我对句子进行硬编码时,它的工作正常,看起来像这样
new_sentence = ['its so broken']
new_testdata_tfidf= tfidf.transform(new_sentence)
#transform it to matrix to see the score TFIDF on the training data
fit_feature_selection = selection.transform(new_testdata_tfidf)
#transform the new data to see if the feature remove or not, because after tfidf i use chi2 selection feature.
predicted = classifier.predict(feature_selection )
#then predict it. the classificaiton out, the class is -1 which is the correct answer
我需要手动输入文本数据作为输入,所以我这样使用
new_sentence = input[('')]
#i input the same sentence its so broken
new_testdata_tfidf= tfidf.transform(new_sentence)
#transform it to matrix to see the score TFIDF on the training data
fit_feature_selection = selection.transform(new_testdata_tfidf)
#transform the new data to see if the feature remove or not, because after tfidf i use chi2 selection feature.
predicted = classifier.predict(feature_selection )
但它给了我输出
File "C:\Users\Myfile\OneDrive\Desktop\model.py", line 170, in <module>
new_testdata_tfidf= tfidf.transform(new_sentence)
File "E:\anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 1898, in transform
X = super().transform(raw_documents)
File "E:\anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 1265, in transform
"Iterable over raw text documents expected, "
ValueError: Iterable over raw text documents expected, string object received.
如何解决这个问题? 任何帮助都非常感谢。
【问题讨论】:
标签: python pandas tensorflow machine-learning scikit-learn