Search code examples
pythonmachine-learningscikit-learnregressionlinear-regression

Attributes of base estimators in Regressor Chain


I am solving multi-output regression problem using RegressorChain in Scikit Learn, but after fitting the model i need to retrieve the fitted model base estiamtor to access the estimator attributes . I tried to access the attributes of base estimators in RegerssorChain in Scikit Learn, but I couldn't.

The following is the code I used for this.

linreg = LinearRegression() 

wrapper=RegressorChain(linreg)

fit_model=wrapper.fit(X_train, y_train)

base_estimator_para=RegressorChain.get_params(fit_model, deep=True)

base_estimator_linreg=base_estimator_para['base_estimator']

print(base_estimator_linreg.coef_)

The last line returns an error that 'LinearRegression' object has no attribute 'coef_', but LinearRegression has the attribute 'coef_'.

The function "base_estimator_para" returns a dicitonary contains objects including the base estimator itself.

base_estimator_para['base_estimator'] returns the base estimator object, so using
base_estimator_linreg.coef_ is supposed to return the coefficients of the fitted linear regression model.


Solution

  • base_estimator is the parameter passed to RegressorChain, and remains unfitted when you fit the RegressorChain. That estimator gets cloned repeatedly, and each clone gets fitted (in turn, using the previously fitted clones' predictions as additional input). You want to pick out one of the entries in estimators_, the fitted clones of base_estimator.