Python: 列表对象没有属性 'lower' - 但语料库已经是小写

Python: list object has no attribute 'lower' - but corpus is already in lower case

我的语料库是一系列包含推特数据的文档,并已根据我的知识进行了清理和预处理(甚至包括表情符号)- 示例如下:

    0         [national, interest, think, worth, holding, ta...
    1         [must, accurate, diane, abbott, done, calculat...

然后我实例化 TFIDF:

    # Instantiate vectoriser
    vect = TfidfVectorizer()

    # Fit
    vect = TfidfVectorizer(min_df=10, ngram_range = (1,3)).fit(text)

当我试图适应这个时,我得到:

   AttributeError: 'list' object has no attribute 'lower' 

但我已经将所有内容都转换为小写了。这与它是一个系列有关吗?

Convert a collection of raw documents to a matrix of TF-IDF features.

从这个意义上讲,您在此处复制的数据框中传递了一系列 list

from sklearn.feature_extraction.text import TfidfVectorizer
import pandas as pd

l1 = 'national, interest, think, worth, holding,'.split(',')
l2 = 'must, accurate, diane, abbott, done'.split(',')

df = pd.DataFrame([[l1],[l2]])

text = df[0]

您的文本参数 returns 为:

0    [national,  interest,  think,  worth,  holding, ]
1            [must,  accurate,  diane,  abbott,  done]
Name: 0, dtype: object

这显然行不通,正如指出的那样,TfidfVectorizer 接受字符串或文档。在您的情况下,根据示例,尽管与您的 example.

有点反直觉
corpus = text.apply(lambda x: ','.join(x)).to_list() # converts your series into a list of strings

vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)

print(vectorizer.get_feature_names())

['abbott', 'accurate', 'diane', 'done', 'holding', 'interest', 'must', 'national', 'think', 'worth']