Machine learning is teaching a program to find patterns from data instead of coding the rules by hand. Rather than writing "if experience > 5 and skills include SQL then…", you show the model examples and it learns the relationship. This chapter trains a real model with scikit-learn's beautifully consistent API.
What is machine learning?
Classic programming: rules + data → answers. Machine learning flips it: data + answers → rules (a "model"). You then apply that model to new data to predict answers you don't have yet.
Types of learning
| Type | You have | Goal | Example |
|---|---|---|---|
| Supervised | Labelled examples | Predict the label | Will this student be placed? |
| Unsupervised | Unlabelled data | Find structure | Group customers into segments |
| Regression | Numeric labels | Predict a number | Expected salary |
| Classification | Category labels | Predict a class | Spam / not spam |
The train/test split
The cardinal rule: never evaluate a model on the data it learned from. Hold back a test set the model never sees during training, so you measure how it performs on new data — which is all that matters:
from sklearn.model_selection import train_test_splitX = df[["experience", "projects"]] # features (inputs)y = df["placed"] # label (what we predict)X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42 # hold back 20% for testing)
Your first model
Every scikit-learn model follows the same three-step pattern: fit (learn), predict (apply), score (evaluate). Learn it once and every model works the same way:
from sklearn.linear_model import LogisticRegressionmodel = LogisticRegression()model.fit(X_train, y_train) # learn from the training setpredictions = model.predict(X_test) # predict on unseen dataaccuracy = model.score(X_test, y_test)print(f"Accuracy: {accuracy:.2%}")# Swap the model — the API is identicalfrom sklearn.ensemble import RandomForestClassifiermodel = RandomForestClassifier().fit(X_train, y_train)
Practice
On a labelled dataset, pick two feature columns and a target, split into train/test, and fit a LogisticRegression. Print the accuracy, then swap in a RandomForestClassifier and compare — noting that only one line changed.
Key takeaways
- 01Machine learning learns rules from data + answers, then predicts on new data.
- 02Supervised learning uses labelled data (regression predicts numbers, classification predicts classes); unsupervised finds structure.
- 03Always hold back a test set — never evaluate on the data the model trained on.
- 04Every scikit-learn model shares the fit → predict → score API.
- 05Start with a simple, interpretable baseline before reaching for complex models.
