python之实战----决策树(ID3,C4.5,CART)战sin(x)+随机噪声

本文深入探讨了Python中决策树算法的应用,包括ID3算法的信息增益、C4.5的信息增益比以及CART算法的基尼指数。通过实战案例,展示了如何使用决策树进行回归和分类,并分析了决策树随着树深度增加的影响。最后,文章还涵盖了决策树的可视化展示。
摘要由CSDN通过智能技术生成

ID3是采用了信息增益作为特征的选择的度量(越大越好),而C4.5采用的是信息增益比(越大越好),CART分类树采用基尼指数选择最优特征(越小越好)。

1.回归决策树:

#-*- coding=utf-8 -*-
import numpy  as np
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection  import train_test_split
import matplotlib.pyplot as plt 
def creat_data(n):
    '''
    随机产生数据集
    '''
    np.random.seed(0)
    X=5*np.random.rand(n,1)#n*1矩阵
    #print(X)
    y=np.sin(X).ravel()
    #print(y)
    noise_num=(int)(n/5)
    #print(noise_num)
    y[::5]+=3*(0.5-np.random.rand(noise_num))
    return train_test_split(X,y,test_size=0.25,random_state=1)

def  test_DecisionTreeRegressor(*data):
    '''
    #在Python里,带*的参数就是用来接受可变数量参数的。
    '''
    X_train,X_test,y_train,y_test=data
    regr=DecisionTreeRegressor()
    regr.fit(X_train,y_train)
    print("the train score :%f"%(regr.score(X_train,y_train)))
    print("the test score:%f "%regr.score(X_test,y_test))
    #绘图
    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)
    X=np.arange(0.0,5.0,0.01)[:,np.newaxis]#增加新维度意思,现在是2维列向量
    Y=regr.predict(X)
    ax.scatter(X_train,y_train,label="train Sample",c='r')
    ax.scatter(X_test,y_test,label="test Sample",c='b')#对应二维坐标点
    ax.plot(X,Y,label=&
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值