Home > Mobile >  Python SKlearn TfidfVectorizer arguments error
Python SKlearn TfidfVectorizer arguments error

Time:11-22

I've been using the SKlearn TfidfVectorizer but suddenly it has been throwing an error:

TypeError: __init__() takes 1 positional argument but 2 positional arguments 
(and 4 keyword-only arguments) were given

The argument I'm giving is:

tfidf_vectorizer = TfidfVectorizer(X_train, ngram_range=(1,2), max_df=0.9, min_df=5, token_pattern=r'(\S )' )

where X_train is a list of strings such as:

 'done earlier siesta',
 'sunday mass us family greatful opportunity',
 'wet wet wet frustrated outside',
 'tired headache headache',
 'friends creative talented inspired friendship love creatives',
 'grateful lucky beaches sunshine hubby family pets awesome sunday',
 'latest artwork',
 'two headache sick tired sore'

Im confused as to why it would say I'm giving two positional arguments when im only inputting one which is the X_train list. Even when I simplify the statement down to:

TfidfVectorizer(X_train)

It still gives the same error saying I gave two positional arguments. I'm using Sklearn 1.0.1 but I tried reverting it to 1.0.0 and it still have the same error Could the error be in the list that I am passing in?

CodePudding user response:

library and their implementation did change. If we look at the version 0.23.1 we get a warning which states that it needs to pass with keyword args.

tfidvect=TfidfVectorizer(X_train)
FutureWarning: Pass input=['done earlier siesta', 'sunday mass us family greatful opportunity', 'wet wet wet frustrated outside', 'tired headache headache', 'friends creative talented inspired friendship love creatives', 'grateful lucky beaches sunshine hubby family pets awesome sunday', 'latest artwork', 'two headache sick tired sore'] as keyword args. From version 0.25 passing these as positional arguments will result in an error
  warnings.warn("Pass {} as keyword args. From version 0.25 "

so fast forward to 1.0.1, the same call will be like:

tfidvect1_01=TfidfVectorizer(input=X_train) # input positional argument

@Ambrayers added.

an alternative way is, create the object and then fit_transform , refer the example in official documentation

vectorizer = TfidfVectorizer()  
X_train = vectorizer.fit_transform(X_train)
  • Related