Linear regression models the relationship between an input feature x and a continuous numeric target y by fitting the best straight line: y = m * x + b. Here m is the slope (how much y changes per unit of x) and b is the intercept.
It is one of the fastest, most interpretable baseline models in Machine Learning for tasks like estimating delivery times from distance or sales from ad spend.
Python 3.10+ includes statistics.linear_regression(x, y) right in the standard library, and scikit-learn (LinearRegression) provides the full ML .fit() / .predict() API for larger workflows.
import statistics
ad_spend_k = [2, 4, 6, 8, 10]
revenue_k = [15, 24, 31, 42, 49]
model = statistics.linear_regression(ad_spend_k, revenue_k)
predicted_for_12 = model.slope * 12 + model.intercept
r_value = statistics.correlation(ad_spend_k, revenue_k)
print(f"Slope={model.slope:.2f}, Intercept={model.intercept:.2f}")
print(f"Predicted at 12k: {predicted_for_12:.2f}k | R^2={r_value**2:.3f}")
R² (Coefficient of Determination): Closer to 1.0 means the line explains most variance in y; near 0.0 means a straight line fits poorly.actual - predicted errors across x to confirm no curved pattern remains.5–15 predicts nonsense for age 90.R² shows two metrics move together, not that x causes y.OLS chooses the slope and intercept that minimize the sum of squared vertical distances (residuals) between actual points and the predicted line.
Instantiate LinearRegression(), reshape X into a 2D array X.reshape(-1, 1), call model.fit(X_train, y_train), and call model.predict(X_test).
Extreme outliers inflate squared errors heavily; inspect scatter plots first or use HuberRegressor for outlier-resistant fitting.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.