Search code examples
pythonnlptf-idftfidfvectorizer

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


I got error like this :

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

When I run these codes in 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. I also tried to convert the xtrain to list with xtrain.tolist(), but it doesn't work for me either.


Solution

  • From the code you provided nothing seems wrong. However, I hypothesize that somewhere before that block of code, you assigned an object to the variable name list (most likely something along the lines of list = [...]) which is usually the cause of this error.

    Try to find that line of code if it exists and rename that variable. Generally it is not a good idea to rename built-in types for this reason. For more info read this