Python 属性错误:';列表';对象没有属性';下';在TF-IDF中

Python 属性错误:';列表';对象没有属性';下';在TF-IDF中,python,pandas,tf-idf,countvectorizer,Python,Pandas,Tf Idf,Countvectorizer,我正在尝试在熊猫专栏中应用TF-IDF 资料 我知道要使用CountVectorier,我需要将列转换为list(这就是我试图做的) 要应用TFIDF,我无法应用列表(我尝试将其转换为字符串) 但我仍然有这个错误 AttributeError Traceback (most recent call last) <ipython-input-239-92f296939ea7> in <module>() 16

我正在尝试在熊猫专栏中应用TF-IDF

资料

我知道要使用CountVectorier,我需要将列转换为list(这就是我试图做的)

要应用TFIDF,我无法应用列表(我尝试将其转换为字符串)

但我仍然有这个错误

AttributeError                            Traceback (most recent call last)
<ipython-input-239-92f296939ea7> in <module>()
     16  
---> 17 tf_idf_vector=tfidf_transformer.transform(cv.transform([documento]))

AttributeError: 'list' object has no attribute 'lower'
AttributeError回溯(最近一次调用)
在()
16
--->17 tf_idf_vector=tfidf_transformer.transform(cv.transform([documento]))
AttributeError:“list”对象没有属性“lower”

我只是在猜测,因为我没有使用sklearn,您也没有发布完整的stacktrace,但异常看起来需要一个字符串列表作为参数,并调用字符串元素的“lower()”

但您要做的是为它提供一个包含字符串的列表:

corpus=[1,2,3]
document=[''.join(str(item))用于语料库中的项目]
打印(文件)
>>> ['1','2','3']
打印([文档])
>>> [['1','2','3']]
我敢打赌,如果你只是打电话,它会被修复:

tf_idf_vector=tfidf_transformer.transform(cv.transform(文档))

您可以使用sklearn管道来简化这一过程

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.pipeline import Pipeline 

tf_idf = Pipeline([('cv',CountVectorizer()), ('tfidf_transformer',TfidfTransformer(smooth_idf=True,use_idf=True))])


tf_idf_vector  = tf_idf.fit_transform(corpus)

但这是另一个问题。我不能问?对不起,如果禁止的话,我会删除这个问题。是的,这是不一样的…你为什么不使用。。简单得多
AttributeError                            Traceback (most recent call last)
<ipython-input-239-92f296939ea7> in <module>()
     16  
---> 17 tf_idf_vector=tfidf_transformer.transform(cv.transform([documento]))

AttributeError: 'list' object has no attribute 'lower'
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.pipeline import Pipeline 

tf_idf = Pipeline([('cv',CountVectorizer()), ('tfidf_transformer',TfidfTransformer(smooth_idf=True,use_idf=True))])


tf_idf_vector  = tf_idf.fit_transform(corpus)