import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures # Generate random data np.random.seed(0) X = np.linspace(-3, 3, 100) y = 0.5 * X**2 + X + np.random.normal(0, 1, 100) # Reshape the input data X = X.reshape(-1, 1) # Plot the original data plt.scatter(X, y, color='b', label='Data') # Fit polynomial regression models of different degrees degrees = [1, 4, 15] colors = ['r', 'g', 'm'] for degree, color in zip(degrees, colors): # Create polynomial features polynomial_features = PolynomialFeatures(degree=degree) X_poly = polynomial_features.fit_transform(X) # Fit the polynomial regression model model = LinearRegression() model.fit(X_poly, y) # Predict the values y_pred = model.predict(X_poly) # Plot the fitted curve plt.plot(X, y_pred, color=color, linewidth=2, label=f'Degree {degree}') # Add labels and title to the plot plt.xlabel('X') plt.ylabel('y') plt.title('Polynomial Regression - Overfitting Example') plt.legend(loc='upper left') # Display the plot plt.show()