sklearn.metrics.mean_squared_error:
标准均方误。
Gradient Tree Boosting方法可以看作将AdaBoost方法的模型组合方式应用于决策树,
但并不采取单点迭代惩罚加权,而是对某个模型采用对所有样本的距离模式(l2 l1 and so on)
来求解。
标准均方误。
Gradient Tree Boosting方法可以看作将AdaBoost方法的模型组合方式应用于决策树,
但并不采取单点迭代惩罚加权,而是对某个模型采用对所有样本的距离模式(l2 l1 and so on)
来求解。
在GTB中设计后的模型中调用loss_可以返回使用的损失函数,可以用来计算单步损失。
调用feature_importance_可以返回模型的特征重要度量。
调用feature_importance_可以返回模型的特征重要度量。
plt.barh用于绘制条形图。
下面是一个例子:
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
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:]
params = {'n_estimators': 500, 'max_depth': 4, 'min_samples_split': 1, 'learning_rate': 0.01, 'loss': 'ls'}
clf = ensemble.GradientBoostingRegressor(**params)
clf.fit(X_train, y_train)
mse = mean_squared_error(y_test, clf.predict(X_test))
print "MSE: %.4f" % mse
test_score = np.zeros((params["n_estimators"],), dtype = np.float64)
for i, y_pred in enumerate(clf.staged_predict(X_test)):
test_score[i] = clf.loss_(y_test, y_pred)
plt.figure(figsize = (12, 6))
plt.subplot(1, 2, 1)
plt.title("Deviance")
plt.plot(np.arange(params["n_estimators"]) + 1, clf.train_score_, 'b-', label = "Training Set Deviance")
plt.plot(np.arange(params["n_estimators"]) + 1, test_score, "r-", label = "Test Set Deviamce")
plt.legend(loc = 'upper right')
plt.xlabel('Boosting Iterations')
plt.ylabel('Deviance')
feature_importance = clf.feature_importances_
feature_importance = 100.0 * (feature_importance / feature_importance.max())
sorted_idx = np.argsort(feature_importance)
pos = np.arange(sorted_idx.shape[0]) + .5
plt.subplot(1, 2, 2)
plt.barh(pos, feature_importance[sorted_idx], align = 'center')
plt.yticks(pos, boston.feature_names[sorted_idx])
plt.xlabel("Relative Importance")
plt.title("Variance Importance")
plt.show()