from sklearn.ensemble import GradientBoostingRegressor from sklearn.model_selection import train_test_split from sklearn.datasets import make_moons from sklearn.metrics import accuracy_score import matplotlib.pyplot as plt import numpy as np def plot_predictions(regressors, X, y, axes, label=None, style="r-", data_style="b.", data_label=None): x1 = np.linspace(axes[0], axes[1], 500) y_pred = sum(regressor.predict(x1.reshape(-1, 1)) for regressor in regressors) plt.plot(X[:, 0], y, data_style, label=data_label) plt.plot(x1, y_pred, style, linewidth=2, label=label) if label or data_label: plt.legend(loc="upper center", fontsize=16) plt.axis(axes) X, y = make_moons(n_samples=500, noise=0.30, random_state=42) X_train, X_val, y_train, y_val = train_test_split(X, y, random_state=42) gbrt = GradientBoostingRegressor(max_depth=2, n_estimators=120, random_state=42) gbrt.fit(X_train, y_train) from sklearn.metrics import mean_squared_error errors = [mean_squared_error(y_val, y_pred) for y_pred in gbrt.staged_predict(X_val)] bst_n_estimators = np.argmin(errors) gbrt_best = GradientBoostingRegressor(max_depth=2, n_estimators=bst_n_estimators, random_state=42) gbrt_best.fit(X_train, y_train) min_error = np.min(errors) print(min_error) plt.figure(figsize=(11, 4)) plt.subplot(121) plt.plot(errors, 'b.-') plt.plot([bst_n_estimators, bst_n_estimators], [0, min_error], 'k--') plt.plot([0, 120], [min_error, min_error], 'k--') plt.axis([0, 120, 0, 0.01]) plt.title('Val Error') plt.subplot(122) plot_predictions([gbrt_best], X, y, axes=[-0.5, 0.5, -0.1, 0.8]) plt.title('Best Model(%d trees)' %bst_n_estimators) plt.show()
到底是哪里输入X时,出错了呀?