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.0Read 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.]Keeping Inputs Consistent
Exercise: Data Science Regression
What is the primary goal of a regression model?