【问题标题】:AttributeError: 'list' object has no attribute 'lower' - SklearnAttributeError: 'list' 对象没有属性 'lower' - Sklearn
【发布时间】:2020-12-11 07:20:00
【问题描述】:

我已经使用 csv 中的一些数据训练了一个聊天机器人。所有的 tçdatabase 和模型都工作正常,它可以预测句子的标签,但我希望聊天机器人预测输入的标签。 这是代码(是一个sklearn管道):

while True:
  usr_inp = input("Tú: ")
  X_predict = train_test_split(usr_inp , test_size=0.1 , random_state=12)
  prediction = pipeline.predict(X_predict)
  print("Prediction: " , prediction)

它给出了这个错误:

    ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-41-db8a012d9786> in <module>()
     47   usr_inp = input("Tú: ")
     48   X_predict = train_test_split(usr_inp , test_size=0.1 , random_state=12)
---> 49   prediction = pipeline.predict(X_predict)
     50   print("Prediction: " , prediction)

6 frames
/usr/local/lib/python3.6/dist-packages/sklearn/feature_extraction/text.py in _preprocess(doc, accent_function, lower)
     66     """
     67     if lower:
---> 68         doc = doc.lower()
     69     if accent_function is not None:
     70         doc = accent_function(doc)

AttributeError: 'list' object has no attribute 'lower'

型号:

#Model & Pipeline

model = LogisticRegression(C=25 , solver="saga" , max_iter=2000)
pipeline = Pipeline([("tfidf" , vectorizer) , ("clf" , model)])

pipeline.fit(X_train , X_test)

有人可以帮我吗? 谢谢

【问题讨论】:

    标签: python pandas scikit-learn


    【解决方案1】:

    您的X_predict 可能是一个字符串列表,因此没有lower 属性。如果您可以访问pipeline.predict 源代码,您可以将其更改为列表推导式。

    doc = [item.lower() for item in doc]
    

    如果没有,您应该查看pipeline.predict 的文档,我猜它包含一个字符串参数。如果是这样,你可以将join列表转成字符串

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 2020-08-21
      • 2019-01-01
      • 2021-12-30
      • 2019-11-06
      • 2019-03-19
      相关资源
      最近更新 更多