sklearn中决策树回归器DecisionTreeRegressor的实际应用及可视化

假设有CSV文件(部分):suzhou.csv
在这里插入图片描述
要对其进行回归分析并输出图像:

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

data = np.genfromtxt("/suzhou.csv",delimiter=",",dtype=int,skip_header=1,usecols=np.arange(0,6))
data = data.transpose()

X = np.array(data[0]).reshape(len(data[0]),1)
y = np.array(data[4]).reshape(len(data[4]),1)

regr_1 = DecisionTreeRegressor(max_depth=2, presort=False)
regr_2 = DecisionTreeRegressor(max_depth=5, presort=False)
regr_1.fit(X, y)
regr_2.fit(X, y)

X_test = np.arange(2007, 2018, 0.9)[:, np.newaxis].astype(int)
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)

x_axis = range(2007,2018,2)

plt.figure()
plt.scatter(X, y, s=20, edgecolor="black", c="darkorange", label="data")
plt.plot(X_test, y_1, color="cornflowerblue", label="Decision Tree Depth=2", linewidth=2)
plt.plot(X_test, y_2, color="yellowgreen", label="Decision Tree Depth=5", linewidth=2)
plt.xticks(x_axis)
plt.xlabel("Year")
plt.ylabel("Gross Industrial Output")
plt.title("Total Industrial Output Value of Enterprises in Suzhou")
plt.legend()
plt.show()

输出图像:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白水baishui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值