python cart算法的简单实现

下面是python cart算法的简单实现,可以直接复制下面代码进行运行,即可查看模型的拟合曲线

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

def plotfigure(X,X_test,y,yp):
    plt.figure()
    plt.scatter(X,y,c="k",label="data")                #scatter must be 1D (cannot above 2D, for example (200,1))
    plt.plot(X_test,yp,c="r",label="max_depth=5",linewidth=2)
    plt.xlabel("data")
    plt.ylabel("target")
    plt.title("Decision Tree Regression")
    plt.legend()
    plt.show()
x = np.linspace(-5,5,200)
siny = np.sin(x)
X = np.mat(x).T
y = siny+np.random.rand(1,len(siny))*1.5
y= y.tolist()[0]
clf = DecisionTreeRegressor(max_depth=5,min_samples_leaf=10,min_samples_split=10)
clf.fit(X,y)
X_test = np.arange(-5.0,5.0,0.05)[:,np.newaxis]
yp = clf.predict(X_test)
plotfigure(np.array(X)[:,0],X_test,y,yp)
print(X.shape,type(X))


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值