首页 > 解决方案 > 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'

标签: pythonscikit-learnnltkfeature-extraction

解决方案


在不知道combi['tidy_tweet']实际上是什么类型的情况下,这很可能是因为 fit_transform 需要一个可迭代的字符串,而您正在为它提供一个系列。

combi['tidy_tweet']实际上应该是 fit_transform 工作的字符串列表。目前它看起来像是一个字符串列表的系列。

因此,您最好的选择是将每一行(列表)中的标记连接成一个字符串,将这些字符串打包成一个列表,然后在其上使用 fit_transform。


推荐阅读