决策树Python 实现——决策树回归

本文通过scikit-learn库展示了如何实现决策树回归,详细解释了代码,适用于初学者。内容涉及1D回归,讨论了决策树如何拟合正数曲线及处理噪声数据,强调了过度拟合问题,特别是当最大深度参数过大时。
摘要由CSDN通过智能技术生成

原文以及代码来自于,本人对每段代码进行了详细注释,希望对初学者有用。
https://scikit-learn.org/stable/auto_examples/tree/plot_tree_regression.html
决策树回归 Decision Tree Regression
带有决策树的 1D 回归。
决策树用于拟合正数曲线和加噪声观测。因此,它学习接近主数曲线的局部线性回归。
我们可以看到,如果树的最大深度(由最大深度参数控制)设置得过高,则决策树会学习训练数据的细节,并从噪声中学习,即它们过度拟合。
在这里插入图片描述

print(__doc__)

# Import the necessary modules and libraries
import numpy as np
from sklearn.tree import DecisionTreeRegressor
import matplotlib.pyplot as plt

# Create a random dataset
rng = np.random.RandomState(1)
X = np.sort(5 * rng.rand(80, 1), axis=0)
y = np.sin(X).ravel()
y[::5] += 3 * (0.5 - rng.rand(16))
'''
numpy.random.RandomState()是一个伪随机数生成器。那么伪随机数是什么呢? ()括号内是seed,确保不同电脑上产生相同的伪随机数
伪随机数是用确定性的算法计算出来的似来自[0,1]均匀分布的随机数序列。并不真正的随机,但具有类似于随机数的统计特征,如均匀性、独立性等。
运行一下下面两个
rng = np.random.RandomState(1)
x = rng.rand(4)
y = rng.rand(4)
rng = np.random.RandomState(1)
x = rng.rand(4)
rng = np.random.RandomState(1)
y = rng.rand(4)

.sort axis =0 每列从小到大排列
import numpy as np
x=np.array([[0,12,48],[4,18,14],[7,1,99]])
np.sort(x)
Out[61]: 
array([[ 0, 12, 48],
       [ 4, 14, 18],
       [ 1,  7, 99]])
np.sort(x, axis=0)
Out[62]: 
array([[ 0,  1, 14],
       [ 4, 12, 48],
       [ 7, 18, 99]])
np.sort(x, axis=1)
Out[63]: 
array([[ 0, 12, 48],
       [ 4, 14, 18],
       [ 1,  7, 99]]) 
       
.ravel() 将多维数组降位一维

y[::5] 从开始到结束,每隔5个数,第0个开始取出算起,更加详细的取数组元素方式可以参看下面链接
https://blog.csdn.net/sinat_34474705/article/details/74458605
'''

# Fit regression model
regr_1 = DecisionTreeRegressor(max_depth=2)
regr_2 = DecisionTreeRegressor(max_depth=5)
regr_1.fit(X, y)
regr_2.fit(X, y)

# Predict
X_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis]
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)
'''
[:, np.newaxis], 将一行数据转换为一列数据,每行是一个一维输入,每列是一个feature,这个例子只有一个feature
'''


# Plot the results
plt.figure()
plt.scatter(X, y, s=20, edgecolor="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 Regression")
plt.legend()
plt.show()
决策树模型的Python实现可以通过使用scikit-learn库中的DecisionTreeClassifier和DecisionTreeRegressor来实现。 通过调用DecisionTreeClassifier可以实现分类决策树模型,而调用DecisionTreeRegressor可以实现回归决策树模型。在实际应用中,可以使用这两个模型来解决不同类型的问题。例如,可以使用DecisionTreeClassifier来预测员工是否离职,或者使用DecisionTreeRegressor来预测某个数值型属性的值。对于Python实现中的参数调优,可以使用K折交叉验证和GridSearch网格搜索来进行模型性能的评估和参数的选择。K折交叉验证可以帮助我们评估模型的泛化能力,而GridSearch网格搜索可以帮助我们找到最佳的参数组合。通过这些方法,我们可以得到一个性能良好的决策树模型,并且可以使用ROC曲线来评估分类模型的性能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [决策树算法python代码实现](https://download.csdn.net/download/u010919410/10452196)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [机器学习——决策树模型:Python实现](https://blog.csdn.net/qq_34357269/article/details/109579188)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值