Ordinary least squares (OLS) estimates a linear outcome from one or more predictors by choosing coefficients that minimize squared prediction errors. Its fitted equation combines predictor values with coefficients and an intercept to produce estimated outcomes. section 9.1.2
Evaluate an OLS model through three complementary outputs:
- Coefficients indicate the contribution of individual predictors. section 9.1.2
- Evaluation metrics summarize overall predictive accuracy. section 9.1.2
- Residuals show discrepancies between observed and predicted values and can be plotted against predictions or summarized as a distribution. section 9.1.2
Housing-price estimation
Use square footage and number of bedrooms as predictors of house price, add an intercept, fit OLS with statsmodels, and inspect the resulting summary. New predictor values can then generate an estimated price. section 9.1.2
import statsmodels.api as sm
import pandas as pd
housing_prices = pd.read_csv("housing_prices.csv") #1
X = housing_prices[["sq_footage", "n_bedrooms"]]
y = housing_prices["price"] #2
X = sm.add_constant(X) #3
model = sm.OLS(y, X).fit() #4
print(model.summary())
A compact Python workflow loads housing data, separates predictors and outcome, adds a constant, fits OLS, and prints diagnostic output. section 9.1.2



