如何使用梯度增强回归预测多输出?

时间:2019-09-26 09:06:09

标签: python-3.x scikit-learn regression prediction

我为gradient boosting regression (GBR)实现了简单的代码来预测one output,并且效果很好,但是当我尝试预测two outputs时,出现了如下所示的错误:

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)

我可以得到任何帮助或想法来使用two outputs来预测GBR吗?

我的尝试如下:

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)

1 个答案:

答案 0 :(得分:1)

为此使用MultiOutputRegressor

  

多目标回归

     

此策略包括为每个目标安装一个回归器。这是一个   扩展本机不支持的回归器的简单策略   多目标回归。

示例:

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)