Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Master Simple Linear Regression, Multiple Regression, and Polynomial Regression to forecast continuous outcomes.
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Optimal weight vector | Best-fit slopes and intercept | |
| Design matrix | Table of input features | |
| Target vector | Actual continuous values |
Simple 1D line through points (1, 2) and (3, 6).
Result: Model: y = 2.0 * x (Residuals = 0, R^2 = 1.0)
Plot target against candidate predictors to assess linear correlation.
Compute optimal coefficients using normal equation or iterative optimizer.
Verify residuals are normally distributed with constant variance (homoscedasticity).
Problem: Estimating market price of houses based on square meters, bedrooms, and transit score.
Solution: Multiple regression outputs estimated dollar valuation with confidence intervals.
Problem: Predicting megawatt electrical load based on temperature forecasts.
Solution: Polynomial regression captures non-linear AC usage spikes during summer heatwaves.
Interactive activity component
Fit an Ordinary Least Squares (OLS) linear regression model on house sizes to predict continuous prices, and visualize the regression line with Matplotlib.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install numpy scikit-learn matplotlib1 import numpy as np 2 import matplotlib.pyplot as plt 3 from sklearn.linear_model import LinearRegression
import numpy as np → NumPy for matrix operations
import matplotlib.pyplot as plt → Matplotlib for plotting trend line
from sklearn.linear_model import LinearRegression → Scikit-Learn OLS regressor
1 # Data: House sizes (sq ft) and corresponding prices ($) 2 X = np.array([500, 700, 900, 1100, 1300]).reshape(-1, 1) # Feature (sq ft) 3 y = np.array([150000, 180000, 210000, 250000, 280000]) # Target (price) 4 5 # Create Linear Regression model 6 model = LinearRegression() 7 8 # Train the model on the data 9 model.fit(X, y) 10 11 # Predict price for a house with 1000 sq ft 12 predicted_price = model.predict([[1000]]) 13 print(f"Predicted Price for 1000 sq ft house: ${predicted_price[0]:,.2f}") 14 15 # Plot Data and Regression Line 16 plt.scatter(X, y, color='blue', label='Actual Prices') 17 plt.plot(X, model.predict(X), color='red', label='Regression Line') 18 plt.xlabel("House Size (sq ft)") 19 plt.ylabel("House Price ($)") 20 plt.title("Linear Regression - House Price Prediction") 21 plt.legend() 22 plt.show()
# Data: House sizes (sq ft) and corresponding prices ($)
Data definition
X = np.array([500, 700, 900, 1100, 1300]).reshape(-1, 1) # Feature (sq ft)
Reshape into column vector
y = np.array([150000, 180000, 210000, 250000, 280000]) # Target (price)
Target house prices
# Create Linear Regression model
Instantiate model
model = LinearRegression()
Create OLS instance
# Train the model on the data
Fitting
model.fit(X, y)
Fit best-fit line
# Predict price for a house with 1000 sq ft
Inference
predicted_price = model.predict([[1000]])
Evaluate prediction at 1000 sqft
print(f"Predicted Price for 1000 sq ft house: ${predicted_price[0]:,.2f}")
Print formatted predicted price
# Plot Data and Regression Line
Visualization
plt.scatter(X, y, color='blue', label='Actual Prices')
Plot data points
plt.plot(X, model.predict(X), color='red', label='Regression Line')
Plot best-fit line
plt.xlabel("House Size (sq ft)")
X-axis label
plt.ylabel("House Price ($)")
Y-axis label
plt.title("Linear Regression - House Price Prediction")
Plot title
plt.legend()
Add legend
plt.show()
Display plot
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Linear Regression: House Price Prediction & Trend Visualization 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install numpy scikit-learn matplotlib 8 9 # Step 2: Import libraries 10 import numpy as np 11 import matplotlib.pyplot as plt 12 from sklearn.linear_model import LinearRegression 13 14 # Step 3: Run Demonstration 15 # Data: House sizes (sq ft) and corresponding prices ($) 16 X = np.array([500, 700, 900, 1100, 1300]).reshape(-1, 1) # Feature (sq ft) 17 y = np.array([150000, 180000, 210000, 250000, 280000]) # Target (price) 18 19 # Create Linear Regression model 20 model = LinearRegression() 21 22 # Train the model on the data 23 model.fit(X, y) 24 25 # Predict price for a house with 1000 sq ft 26 predicted_price = model.predict([[1000]]) 27 print(f"Predicted Price for 1000 sq ft house: ${predicted_price[0]:,.2f}") 28 29 # Plot Data and Regression Line 30 plt.scatter(X, y, color='blue', label='Actual Prices') 31 plt.plot(X, model.predict(X), color='red', label='Regression Line') 32 plt.xlabel("House Size (sq ft)") 33 plt.ylabel("House Price ($)") 34 plt.title("Linear Regression - House Price Prediction") 35 plt.legend() 36 plt.show()
Encodes 5 house sizes [500-1300 sq ft] and prices [$150k-$280k].
Finds optimal slope m and intercept b minimizing squared residuals.
Forecasts 1000 sq ft house at $230,000 and plots data points alongside the regression trend line.
House sizes: [500, 700, 900, 1100, 1300] sq ft
Predicted Price for 1000 sq ft house: $230,000.00
The linear regression model accurately captures the ~$162.50 per square foot trend line, providing reliable continuous price estimations.
Data, prediction, linear regression, and the bridge to modern artificial intelligence.
Test your understanding with these questions. Remember, this is for learning, not examination!