使用 scikit RFE 获取参数数量错误

Getting number of arguments error with scikit RFE

我遇到了这个错误。我认为这是我本地设置的问题。

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

当我 运行 jupyter

上的单元格时出现以下错误
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/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

以上代码似乎适用于其他人。

在RFEclass(https://github.com/scikit-learn/scikit-learn/blob/7e1e6d09b/sklearn/feature_selection/_rfe.py#L176)的__init__方法中可以看到,只能传递两个位置参数(selfestimator).

根据 Python documentation:

Parameters after “*” or “*identifier” are keyword-only parameters and may only be passed used keyword arguments.

在你的情况下,你应该输入:

rfe = RFE(lm, n_features_to_select=10)