【问题标题】:AttributeError: 'list' object has no attribute 'lower' with CountVectorizerAttributeError: 'list' 对象没有属性 'lower' 与 CountVectorizer
【发布时间】:2020-08-21 04:39:42
【问题描述】:

我正在尝试对 Python 中的 pandas 数据框进行预测。不知何故 CountVectorizer 无法转换数据。有谁知道是什么导致了这个问题?

这是我的代码:

filename = 'final_model.sav'
print(response.status_code)
data = response.json()
print(data)

dictionary = pd.read_json('rating_company_small.json', lines=True)

dictionary_df = pd.DataFrame()
dictionary_df["comment text"] = dictionary["comment"]

data = pd.DataFrame.from_dict(json_normalize(data), orient='columns')
print(data)

df = pd.DataFrame()

df["comment text"] = data["Text"]
df["status"] = data["Status"]

print(df)
Processing.dataframe_cleaning(df)

comment_data = df['comment text']

tfidf = CountVectorizer()
tfidf.fit(dictionary_df["comment text"])
Test_X_Tfidf = tfidf.transform(df["comment text"])


print(comment_data)
print(Test_X_Tfidf)
loaded_model = pickle.load(open(filename, 'rb'))
predictions_NB = loaded_model.predict(Test_X_Tfidf)

这是数据框:

                         comment text    status
0                   [slecht, bedrijf]    string
1  [leuk, bedrijfje, goed, behandeld]  Approved
2  [leuk, bedrijfje, goed, behandeld]  Approved
3                   [leuk, bedrijfje]  Approved 

完整的错误信息:

Traceback (most recent call last):
  File "Request.py", line 36, in <module>
    Test_X_Tfidf = tfidf.transform(df["comment text"])
  File "C:\Users\junio\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 1112, in transform
    _, X = self._count_vocab(raw_documents, fixed_vocab=True)
  File "C:\Users\junio\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 970, in _count_vocab
    for feature in analyze(doc):
  File "C:\Users\junio\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 352, in <lambda>
    tokenize(preprocess(self.decode(doc))), stop_words)
  File "C:\Users\junio\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 256, in <lambda>
    return lambda x: strip_accents(x.lower())
AttributeError: 'list' object has no attribute 'lower'

我希望它返回对数据帧的预测。

【问题讨论】:

    标签: python pandas machine-learning nlp


    【解决方案1】:

    CountVectorizer 无法直接处理列表的Series,这就是您收到该错误的原因(lower 是一个字符串方法)。 我看起来你想要一个 MultiLabelBinarizer 来代替,它可以处理这个输入结构:

    from sklearn.preprocessing import MultiLabelBinarizer
    
    count_vec = MultiLabelBinarizer()
    mlb = count_vec.fit(df["comment text"])
    pd.DataFrame(mlb.transform(df["comment text"]), columns=[mlb.classes_])
    
      bedrijf bedrijfje behandeld goed leuk slecht
    0       1         0         0    0    0      1
    1       0         1         1    1    1      0
    2       0         1         1    1    1      0
    3       0         1         0    0    1      0
    

    但上述方法不会考虑列表中的重复元素,输出元素可以是01。如果这是您所期望的行为,您可以将列表加入字符串并然后使用CountVectorizer,因为它需要字符串:

    text = df["comment text"].map(' '.join)
    count_vec = CountVectorizer()
    cv = count_vec.fit(text)
    
    pd.DataFrame(cv.transform(text).toarray(), columns=[mlb.classes_])
    
      bedrijf bedrijfje behandeld goed leuk slecht
    0       1         0         0    0    0      1
    1       0         1         1    1    1      0
    2       0         1         1    1    1      0
    3       0         1         0    0    1      0
    

    请注意,此与输入字符串的tf-idf 不同。在这里,您只有实际数量。为此,您有 TfidfVectorizer,对于同一个示例,它会产生:

        bedrijf bedrijfje behandeld      goed      leuk    slecht
    0  0.707107  0.000000  0.000000  0.000000  0.000000  0.707107
    1  0.000000  0.444931  0.549578  0.549578  0.444931  0.000000
    2  0.000000  0.444931  0.549578  0.549578  0.444931  0.000000
    3  0.000000  0.707107  0.000000  0.000000  0.707107  0.000000
    

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 2019-01-01
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多