4

I have implemented simple code for gradient boosting regression (GBR) to predict one output and it works well, but when I try to predict two outputs I got error as shown below:

ValueError                                Traceback (most recent call last)
<ipython-input-5-bb1f191ee195> in <module>()
      4          }
      5 gradient_boosting_regressor = ensemble.GradientBoostingRegressor(**params)
----> 6 gradient_boosting_regressor.fit(train_data,train_targets)
      7 # 'learning_rate': 0.01

D:\Anoconda\lib\site-packages\sklearn\ensemble\gradient_boosting.py in fit(self, X, y, sample_weight, monitor)
    977 
    978         # Check input
--> 979         X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'], dtype=DTYPE)
    980         n_samples, self.n_features_ = X.shape
    981         if sample_weight is None:

D:\Anoconda\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    576                         dtype=None)
    577     else:
--> 578         y = column_or_1d(y, warn=True)
    579         _assert_all_finite(y)
    580     if y_numeric and y.dtype.kind == 'O':

D:\Anoconda\lib\site-packages\sklearn\utils\validation.py in column_or_1d(y, warn)
    612         return np.ravel(y)
    613 
--> 614     raise ValueError("bad input shape {0}".format(shape))
    615 
    616 

ValueError: bad input shape (22, 2)

Could I get any assistance or idea to predict two outputs using GBR?

My try as below:

Data_ini = pd.read_excel('Data - 2output -Ra-in - angle.xlsx').iloc[:,:]  
Data_ini_val = pd.read_excel('val - Ra-in -angle 12.xlsx').iloc[:,:] 
train_data = Data_ini.iloc[:,:4] 
train_targets =  Data_ini.iloc[:,-2:]
val_data = Data_ini_val.iloc[:,:4] 
val_targets = Data_ini_val.iloc[:,-2:] 
params = {'n_estimators': 5000, 'max_depth': 4, 'min_samples_split': 2, 'min_samples_leaf': 2
         }
gradient_boosting_regressor = ensemble.GradientBoostingRegressor(**params)
gradient_boosting_regressor.fit(train_data,train_targets)
vestland
  • 55,229
  • 37
  • 187
  • 305
SH_IQ
  • 631
  • 5
  • 14

1 Answers1

5

Use MultiOutputRegressor for that.

Multi target regression

This strategy consists of fitting one regressor per target. This is a simple strategy for extending regressors that do not natively support multi-target regression.

Example:

from sklearn.multioutput import MultiOutputRegressor

...

params = {'n_estimators': 5000, 'max_depth': 4, 'min_samples_split': 2, 'min_samples_leaf': 2
         }

estimator = MultiOutputRegressor(ensemble.GradientBoostingRegressor(**params))
estimator.fit(train_data,train_targets)
Amine Benatmane
  • 1,191
  • 1
  • 8
  • 15
  • Does this care about correlation between the two dataset, or does it just train two separate models completely independent of each other? i.e. is this more than fitting two separate model on `y, x[:,0]` and `y, x[:,1]`? – Neinstein Jul 05 '23 at 09:17