机器学习中决策树不同树深度对拟合效果的影响demo

code:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor

if __name__ =='__main__':
	N = 100
	# 获取100个在[-3, 3)之间的数据
	x = np.random.rand(N) * 6 - 3
	x.sort()
	# y值加入了一点随机噪声
	y = np.sin(x) + np.random.randn(N) * 0.05
	print("y:\n", y)
	x = x.reshape(-1, 1)
	print("x:\n", x)
	# 使用决策树进行训练
	reg = DecisionTreeRegressor(criterion='mse', max_depth=9)
	dt = reg.fit(x, y)
	x_test = np.linspace(-3, 3, 50).reshape(-1, 1)
	# 预测值
	y_hat = dt.predict(x_test)
	plt.plot(x, y, 'r*', ms=10, label='Actual')
	plt.plot(x_test, y_hat, 'g-', lw=2, label='Predict')
	plt.legend(loc='upper left')
	plt.grid()
	plt.show()

	depth = [2, 4, 6, 8, 10, 12]
	clr = 'rgbmyc'
	reg=[
		DecisionTreeRegressor(criterion='mse', max_depth=depth[0]),
		DecisionTreeRegressor(criterion='mse', max_depth=depth[1]),
		DecisionTreeRegressor(criterion='mse', max_depth=depth[2]),
		DecisionTreeRegressor(criterion='mse', max_depth=depth[3]),
		DecisionTreeRegressor(criterion='mse', max_depth=depth[4]),
		DecisionTreeRegressor(criterion='mse', max_depth=depth[5]),
	]

	plt.plot(x, y, 'k^', lw=2, label='Actual')
	x_test = np.linspace(-5, 5, 100).reshape(-1, 1)
	for i, r in enumerate(reg):
		dt = r.fit(x, y)
		y_hat = dt.predict(x_test)
		plt.plot(x_test, y_hat, '-', color=clr[i], lw=2, label='Depth=%d' % depth[i])
	plt.legend(loc='upper left')
	plt.grid()
	plt.show()

树结构越深对数据的拟合越好,可也会可能导致过拟合。
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值