码迷,mamicode.com
首页 > 编程语言 > 详细

吴裕雄 python 机器学习——集成学习随机森林RandomForestRegressor回归模型

时间:2019-05-02 09:46:56      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:data   pytho   参数   alt   title   ESS   scores   spl   otl   

import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets,ensemble
from sklearn.model_selection import train_test_split

def load_data_regression():
    ‘‘‘
    加载用于回归问题的数据集
    ‘‘‘
    #使用 scikit-learn 自带的一个糖尿病病人的数据集
    diabetes = datasets.load_diabetes() 
    # 拆分成训练集和测试集,测试集大小为原始数据集大小的 1/4
    return train_test_split(diabetes.data,diabetes.target,test_size=0.25,random_state=0) 

#集成学习随机森林RandomForestRegressor回归模型
def test_RandomForestRegressor(*data):
    X_train,X_test,y_train,y_test=data
    regr=ensemble.RandomForestRegressor()
    regr.fit(X_train,y_train)
    print("Traing Score:%f"%regr.score(X_train,y_train))
    print("Testing Score:%f"%regr.score(X_test,y_test))
    
# 获取分类数据
X_train,X_test,y_train,y_test=load_data_regression() 
# 调用 test_RandomForestRegressor
test_RandomForestRegressor(X_train,X_test,y_train,y_test) 

技术图片

def test_RandomForestRegressor_num(*data):
    ‘‘‘
    测试 RandomForestRegressor 的预测性能随  n_estimators 参数的影响
    ‘‘‘
    X_train,X_test,y_train,y_test=data
    nums=np.arange(1,100,step=2)
    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)
    testing_scores=[]
    training_scores=[]
    for num in nums:
        regr=ensemble.RandomForestRegressor(n_estimators=num)
        regr.fit(X_train,y_train)
        training_scores.append(regr.score(X_train,y_train))
        testing_scores.append(regr.score(X_test,y_test))
    ax.plot(nums,training_scores,label="Training Score")
    ax.plot(nums,testing_scores,label="Testing Score")
    ax.set_xlabel("estimator num")
    ax.set_ylabel("score")
    ax.legend(loc="lower right")
    ax.set_ylim(-1,1)
    plt.suptitle("RandomForestRegressor")
    plt.show()
    
# 调用 test_RandomForestRegressor_num
test_RandomForestRegressor_num(X_train,X_test,y_train,y_test) 

技术图片

def test_RandomForestRegressor_max_depth(*data):
    ‘‘‘
    测试 RandomForestRegressor 的预测性能随  max_depth 参数的影响
    ‘‘‘
    X_train,X_test,y_train,y_test=data
    maxdepths=range(1,20)
    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)
    testing_scores=[]
    training_scores=[]
    for max_depth in maxdepths:
        regr=ensemble.RandomForestRegressor(max_depth=max_depth)
        regr.fit(X_train,y_train)
        training_scores.append(regr.score(X_train,y_train))
        testing_scores.append(regr.score(X_test,y_test))
    ax.plot(maxdepths,training_scores,label="Training Score")
    ax.plot(maxdepths,testing_scores,label="Testing Score")
    ax.set_xlabel("max_depth")
    ax.set_ylabel("score")
    ax.legend(loc="lower right")
    ax.set_ylim(0,1.05)
    plt.suptitle("RandomForestRegressor")
    plt.show()
    
# 调用 test_RandomForestRegressor_max_depth
test_RandomForestRegressor_max_depth(X_train,X_test,y_train,y_test) 

技术图片

def test_RandomForestRegressor_max_features(*data):
    ‘‘‘
   测试 RandomForestRegressor 的预测性能随  max_features 参数的影响
    ‘‘‘
    X_train,X_test,y_train,y_test=data
    max_features=np.linspace(0.01,1.0)
    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)
    testing_scores=[]
    training_scores=[]
    for max_feature in max_features:
        regr=ensemble.RandomForestRegressor(max_features=max_feature)
        regr.fit(X_train,y_train)
        training_scores.append(regr.score(X_train,y_train))
        testing_scores.append(regr.score(X_test,y_test))
    ax.plot(max_features,training_scores,label="Training Score")
    ax.plot(max_features,testing_scores,label="Testing Score")
    ax.set_xlabel("max_feature")
    ax.set_ylabel("score")
    ax.legend(loc="lower right")
    ax.set_ylim(0,1.05)
    plt.suptitle("RandomForestRegressor")
    plt.show()
    
# 调用 test_RandomForestRegressor_max_features
test_RandomForestRegressor_max_features(X_train,X_test,y_train,y_test) 

技术图片

 

吴裕雄 python 机器学习——集成学习随机森林RandomForestRegressor回归模型

标签:data   pytho   参数   alt   title   ESS   scores   spl   otl   

原文地址:https://www.cnblogs.com/tszr/p/10801596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!