ML Fundamentals: lesson 4 of 7

ML Fundamentals

PATH 02MODULE 01LESSON 04 OF 07Next: Training, Inference, and the Machine Learning Workflow

Your First Machine Learning Model with scikit-learn

Train, predict with, and inspect a simple scikit-learn model using the full machine learning loop.

Beginner18 min readmachine-learningscikit-learnfitpredictregression

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 priceIllustrative predictionAbsolute 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

  1. X selects information available before a price is known.
  2. y selects the known price to learn from.
  3. The split keeps some rows out of training.
  4. LinearRegression() creates a model object.
  5. fit() learns a relationship from training examples.
  6. predict() applies it to held-out feature rows.
  7. 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

  1. Passing y inside X.
  2. Fitting on test data.
  3. Comparing predictions with y_train instead of aligned y_test rows.
  4. Assuming a low toy-data error proves production value.
  5. 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

  1. Replace size_sqft with customer tenure in a simple prediction example.
  2. Why must predictions be compared with y_test?
  3. Explain MAE in plain language to a business stakeholder.

Quick Quiz

  1. Which method learns from training data? Answer: fit().
  2. Which method produces estimates for new rows? Answer: predict().
  3. 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.