回归决策树模拟sin函数

python 复制代码
# -*-coding:utf-8-*-
import numpy as np
from sklearn import tree
import matplotlib.pyplot as plt

plt.switch_backend("TkAgg")
# 创建了一个随机数生成器对象 rng
rng=np.random.RandomState(1)
print("rng",rng)
#5*rng.rand(80,1)生成一个80行、1列的数组,数组中的每个元素都是从0到5之间的随机数。然后,np.sort函数对这个数组进行排序,axis=0表示按行(也就是每一列)排序。
#axis=0,数组只有行,没有列
X=np.sort(5*rng.rand(80,1),axis=0)

#ravel()把二维数组变为一位数组
y=np.sin(X).ravel()



#选取0,5,10,15,20....,让这些下标数字加上噪声
y[::5]+=3*(0.5-rng.rand(16))

regr_1=tree.DecisionTreeRegressor(max_depth=2)
regr_2=tree.DecisionTreeRegressor(max_depth=5)
clf1=regr_1.fit(X,y)
clf2=regr_2.fit(X,y)

#转为二维数组
X_test=np.reshape( np.arange(0.0,5.0,0.01),(-1,1) )
# X_test=np.arrange(0.0,5.0,0.01)[:,np.newaxis]

y_1=regr_1.predict(X_test)
y_2=regr_2.predict(X_test)


plt.figure()
plt.scatter(X,y,s=20,edgecolors="black",c="darkorange",label="data")
plt.plot(X_test,y_1,color="cornflowerblue",label="max_depth=2",linewidth=2)
plt.plot(X_test,y_2,color="yellowgreen",label="max_depth=5",linewidth=2)
plt.xlabel("data")
plt.ylabel("target")
plt.title("Decision Tree Regreesion")
plt.legend()
plt.show()
相关推荐
学术中心-龙老师3 小时前
2027年仿真研究与数据分析国际会议(ICSRDA 2027)
数据挖掘·数据分析·全文检索·ei检索·论文发表
钉先生贾2 天前
HarmonyOS 7.0性能与稳定性排障实战 05:真机预检、冷启动埋点与回归验证
华为·回归·harmonyos
田里的水稻2 天前
FA_编程算法200个分类列数-SUM
算法·分类·数据挖掘
简单风2 天前
视频模型的deepseek时刻来了
人工智能·回归·音视频
geovindu2 天前
python: Decision Tree
开发语言·后端·python·决策树
@陈小鱼4 天前
基于InceptionTime网络的PPG无创血压估计算法研究
人工智能·python·算法·回归·开源·ppg·血压估计
Brain 博览记4 天前
脑机接口研究丨FPGA 5 毫秒完成运动想象分类
开发语言·论文阅读·人工智能·机器学习·fpga开发·分类·数据挖掘
桐果云5 天前
医院数据分析怎么做?一图分清就诊统计、工时、设备预警
数据挖掘·数据分析·医院数据分析·医疗数据中台·零代码数据建模·就诊人数统计·科室工时统计
geovindu7 天前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
意念回复7 天前
Transformer 系列(五):从 Block 到模型——三大架构、因果掩码与自回归生成
android·回归·transformer