import numpy as np from sklearn.linear_model import SGDRegressor from sklearn.preprocessing import StandardScaler import matplotlib.pyplot as plt # Original data X = np.array([ [1100], [1400], [1425], [1550], [1600], [1700], [1700], [1875], [2350], [2450] ]) y = np.array([ 199000, 245000, 319000, 240000, 312000, 279000, 310000, 308000, 405000, 324000 ]) # Scale input and output x_scaler = StandardScaler() y_scaler = StandardScaler() X_scaled = x_scaler.fit_transform(X) y_scaled = y_scaler.fit_transform(y.reshape(-1, 1)).ravel() # Use SGDRegressor model = SGDRegressor(max_iter=150, learning_rate='constant', eta0=0.01) model.fit(X_scaled, y_scaled) # Predict and convert back to original scale X_test = np.linspace(min(X_scaled), max(X_scaled), 100).reshape(-1, 1) y_pred_scaled = model.predict(X_test) y_pred = y_scaler.inverse_transform(y_pred_scaled.reshape(-1, 1)) # Plot plt.plot(X, y, 'k^', label='Original Data') X_orig = x_scaler.inverse_transform(X_test) plt.plot(X_orig, y_pred, 'r-', label='SGDRegressor Line') plt.xlabel("Square Footage") plt.ylabel("Price") plt.title("SGD Regressor (scikit-learn)") plt.legend() plt.show()