如何可视化决策树回归器中数据的各种max_depth参数的效果

时间:2020-07-17 02:26:29

标签: regression cross-validation decision-tree

我需要确定DecisionTreeRegressor的最佳max_depth,然后尝试可视化数据以显示最佳max_depth如何适合我的数据,如下图所示。我用了这段代码

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

# Regression
regr_1 = DecisionTreeRegressor(max_depth=5)
regr_2 = DecisionTreeRegressor(max_depth=10)
regr_1.fit(X_train, y_train)
regr_2.fit(X_train, y_train)
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)

# Plot the results
plt.figure()
plt.scatter(X_train, y_train, 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() 

enter image description here

但是我发现了这个问题ValueError: x and y must be the same size!?

0 个答案:

没有答案