python中基于深度depth的回归决策树分析

import numpy as np
from sklearn.tree  import DecisionTreeRegressor
from sklearn import cross_validation
import matplotlib.pyplot as plt

#给出一个随机产生的数据

def create_data(n):
    np.random.seed(0)
    X = 5*np.random.rand(n,1)
    y = np.sin(X).ravel()
    noise_num=(int)(n/5)
    y[::5] += 3*(0.5 - np.random.rand(noise_num))
    return  cross_validation.train_test_split(X,y,test_size=0.25,random_state=1)


#参数  n为数据集容量
#x是随机在0-1之间产生的 ,y是sin(x),其中y每隔5个点添加一个随机噪声

def test_DecisionTreeRegressor_depth(*data,maxdepth):
            X_train,X_test,y_train,y_test = data
            depths = np.arange(1,maxdepth)
            training_scores= []
            testing_scores  = []
            for depth in depths:
                    regr = DecisionTreeRegressor(max_depth = 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))
                    
                    
            fig = plt.figure()
            ax = fig.add_subplot(1,1,1)
            ax.plot(depths,training_scores,label="traing score")
            ax.plot(depths,testing_scores,label="testing_score")
            ax.set_xlabel("maxdepth")
            ax.set_ylabel("score")
            ax.set_title("Decision tree Regression")
            ax.legend(framealpha=0.5)
            plt.show()

X_train,X_test,y_train,y_test = create_data(100)
print(X_train)
print(X_test)
print(y_train)
print(y_test)
test_DecisionTreeRegressor_depth(X_train,X_test,y_train,y_test,maxdepth=20)

#运行结果可以看到,随着树的深度加深,模型对训练集和预测集的拟合都在提升,样本只有100个
#树深度为7之后,无法划分
                    
                    

分类结果如下:


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值