python - python特征提取:AttributeError:'list'对象没有属性'lower'
问题描述
如果我在写这个::
bow_vect = CountVectorizer(max_df=0.90, min_df=2, max_features=1000, stop_words='english')
bow = bow_vect.fit_transform(combi['tidy_tweet'])
我收到此错误::
AttributeError Traceback (most recent call last)
<ipython-input-65-745529b5930e> in <module>
1 bow_vect = CountVectorizer(max_df=0.90, min_df=2, max_features=1000, stop_words='english')
----> 2 bow = bow_vect.fit_transform(combi['tidy_tweet'])
c:\users\avinash\appdata\local\programs\python\python37\lib\site-packages\sklearn\feature_extraction\text.py in fit_transform(self, raw_documents, y)
1010
1011 vocabulary, X = self._count_vocab(raw_documents,
-> 1012 self.fixed_vocabulary_)
1013
1014 if self.binary:
c:\users\avinash\appdata\local\programs\python\python37\lib\site-packages\sklearn\feature_extraction\text.py in _count_vocab(self, raw_documents, fixed_vocab)
920 for doc in raw_documents:
921 feature_counter = {}
--> 922 for feature in analyze(doc):
923 try:
924 feature_idx = vocabulary[feature]
c:\users\avinash\appdata\local\programs\python\python37\lib\site-packages\sklearn\feature_extraction\text.py in <lambda>(doc)
306 tokenize)
307 return lambda doc: self._word_ngrams(
--> 308 tokenize(preprocess(self.decode(doc))), stop_words)
309
310 else:
c:\users\avinash\appdata\local\programs\python\python37\lib\site-packages\sklearn\feature_extraction\text.py in <lambda>(x)
254
255 if self.lowercase:
--> 256 return lambda x: strip_accents(x.lower())
257 else:
258 return strip_accents
AttributeError: 'list' object has no attribute 'lower'
解决方案
在不知道combi['tidy_tweet']
实际上是什么类型的情况下,这很可能是因为 fit_transform 需要一个可迭代的字符串,而您正在为它提供一个系列。
combi['tidy_tweet']
实际上应该是 fit_transform 工作的字符串列表。目前它看起来像是一个字符串列表的系列。
因此,您最好的选择是将每一行(列表)中的标记连接成一个字符串,将这些字符串打包成一个列表,然后在其上使用 fit_transform。
推荐阅读
- r - 子集错误。默认值。找不到对象
- python - 逻辑运算符 OR 内部 for 循环
- c# - 无法让 Surface Duo 使用 Xamarin 使用两个屏幕
- flutter - 在 Flutter 中使用 Google ML-Kit 设备端文本识别
- python - 当张量不提供 numpy() 时如何获取数据
- javascript - 在 React 中提交表单后函数不返回组件
- c - VS 代码:来自命令“-exec-run”的意外 GDB 输出
- mysql - 对相似的 meta_key 值进行分组并使用 meta_value 数据创建结果集
- jestjs - 将 Jest 与 @web/test-runner 一起使用
- wpf - Prism - 在多外壳应用程序 (WPF) 中每个外壳使用子 IoC 容器