回归决策树模拟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()
相关推荐
救救孩子把1 小时前
Dogs vs. Cats:从零到一的图像分类数据集
人工智能·分类·数据挖掘
databook2 小时前
拒绝“凭感觉”:用回归分析看透数据背后的秘密
python·数据挖掘·数据分析
Christo33 小时前
2024《Three-way clustering: Foundations, survey and challenges》
人工智能·算法·机器学习·数据挖掘
2501_941329723 小时前
基于DETR的血细胞显微图像检测与分类方法研究【原创】_1
人工智能·数据挖掘
Christo35 小时前
2022-《Deep Clustering: A Comprehensive Survey》
人工智能·算法·机器学习·数据挖掘
啊巴矲7 小时前
小白从零开始勇闯人工智能:机器学习初级篇(决策树)
人工智能·决策树·机器学习
LDG_AGI8 小时前
【推荐系统】深度学习训练框架(十七):TorchRec之KeyedJaggedTensor
人工智能·pytorch·深度学习·机器学习·数据挖掘·embedding
Christo39 小时前
2024《A Rapid Review of Clustering Algorithms》
人工智能·算法·机器学习·数据挖掘
十三画者9 小时前
【文献分享】vConTACT3机器学习能够实现可扩展且系统的病毒分类体系的构建
人工智能·算法·机器学习·数据挖掘·数据分析
wfeqhfxz25887829 小时前
基于YOLOv10n的热带海洋蝴蝶鱼物种识别与分类系统_P3456数据集训练_1
yolo·分类·数据挖掘