Drag to Rotate View
Mean Squared Error
151.6

Thinking in 3D

Real life is complex. One variable is rarely enough. House Price depends on Size, Location, Age, AND Number of Rooms. This is Multiple Linear Regression.
Continue

1. The 3D Scatter Plot

Imagine we have data with 2 features: • X-axis: Size • Z-axis: Rooms • Y-axis (Height): Price Each data point floats in 3D space.
Continue

2. Fitting a Plane (Not a Line)

With 2 inputs, we don't fit a line. We fit a Plane (flat sheet). We adjust the 'tilt' of the sheet to slice through the middle of the points.
Continue

3. The Formula

Line: `y = wx + b` Plane: `y = w1*x1 + w2*x2 + b` Each feature gets its own Weight (Importance). `w1` tilts the X-axis. `w2` tilts the Z-axis.
Continue

Interactive 3D Fit

Drag the scene to rotate. Adjust the sliders to tilt the plane. Try to minimize the Mean Squared Error (MSE)!
Continue

Coding Multiple Regression

It's exactly the same code as Simple Regression! The matrix math (`X * w`) handles any number of columns automatically.

multivariate.py
from sklearn.linear_model import LinearRegression

# 1. Data with 2 Features (Size, #Rooms)
X = [
    [1500, 3],  # House 1
    [2000, 4],  # House 2
    [800,  1]   # House 3
]

y = [300000, 500000, 150000] # Prices

# 2. Train Model
model = LinearRegression()
model.fit(X, y)

# 3. Predict new house
# Size: 1200, Rooms: 2
prediction = model.predict([[1200, 2]])

print(f"Coefficients: {model.coef_}")
# Output: [150.5, 4000.2] (Weight per sqft, Weight per room)
Using Scikit-Learn
AlgoAnimator: Interactive Data Structures