sklearn 可视化模型的训练测试收敛情况和特征重要性

 show the code: 

# Plot training deviance
def plot_training_deviance(clf, n_estimators, X_test, y_test):
    # compute test set deviance
    test_score = np.zeros((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')
    train_score = clf.train_score_
    logging.info("len(train_score): %s" % len(train_score))
    logging.info(train_score)
    logging.info("len(test_score): %s" % len(test_score))
    logging.info(test_score)
    plt.plot(np.arange(n_estimators) + 1, train_score, 'b-',
             label='Training Set Deviance')
    plt.plot(np.arange(n_estimators) + 1, test_score, 'r*', label='Test Set Deviance')
    plt.legend(loc='upper right')
    plt.xlabel('Boosting Iterations')
    plt.ylabel('Deviance')
    plt.show()


# Plot feature importance
def plot_feature_importance(clf, feature_names):
    feature_importance = clf.feature_importances_
    # make importances relative to max importance
    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, feature_names[sorted_idx])
    plt.yticks(pos, [feature_names[idx] for idx in sorted_idx])
    plt.xlabel('Relative Importance')
    plt.title('Variable Importance')
    plt.show()


class Train(object):
    def __init__(self, data_file):
        self.data_file = data_file
        self.x_fields = ["xxx", "xxx", "xxx"]
        self.x_features, self.y_labels = self.load_data()

    def load_data(self):
        x_features, y_labels = [], []
        # ......
        return x_features, y_labels

    def train_model(self):
        model = GradientBoostingRegressor(random_state=42)
        model.fit(self.x_features, self.y_labels)
        y_pred = model.predict(self.x_features)
        logging.info("mean_squared_error: %.6f" % mean_squared_error(self.y_labels, y_pred))
        logging.info("mean_squared_log_error: %.6f" % mean_squared_log_error(self.y_labels, y_pred))

        plot_training_deviance(clf=model, n_estimators=model.get_params()["n_estimators"], X_test=self.x_features, y_test=self.y_labels)
                               
        # 输出feature重要性
        logging.info("feature_importances_: %s" % model.feature_importances_)
        plot_feature_importance(clf=model, feature_names=self.x_fields)

 

参考的是sklearn中的样例: Gradient Boosting regression — scikit-learn 0.19.2 documentation

 画出的图如下所示:

 

转载于:https://www.cnblogs.com/bymo/p/9483584.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值