# Fit regression model
#regr_1 = DecisionTree(task='regression', criterion='mse', max_depth=1)
dt = DecisionTree(task='regression', criterion='mse', max_depth=15, min_samples_leaf=3,
                      verbose=True)
rf_params = {'n_estimators': 100, 'max_depth': 15, 'min_samples_leaf': 5}
rf = RandomForest(task='regression', **rf_params)
skrf = skRFR(**rf_params)
dt.fit(X, y)
rf.fit(X, y)
skrf.fit(X, y)

# Predict
X_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis]
y_dt = dt.predict(X_test)
y_rf = rf.predict(X_test)
y_skrf = skrf.predict(X_test)

# Plot the results
plt.figure(1, (15, 10))
plt.scatter(X, y, s=20, edgecolor="black",
            c="darkorange", label="data")
plt.plot(X_test, y_dt, color="cornflowerblue",
         label="DecisionTree", linewidth=2)
plt.plot(X_test, y_rf, color="r", label="RandomForest", linewidth=2)
plt.plot(X_test, y_skrf, color="yellowgreen", label="sklearn RandomForest", linewidth=2)

plt.xlabel("data")
plt.ylabel("target")
plt.title("Decision Tree Regression VS RandomForest")
Ejemplo n.º 2
0
regr_2 = DecisionTree(task='regression',
                      criterion='mse',
                      max_depth=15,
                      min_samples_leaf=1)
regr_xgb = XGBoostRegressor(n_estimators=50,
                            max_depth=1,
                            gamma=0.005,
                            lambd=1.0,
                            tree_method='hist')
regr_1.fit(X, y)
regr_2.fit(X, y)
regr_xgb.fit(X, y)

# Predict
X_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis]
dfX_test = pd.DataFrame(X_test, columns=['X'])
y_1 = np.array(regr_1.predict(dfX_test))
y_2 = np.array(regr_2.predict(dfX_test))
y_xgb = np.array(regr_xgb.predict(dfX_test))

# Plot the results
plt.figure(1, (15, 10))
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=15", linewidth=2)
plt.plot(X_test, y_xgb, color="orange", label="xgboost", linewidth=2)
plt.xlabel("data")
plt.ylabel("target")
plt.title("Decision Tree Regression")
plt.legend()
plt.show()
Ejemplo n.º 3
0
                                  max_depth=5,
                                  min_samples_leaf=2)
    skdt.fit(X, y)
    print(skdt.feature_importances_)

    plt.subplot(2, 3, pairidx + 1)
    plt.xlabel(iris.feature_names[pair[0]])
    plt.ylabel(iris.feature_names[pair[1]])

    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
                         np.arange(y_min, y_max, plot_step))
    plt.tight_layout(h_pad=0.5, w_pad=0.5, pad=2.5)

    Z = dt.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    cs = plt.contourf(xx, yy, Z, cmap=plt.cm.RdYlBu)

    plt.xlabel(iris.feature_names[pair[0]])
    plt.ylabel(iris.feature_names[pair[1]])

    # Plot the training points
    for i, color in zip(range(n_classes), plot_colors):
        idx = np.where(y == i)
        plt.scatter(X[idx, 0],
                    X[idx, 1],
                    c=color,
                    label=iris.target_names[i],
                    cmap=plt.cm.RdYlBu,
                    edgecolor='black',