【发布时间】: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