Data Science Multiple Regression

Multiple regression predicts a numeric outcome from two or more input variables at once, and scikit-learn's LinearRegression fits the best-fitting hyperplane through all of them together.

One Outcome, Several Inputs

Multiple regression is the natural extension of linear regression: instead of y = slope * x + intercept, the model becomes y = intercept + coef1 * x1 + coef2 * x2 + ... Each coefficient shows the effect of that one input on the outcome while holding every other input fixed.

Fitting a Multiple Regression Model

scikit-learn's LinearRegression expects a 2D array X with one row per observation and one column per input feature, plus a 1D array y of the known outcomes. Calling .fit(X, y) solves for the coefficients and intercept in one step.

Example

import numpy as np
from sklearn.linear_model import LinearRegression

# two input features: engine size (liters) and weight (kg)
X = np.array([
    [1.0, 1100],
    [1.6, 1300],
    [2.0, 1400],
    [2.5, 1500],
    [3.0, 1700],
    [3.5, 1900],
])
co2 = np.array([205, 228, 240, 252.5, 275, 297.5])

model = LinearRegression()
model.fit(X, co2)

print("coefficients:", model.coef_)      # about [5.  0.1]
print("intercept:", model.intercept_)    # about 90.0

Read the two coefficients side by side with what they're attached to: about +5 g/km of CO2 per extra liter of engine size, and about +0.1 g/km per extra kilogram of weight, with both effects measured holding the other input constant.

Making Predictions With Multiple Inputs

Example

# predict CO2 emissions for a new car: 2.2-liter engine, 1450 kg
predicted = model.predict([[2.2, 1450]])
print(predicted)    # about [246.]
Note: Engine size is measured in single-digit liters while weight is measured in hundreds of kilograms. Because the two features sit on very different scales, comparing the raw size of their coefficients directly can be misleading - a small-looking coefficient can still represent a large real-world effect.

Keeping Inputs Consistent

TermMeaning
XA 2D array/table with one column per input feature
yThe 1D array of known outcomes you're trying to predict
model.coef_One weight per feature, showing its effect holding the others constant
model.intercept_The predicted outcome when every feature is 0
Note: Always pass new data to .predict() with the same columns in the same order used when you called .fit() - LinearRegression has no built-in way to know which number means what.

Exercise: Data Science Regression

What is the primary goal of a regression model?