@@ -0,0 +1,1634 @@
+import numpy as np%0Aimport matplotlib.pyplot as plt%0Afrom sklearn.pipeline import Pipeline%0Afrom sklearn.preprocessing import PolynomialFeatures%0Afrom sklearn.linear_model import LinearRegression%0Afrom sklearn.cross_validation import cross_val_score%0A%0A%0Adef main():%0A%09np.random.seed(0)%0A%09%0A%09n_samples = 30%0A%09degrees = %5B1, 4, 15%5D%0A%0A%09true_fun = lambda X: np.cos(1.5 * np.pi * X)%0A%09X = np.sort(np.random.rand(n_samples))%0A%09y = true_fun(X) + np.random.randn(n_samples) * 0.1%0A%0A%09plt.figure(figsize=(14, 5))%0A%09for i in range(len(degrees)):%0A%09    ax = plt.subplot(1, len(degrees), i + 1)%0A%09    plt.setp(ax, xticks=(), yticks=())%0A%0A%09    polynomial_features = PolynomialFeatures(degree=degrees%5Bi%5D,%0A%09                                             include_bias=False)%0A%09    linear_regression = LinearRegression()%0A%09    pipeline = Pipeline(%5B(%22polynomial_features%22, polynomial_features),%0A%09                         (%22linear_regression%22, linear_regression)%5D)%0A%09    pipeline.fit(X%5B:, np.newaxis%5D, y)%0A%0A%09    # Evaluate the models using crossvalidation%0A%09    scores = cross_val_score(pipeline, X%5B:, np.newaxis%5D, y,%0A%09                             scoring=%22mean_squared_error%22, cv=10)%0A%0A%09    X_test = np.linspace(0, 1, 100)%0A%09    plt.plot(X_test, pipeline.predict(X_test%5B:, np.newaxis%5D), label=%22Model%22)%0A%09    plt.plot(X_test, true_fun(X_test), label=%22True function%22)%0A%09    plt.scatter(X, y, label=%22Samples%22)%0A%09    plt.xlabel(%22x%22)%0A%09    plt.ylabel(%22y%22)%0A%09    plt.xlim((0, 1))%0A%09    plt.ylim((-2, 2))%0A%09    plt.legend(loc=%22best%22)%0A%09    plt.title(%22Degree %7B%7D%5CnMSE = %7B:.2e%7D(+/- %7B:.2e%7D)%22.format(%0A%09        degrees%5Bi%5D, -scores.mean(), scores.std()))%0A%09plt.show()%0A%0A%0Aif __name__ == '__main__':%0A%09main()%0A

Version of sklearn plot_underfitting_overfitting.html with import corrections