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

标签 python nlp tfidfvectorizer

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

    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.

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

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]

它将您的文本参数返回为:

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']

关于Python:列表对象没有属性 'lower' - 但语料库已经是小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59206375/

相关文章:

Python - 用于提取 JSON 响应项的基本命令

Python正则表达式删除字母数字字符而不删除字符串末尾的单词

Python 猜数字游戏

python - 是我的类内装饰器不够 Pythonic 还是 PyCharm 在 lint 警告方面不够智能?

c# - C# 中自然语言生成的任何库或示例?

python - 如何执行 ngram 到 ngram 关联

python - k-means 中特征的权重

scikit-learn - 将 sklearn TfidfVectorizer 与已经标记化的输入一起使用?

python - Scikit - TF-IDF 空词汇表

python - 了解 TfidfVectorizer 输出