今天在学习Python机器语言中特征数据抽取的时候,有一段代码报错信息如下:
res = vector.fit_transform("hello word , i like apples", "word hello, he like apples")
File "E:\pythonwork\venv\lib\site-packages\sklearn\feature_extraction\text.py", line 1309, in fit_transform
raise ValueError(
ValueError: Iterable over raw text documents expected, stri[ng object received.
这是因为某处代码传参时,应给他传入list参数,而自己却给传了string参数导致的。
解决方法:
在所参数外加上“[]”变成list类型即可。
原报错行代码:
res = vector.fit_transform("hello word , i like apples", "word hello, he like apples")
改正后代码:
from sklearn.feature_extraction.text import CountVectorizer
# 实例化CountVectorizer
vector = CountVectorizer()
# 调用fit_transform 输入并转换数据
res = vector.fit_transform(["hello word , i like apples", "word hello, he like apples"])
# 打印结果
print(vector.get_feature_names())
print(res.toarray())
基础不牢,地动山摇;
在此分享,与君共勉。