简体   繁体   中英

AttributeError: 'tuple' object has no attribute 'fit'

I want to use bagging with XGBoost an Mlpregression If i use one algorithm it will work properly

XGBRegressor_bagging_model = BaggingRegressor(XGBRegressor_model,
                                              n_estimators=100,                                              
                                              max_samples=1.0, 
                                              max_features=1.0, 
                                              bootstrap=True,
                                              oob_score=True, 
                                              warm_start=False,
                                              n_jobs=-1,
                                              verbose=0)

MLP = BaggingRegressor(MLPRegressor_Model,
                       n_estimators=1000,
                       max_samples=1.0,
                       max_features=1.0,
                       bootstrap=True,
                       oob_score=True,
                       warm_start=False,
                       n_jobs=-1,
                       verbose=0)

XGBRegressor_bagging_model.fit(X_train, y_train)
MLP.fit(X_train, y_train)

print("XGBRegressor_bagging_model Predicted Is:", XGBRegressor_bagging_model.predict(X_test)[0:5])

print("MLP Predicted Is:", MLP.predict(X_test)[0:5])

print("XGBRegressor_bagging_model Score Is:", XGBRegressor_bagging_model.oob_score_)
print("MLP Score Is:", MLP.oob_score_)

but if i use it like this

bagging_model = BaggingRegressor((XGBRegressor_model, MLPRegressor_Model), n_estimators=100,max_samples=1.0, max_features=1.0, bootstrap=True, oob_score=True, warm_start=False, n_jobs=-1, verbose=0)

it wont work and show me this error

AttributeError: 'tuple' object has no attribute 'fit'

what should i do to fix this problem?

In your 2nd version, you're passing (XGBRegressor_model, MLPRegressor_Model) as the regressor. This is not a regressor, but rather a tuple (which happens to be composed of regressors). The error states that tuple does not have the method fit .

You should pass one of these regressors, or create a composite regressor from both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM