Skip to content
Advertisement

TypeError: fit() missing 1 required positional argument: ‘y’,

I want to try out all regressors within the library. Since I do know, that some of the regressors require more input I build the try and expept catch block.

for name, estimator in sklearn.utils.all_estimators(type_filter='regressor'):
    model =  make_pipeline(StandardScaler(), estimator)
    try:
        scores =  cross_validate(model, X, y, scoring='r2')
        print(name, ': ', np.mean(scores['test_score']))
    except:
        print('Does not get printed.')

This returns the following snipped many times:

FitFailedWarning: Estimator fit failed. The score on this train-test partition for these parameters will be set to nan. Details:
Traceback (most recent call last):
  File "venvpathlibsite-packagessklearnmodel_selection_validation.py", line 598, in _fit_and_score
    estimator.fit(X_train, y_train, **fit_params)
  File "venvpathvenvlibsite-packagessklearnpipeline.py", line 346, in fit
    self._final_estimator.fit(Xt, y, **fit_params_last_step)
TypeError: fit() missing 1 required positional argument: 'y'

In my opinion there are two problems here. First, exept never gets called. Second, the y input is not recognized.

I am gratefull for any kind of help.

Advertisement

Answer

all_estimators does not return instances of estimators but only their classes (see the documentation). When defining the pipeline, you should instantiate an object of that class:

for name, estimator in sklearn.utils.all_estimators(type_filter='regressor'):
    model =  make_pipeline(StandardScaler(), estimator()) # <-- change in this line

Note the () after estimator. Now you have actual objects that can be fitted to data.


Concerning the except block: by default, cross_validate will just assign np.nan to the score if an error occurs. To actually raise the error, set error_score='raise' in cross_validate:

scores =  cross_validate(model, X, y, scoring='r2', error_score='raise')
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement