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))


你可能感兴趣的:(Python,python)