我正在使用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_'
我本以为原始类的所有属性都会被继承。如何解决这个问题?
这里有一个官方的例子。用我的初始化行替换会导致崩溃。
正如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-043.57392613e-02 2.02435922e-01 1.22755948e-02 7.03996426e-021.54903176e-03 1.90771421e-02 1.98577625e-02 1.63376111e-025.54302378e-01]