ML Fundamentals: lesson 4 of 7
ML Fundamentals
Your First Machine Learning Model with scikit-learn
Train, predict with, and inspect a simple scikit-learn model using the full machine learning loop.
Concept
This lesson runs the complete machine learning loop once: choose inputs and a target, split examples, fit a model, predict unseen rows, and inspect an intuitive error. LinearRegression is used as a tool; the detailed algorithm comes later.
Example: House Prices
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
df = pd.DataFrame({
"size_sqft": [800, 950, 1100, 1300, 1500, 1800, 2100, 2400],
"bedrooms": [2, 2, 2, 3, 3, 3, 4, 4],
"price": [160000, 185000, 210000, 250000, 285000, 340000, 400000, 455000],
})
X = df[["size_sqft", "bedrooms"]]
y = df["price"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42
)
model = LinearRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
mae = mean_absolute_error(y_test, predictions)
print(predictions)
print(mae)
What Just Happened?
train_test_split keeps some rows aside so the model can be checked on examples it did not fit. model.fit(X_train, y_train) learns parameters from the training feature rows and their known prices. model.predict(X_test) applies those learned parameters to new feature rows. This prediction stage is inference. MAE asks, in the price unit, how far predictions were from actual test prices on average.
The exact numbers depend on the split and tiny dataset. Successful code does not prove a useful real-world model; this example only demonstrates the workflow.
Actual Versus Predicted
The following is an illustrative interpretation, not literal output from the code. A held-out house with an actual price of $400,000 might receive a prediction near $392,000, leaving an $8,000 absolute difference. A second house could be predicted near $449,000 when its actual price is $455,000.
| Actual price | Illustrative prediction | Absolute difference |
|---|---|---|
| $400,000 | $392,000 | $8,000 |
| $455,000 | $449,000 | $6,000 |
A prediction is an estimate from feature values, not a promise that the final sale price will match. MAE summarizes the average absolute gap in the same unit as price: "on average, how far were our predictions from the actual prices?" A low MAE on eight toy rows is not evidence that the model will work in a real housing market; the data is too small and simple to represent normal variation.
The Workflow in One Pass
Xselects information available before a price is known.yselects the known price to learn from.- The split keeps some rows out of training.
LinearRegression()creates a model object.fit()learns a relationship from training examples.predict()applies it to held-out feature rows.- MAE compares predictions with matching held-out prices.
Training Versus Prediction
Training uses examples with answers to adjust a model. Prediction uses the fitted model with new features, where the answer is not yet known. Never fit on test data just to improve the displayed score: that removes the honest check the held-out rows provide.
Failure Signals
Common Mistakes
- Passing
yinsideX. - Fitting on test data.
- Comparing predictions with
y_traininstead of alignedy_testrows. - Assuming a low toy-data error proves production value.
- Treating a model as an explanation of house prices.
Best Practices
Keep feature names explicit, preserve matching X/y rows, use a reproducible split while learning, and inspect a few prediction-versus-actual pairs. Later lessons add more careful validation, generalization, and metric selection.
Interview Perspective
Question: What do fit and predict mean? Answer: fit learns model parameters from training examples; predict applies the learned relationship to new feature rows. Follow-up: why hold out test data?
Practice Questions
- Replace
size_sqftwith customer tenure in a simple prediction example. - Why must
predictionsbe compared withy_test? - Explain MAE in plain language to a business stakeholder.
Quick Quiz
- Which method learns from training data? Answer:
fit(). - Which method produces estimates for new rows? Answer:
predict(). - Does a test split fully solve evaluation? Answer: no; later lessons add more care.
Key Takeaway
Key Takeaways
The core loop is data -> X/y -> split -> model -> fit -> predict -> inspect error. Training learns from known outcomes; inference applies that learned pattern to new rows.
Next Lesson
Next, separate training and inference more carefully within the broader machine learning workflow.
Finish this lesson on your terms
Mark it complete when you have worked through the material and are ready to move on.