Getting number of arguments error with scikit rfe ( Python, Scikit Learn )

Problem :
( Scroll to solution )

I am learning machine learning and came across this error. I think it is an issue with my local setup.

# Importing RFE and LinearRegression
from sklearn.feature_selection import RFE
from sklearn.linear_model import LinearRegression

# Running RFE with the output number of the variable equal to 10
lm = LinearRegression()
lm.fit(X_train, y_train)

rfe = RFE(lm, 10)             # running RFE
rfe = rfe.fit(X_train, y_train)

I get the following error when I run the cell on jupyter

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_3420/241754350.py in <module>
      3 lm.fit(X_train, y_train)
      4 
----> 5 rfe = RFE(lm, 10)             # running RFE
      6 rfe = rfe.fit(X_train, y_train)

TypeError: __init__() takes 2 positional arguments but 3 were given

The above code seems to be working for others. Any help would be appreciated.

Thanks.

As you can see in the init method of RFE class (github.com/scikit-learn/scikit-learn/blob/7e1e6d09b/sklearn/feature_selection/_rfe.py#L176) you can only pass two positional arguments (self + estimator).
According to python docs (docs.python.org/3.5/reference/compound_stmts.html#function-definitions):
Parameters after “*” or “*identifier” are keyword-only parameters and may only be passed used keyword arguments.

In your case, you should type:

rfe = RFE(lm, n_features_to_select=10)

Read more here: Source link