python cart算法的简单实现

来源:互联网 发布:mac风扇一直响 编辑:程序博客网 时间:2024/05/29 15:36

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

import matplotlib.pyplot as pltimport numpy as npfrom sklearn.tree import DecisionTreeRegressordef 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).Ty = siny+np.random.rand(1,len(siny))*1.5y= 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))


原创粉丝点击