回归决策树模拟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()
相关推荐
捷焱GEO2 天前
用页面状态和回归样本排查 AI 错用旧预约公告
前端·软件测试·人工智能·数据挖掘·回归
曦樂~2 天前
【大数据挖掘】06 分类Classification
人工智能·分类·数据挖掘
linweidong3 天前
Headless BI 实战:指标语义层从设计到落地的完整过程
大数据·数据挖掘·flink·数据湖·finebi·bi·chat bi
欧雷殿3 天前
没有数据勿谈智能:揭秘我的数据底座
数据挖掘·github·ai编程
学术中心-龙老师3 天前
2027年仿真研究与数据分析国际会议(ICSRDA 2027)
数据挖掘·数据分析·全文检索·ei检索·论文发表
钉先生贾5 天前
HarmonyOS 7.0性能与稳定性排障实战 05:真机预检、冷启动埋点与回归验证
华为·回归·harmonyos
田里的水稻5 天前
FA_编程算法200个分类列数-SUM
算法·分类·数据挖掘
简单风5 天前
视频模型的deepseek时刻来了
人工智能·回归·音视频
geovindu5 天前
python: Decision Tree
开发语言·后端·python·决策树