0

我想通过与原始特征堆叠形成的元特征来预测结果。
我使用 mlxtend 进行堆叠,并尝试将原始功能与元功能一起使用,但这个库不能很好地工作。

from lightgbm import LGBMRegressor
from sklearn.ensemble import RandomForestRegressor

from sklearn.datasets import load_boston
from mlxtend.regressor import StackingRegressor
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import cross_validate

boston= load_boston()
y = boston['target']
X = boston['data']

class extAll(BaseEstimator, TransformerMixin):
  def __init__(self):
      pass
  def fit(self, X, y=None):
      return self
  def transform(self, X):
      return self
  def predict(self, X):
      return self
RF =  RandomForestRegressor()
LGBM = LGBMRegressor()
pipe = make_pipeline(extAll())
stack1 = StackingRegressor(regressors=[RF,LGBM,pipe], meta_regressor=LGBM, verbose=1)
scores = cross_validate(stack1, X, y, cv=10)

并且发生错误

Fitting 3 regressors...
Fitting regressor1: randomforestregressor (1/3)
Fitting regressor2: lgbmregressor (2/3)
Fitting regressor3: pipeline (3/3)
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "C:\ProgramData\Anaconda3\lib\site-packages\mlxtend\regressor\stacking_regression.py", line 154, in fit
    meta_features = self.predict_meta_features(X)
  File "C:\ProgramData\Anaconda3\lib\site-packages\mlxtend\regressor\stacking_regression.py", line 221, in predict_meta_features
    return np.column_stack([r.predict(X) for r in self.regr_])
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\shape_base.py", line 369, in column_stack
    return _nx.concatenate(arrays, 1)
ValueError: all the input array dimensions except for the concatenation axis must match exactly

我认为这是由具有多维的原始数据引起的。
我想知道更好的方法或工具。
我应该怎么办?

4

1 回答 1

0

代码在预测部分有一些错误。它应该是正确的

class extAll(BaseEstimator, TransformerMixin,RegressorMixin):
 def __init__(self):
    pass
 def fit(self, X, y=None):
    return self
 def transform(self, X):
    return self
 def predict(self, X):
    return X

当我们开发 scikit-learn 类型的方法时,需要 RegressorMixin 或 ClassifierMixin 进行预测。这段代码运行良好。

于 2019-03-01T05:12:02.260 回答