美文网首页
回归决策树和线性回归对比

回归决策树和线性回归对比

作者: 涓涓自然卷 | 来源:发表于2021-03-12 17:41 被阅读0次

1、代码例子:

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

# 生成数据
x = np.array(list(range(1, 11))).reshape(-1, 1)
y = np.array([5.56, 5.70, 5.91, 6.40, 6.80, 7.05, 8.90, 8.70, 9.00, 9.05])

# 训练模型
model1 = DecisionTreeRegressor(max_depth=1)
model2 = DecisionTreeRegressor(max_depth=3)
model3 = linear_model.LinearRegression()

model1.fit(x, y)
model2.fit(x, y)
model3.fit(x, y)

# 模型预测
X_test = np.arange(0.0, 10.0, 0.01).reshape(-1, 1)  # 生成1000个数,用于训练模型
X_test.shape

y_1 = model1.predict((X_test))
y_2 = model2.predict((X_test))
y_3 = model3.predict((X_test))

# 结果可视化
plt.figure(figsize=(10, 6), dpi=100)
plt.scatter(x, y, label="data")
plt.plot(X_test, y_1, label="max_depth=1")
plt.plot(X_test, y_2, label="max_depth=3")
plt.plot(X_test, y_3, label="liner regression")

plt.xlabel("data")
plt.ylabel("target")
plt.title("Decision Tree Regression")
plt.legend()

plt.show()

2、运行结果对比:

运行结果.png

相关文章

网友评论

      本文标题:回归决策树和线性回归对比

      本文链接:https://www.haomeiwen.com/subject/cproqltx.html