【问题标题】:ValueError: Iterable over raw text documents expected, string object received. Predicting new test data with tfidf and selection featureValueError:预期可迭代原始文本文档,接收到字符串对象。使用 tfidf 和选择功能预测新的测试数据
【发布时间】: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


    【解决方案1】:

    如果您尝试在代码中使用 new_sentence = input[('')] 传递字符串列表,那么您可能希望将其替换为

    new_sentence = [input()]
    

    希望这会有所帮助。

    【讨论】:

    • @Sdtv 感谢您的确认。没问题,我也是新来的。一切顺利。
    【解决方案2】:

    您是否尝试过将新句子作为数组传递?即

    new_testdata_tfidf= tfidf.transform([new_sentence])
    

    第一个实例是传递一个带有一个字符串元素的数组,另一个实例是简单地传递一个字符串

    【讨论】:

    • 谢谢它的正常工作。抱歉不能投票我是新人(我的代表少于 15 个)。非常感谢!
    • 再次感谢,这对我有用,将字符串变量放入括号 [] bag_of_words_for_message = bag_of_words_trans.transform([message])
    猜你喜欢
    • 2019-10-07
    • 2018-09-23
    • 2018-10-23
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多