Implementing Multiple Linear Regression in Scikit-learn
What You'll Learn
You'll learn how to use Scikit-learn's LinearRegression class to handle datasets with multiple features, interpret the model's outputs, and make predictions on new data.
Using LinearRegression for Multi-Feature Problems
You've already seen LinearRegression work with one feature. The beautiful part? It handles multiple features identically. Simply pass in a 2D array where each column represents a different feature, and Scikit-learn does the rest.
from sklearn.linear_model import LinearRegression
import numpy as np
# Features: [square_feet, bedrooms, age]
X = np.array([[1500, 3, 10],
[2000, 4, 5],
[1200, 2, 15]])
y = np.array([300000, 400000, 250000]) # prices
model = LinearRegression()
model.fit(X, y)
Interpreting the Outputs
After fitting, examine the learned parameters:
print(model.coef_) # Array of coefficients [β₁, β₂, β₃]
print(model.intercept_) # The intercept term β₀
Each coefficient tells you: "Holding all other features constant, a one-unit increase in this feature changes the prediction by this amount."
Making Predictions
Predict on new data by passing a 2D array with the same feature structure:
new_house = np.array([[1800, 3, 7]])
predicted_price = model.predict(new_house)
Think of it like a recipe: you provide the ingredients (features), and the model combines them using its learned weights (coefficients) to produce the dish (prediction).
Key Takeaway: Scikit-learn's LinearRegression seamlessly scales from one to many features—just ensure your input is 2D, interpret coef_ to understand feature importance, and use predict() for new examples.