当我在自然语言处理中使用 TF-IDF 时,它说 list is not callable.Can 你帮我看看?

When I use TF-IDF in Natural language processing, it said list is not callable.Can you help me with it?

我遇到这样的错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-38-b9ac626e6121> in <module>
      5 
      6 # Fitting TF-IDF to both training and test sets (semi-supervised learning)
----> 7 tfv.fit(list(xtrain) + list(xvalid))
      8 xtrain_tfv =  tfv.transform(xtrain)
      9 xvalid_tfv = tfv.transform(xvalid)

TypeError: 'list' object is not callable

当我运行这些代码在python:

tfv = TfidfVectorizer(min_df=3,  max_features=None, 
            strip_accents='unicode', analyzer='word',token_pattern=r'\w{1,}',
            ngram_range=(1, 3), use_idf=1,smooth_idf=1,sublinear_tf=1,
            stop_words = 'english')

# Fitting TF-IDF to both training and test sets (semi-supervised learning)
tfv.fit(list(xtrain) + list(xvalid))
xtrain_tfv =  tfv.transform(xtrain) 
xvalid_tfv = tfv.transform(xvalid)

P.S。我还尝试将 xtrain 转换为带有 xtrain.tolist() 的列表,但它对我也不起作用。

从您提供的代码看来没有任何问题。但是,我假设在该代码块之前的某处,您将一个对象分配给了变量名称 list(很可能是 list = [...] 的内容),这通常是导致此错误的原因。

尝试找到该行代码(如果存在)并重命名该变量。由于这个原因,重命名内置类型通常不是一个好主意。有关详细信息,请阅读