我想同时使用Pipeline和TransformedTargetRegressor来处理所有的缩放(数据和目标):是否可以混合使用Pipeline和TransformedTargetRegressor?如何从TransformedTargetRegressor中获取结果?
$ cat test_ttr.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from sklearn.datasets import make_regression
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn import linear_model
from sklearn.pipeline import Pipeline
from sklearn.compose import TransformedTargetRegressor
def main():
x, y = make_regression()
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
model = linear_model.Ridge(alpha=1)
pipe = Pipeline([('scale', preprocessing.StandardScaler()), ('model', model)])
treg = TransformedTargetRegressor(regressor=pipe, transformer=preprocessing.MinMaxScaler())
treg.fit(x_train, y_train)
print(pipe.get_params()['model__alpha']) # OK !
print(treg.get_params()['regressor__model__coef']) # KO ?!
if __name__ == '__main__':
main()
但是无法从TransformedTargetRegressor中得到结果(例如系数)
1
Traceback (most recent call last):
File ".\test_ttr.py", line 26, in <module>
main()
File ".\test_ttr.py", line 23, in main
print(treg.get_params()['regressor__model__coef']) # KO ?!
TypeError: 'TransformedTargetRegressor' object is not subscriptable
treg.get_params()
中获取coef_
或estimators_
(bagging、forests)的值。 - fghoussen