IFI 8410 — Session 13: Introductory Machine Learning
The examples are the input; the rule is the output
The problem
What usually exists is a record of past cases with their outcomes. Machine learning hands those examples to a program and lets it find a rule. That takes three lines of code. The problem is everything around them:
Compared with what?
A model can score perfectly on its own examples and be useless on new ones.
Missing a fraud and raising a false alarm are not the same error.
A model will use any column that helps, including ones that would not exist when a real prediction is needed.
The tools
machine learning
Dozens of models behind one interface: fit, predict, score. Splitting data, metrics, baselines, cross-validation.
preparing the data
Building the table of features: filling blanks, encoding categories, selecting columns.
arrays
What scikit-learn computes on and returns.
looking
Accuracy against model complexity; pictures of what a model has learned.
scikit-learn's strength is uniformity: every model is used with the same four steps, so what you learn on one transfers to all.
Concept 1
X = days[["temperature", "rain", "weekend"]]
y = days["busy"]| Name | Example | |
|---|---|---|
| what the model may look at | features, X | temperature, rain, weekend |
| what it should predict | target, y | busy or not |
A target that is a category makes the task classification; a number makes it regression. Today: two classes, 0 and 1, where 1 is the outcome you are looking for.
Concept 2
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=13)Score a model on the rows it learned from and the answer is often close to 100 %. That is an exam whose questions were handed out beforehand.
Breaking any of the three produces a number that looks better than the truth. When the model will predict the future from the past, the honest test set is the later part of the data, not a random part.
Concept 3
majority = y_train.value_counts().idxmax() # learned from the training set
baseline = (y_test == majority).mean() # measured on the test setA model that does not beat the baseline is a finding, and should be reported as one.
Concept 4
from sklearn.tree import DecisionTreeClassifier, export_text
model = DecisionTreeClassifier(max_depth=2, random_state=13) # make
model.fit(X_train, y_train) # learn
predictions = model.predict(X_test) # predict
model.score(X_test, y_test) # accuracy
print(export_text(model, feature_names=list(X.columns))) # read itConcept 5
| Depth | Training rows | Unseen rows |
|---|---|---|
| 1 | modest | modest |
| 4 | better | best |
| 20 | nearly 100 % | lower again |
Training accuracy can only rise. Test accuracy rises, stalls, then falls. The widening gap is the picture of overfitting.
max_depth limits how many questions the tree may ask in a row.Concept 5, continued
Which depth, then? Not the one with the best test score: that would use the test set to choose.
all rows ──▶ training set ──▶ fit part models are trained here
│ └──▶ validation part the settings are chosen here
└──▶ test set the final model is measured here, oncecross_val_score repeats the validation split several times, which shows how much one split's number can move by chance.Concept 6
| Is 1 | Is 0 | |
|---|---|---|
| Predicted 1 | true positive | false positive: a false alarm |
| Predicted 0 | false negative: a miss | true negative |
| Score | Formula | Question |
|---|---|---|
| precision | tp / (tp + fp) | when it says 1, how often is it right? |
| recall | tp / (tp + fn) | of the real 1s, how many does it find? |
confusion_matrix(y_test, predictions) is laid out [[tn, fp], [fn, tp]].
Concept 7
A feature leaks when it carries information that would not be available at the moment a real prediction is needed.
A column filled in after the outcome. An average that includes the row itself. A statistic computed over training and test rows together.
A score too good to be true. The model seizes on the leak, scores brilliantly, and is worthless in use.
Worked example — notebook 1
A year of records: temperature, rain, weekend, and whether the day was busy.
A tree trained and scored on all the days.
The same kind of tree, scored on days put aside.
Always saying "quiet".
Worked example — notebook 2
1.3 million consumer complaints; one in forty is answered late. An agency would like to flag those on arrival.
| Model | What happened |
|---|---|
| Always "on time" | 97.5 % accurate. |
| Product, channel, date | Also 97.5 %, and flags nothing: recall 0. |
| Adds the company's response category | Perfect precision. One value is "untimely response", written after the outcome. A leak: discard. |
| Each company's past share of late answers, from training rows only | Accuracy 97.5 % → 97.9 %. Recall 0 → about 0.3, with seven alarms in ten real. |
| Trained on earlier years, tested on later ones | Precision falls to about five in ten. The random split had been optimistic. |
Most of the improvement came from the choice of feature, not from the algorithm, which is the usual experience.
Reporting
Not what causes what. Does a restaurant close because it lists no hours, or does a closed restaurant's listing lose its hours?
It predicts the future only as far as the future resembles the past.
It will use any column that helps.
To take away
Now: hands-on
Folder 13-Intro-Machine-Learning — work them in this order:
Concepts
The coffee cart: features, the split, the baseline, a tree, overfitting, and the confusion matrix. Four exercises.
Worked example
A model that is 97.5 % accurate and useless, and how to do better, in nine steps. Run it on ARC.
Look things up
scikit-learn calls and the modelling workflow to keep open while you work.
As you work, ask of every score: compared with what, and on which rows?
Homework: Homework 12: Does It Beat Guessing?

