AI Hints
Home›Machine Learning Tutorial›Real Prices, Real Metrics
Machine Learning · Lesson 13 of 85

Many Features at Once

Multiple linear regression.

⏱ 2 min read▶ Video at 28:13🎓 Free
Watch this lesson in the Machine Learning courseWatch on YouTube ↗Full Machine Learning course

Your boss drops by. The line through rooms is lovely, the boss says, but a house is more than its rooms. What about income in the area? Location? Age? The answer is a simple upgrade. Instead of one slope, we give the model one slope for every feature.

Many Features at Once
From the video: Many Features at Once
  • A house is more than rooms
  • One slope per feature
  • The same idea, more numbers

From a line to many weights

With one feature, price equals slope times rooms, plus a start. With eight features, it is just longer. Price equals weight one times income, plus weight two times age, and so on, plus a start. Each weight says how much that feature pushes the price up or down. With two features, the line becomes a flat plane. With eight, we cannot draw it, but the maths is exactly the same, and gradient descent still works.

One featurea line
Two featuresa flat plane
Eight featuresthe same maths, unseen
Weightshow much each feature matters

Training on all eight features

Let's train on the whole dataset. Fit takes all the columns at once. Then we print each feature with its learned weight. Look at the output. Med inc has a positive weight, so higher income means a higher price. Latitude and longitude are negative, which means prices fall as you move north and east, away from the rich coast. The model worked out California's geography, from numbers alone.

all_features.pypython
from sklearn.datasets import fetch_california_housing
from sklearn.linear_model import LinearRegression

data = fetch_california_housing(as_frame=True)
X = data.data
y = data.target

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

for name, weight in zip(X.columns, model.coef_):
    print(f"{name:<11} {weight:8.4f}")
print(f"{'start':<11} {model.intercept_:8.2f}")
Output
MedInc        0.4367
HouseAge      0.0094
AveRooms     -0.1073
AveBedrms     0.6451
Population   -0.0000
AveOccup     -0.0038
Latitude     -0.4213
Longitude    -0.4345
start         -36.94

Summary

Eight features, eight weights, one start. And each weight tells a story about the data. But is the model any good? How close are its prices, in real dollars? Next, the numbers that answer that question.

Key takeaways
  • One weight per feature
  • Weights can be read and explained
  • Same training, more dimensions
Next up: How Good Is It? — MAE, RMSE, and R squared