我正在使用TransformedTargetRegressor将目标转换为日志空间。就像
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.compose import TransformedTargetRegressor
clf = TransformedTargetRegressor(regressor=GradientBoostingRegressor(**params),
func=np.log1p, inverse_func=np.expm1)
但是我以后再打电话
feature_importance = clf.feature_importances_
我明白了
AttributeError:“ TransformedTargetRegressor”对象没有属性 'feature_importances _'
我本以为原始类的所有属性都会被继承。该如何解决?
对于其他情况,here是一个官方示例。用我的替换初始化行会导致崩溃。
答案 0 :(得分:3)
正如TransformedTargetRegressor Doc所说,人们可以通过.regressor_
访问其组件回归器。
这就是您想要的:
clf.regressor_.feature_importances_
可行代码:
import numpy as np
import matplotlib.pyplot as plt
from sklearn import ensemble
from sklearn import datasets
from sklearn.utils import shuffle
from sklearn.metrics import mean_squared_error
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.compose import TransformedTargetRegressor #only in sklearn==0.20.2
# #############################################################################
# Load data
boston = datasets.load_boston()
X, y = shuffle(boston.data, boston.target, random_state=13)
X = X.astype(np.float32)
offset = int(X.shape[0] * 0.9)
X_train, y_train = X[:offset], y[:offset]
X_test, y_test = X[offset:], y[offset:]
# #############################################################################
# Fit regression model
params = {'n_estimators': 500, 'max_depth': 4, 'min_samples_split': 2,
'learning_rate': 0.01, 'loss': 'ls'}
#clf = ensemble.GradientBoostingRegressor(**params)
clf = TransformedTargetRegressor(regressor=GradientBoostingRegressor(**params),
func=np.log1p, inverse_func=np.expm1)
clf.fit(X_train, y_train)
mse = mean_squared_error(y_test, clf.predict(X_test))
print("MSE: %.4f" % mse)
print(clf.regressor_.feature_importances_)
其输出:
MSE: 7.7145 [6.45223704e-02 1.32970011e-04 2.92221184e-03 4.48101769e-04 3.57392613e-02 2.02435922e-01 1.22755948e-02 7.03996426e-02 1.54903176e-03 1.90771421e-02 1.98577625e-02 1.63376111e-02 5.54302378e-01]