Fig. 1 in words
178 Italian wines · two measurements · four maps · same data as code 05
1 Linear · z-scores · 0.926
- proline (500 to 1 500) against flavanoids (1 to 5), the same axes in all four maps
- dots: cultivar 1 (green) · cultivar 2 (gold) · cultivar 3 (purple) · red ring: missed
- ① three regions parted by straight lines
2 Tree · depth 3 · 0.907
- ② box-shaped regions
- dashed line: proline ≤ 755?
3 5-NN · z-scores · 0.944
- ③ regions that follow the shape of the clouds
4 5-NN · raw units · 0.704
- ④ thin horizontal bands of all three colours
- red rings on many wines at the left and in the bands
01 Linear: Weigh, Add Linear
- Logistic regression sums weighted measurements and turns the sum into a probability (Cox 1958)
- Its boundaries are straight lines, or flat planes in more dimensions
Assumption: the classes part along straight lines. Here it held: 0.926 on two columns, 1.000 on all 13.
02 Tree: Ask, Split Tree
- One yes/no question per node, until a leaf names the class (Breiman et al. 1984)
- Regions are boxes; scaling the columns left its score unchanged
- Small changes in data can grow a different tree (scikit-learn)
Depth 3 here: 0.907 on two columns, 0.870 on all 13.
03 Neighbours: Vote k-NN
- 5-NN labels a wine by the vote of its 5 closest learn wines (Fix & Hodges 1951)
- With unlimited data, 1-NN errs at most twice as often as the best possible rule (Cover & Hart 1967)
Assumption: close in measurement means close in class. “Close” needs a common scale.
04 Scale First Units
- Raw units: 5-NN scores 0.704 on two columns, 0.741 on 13
- z-scores fitted on the learn set: 0.944 and 1.000
- The tree does not move: 0.870 either way
Scaling sits inside the pipeline, so test wines never shape it (Sprint 20).
05 Try It Yourself PYTHON 3.13.15
sprint28_three_classical_learners.py
from sklearn.datasets import load_wine
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
X, y = load_wine(return_X_y=True) # 3 cultivars
a, b, ya, yb = train_test_split(X, y, test_size=0.3,
stratify=y, random_state=28)
learners = {
"linear": LogisticRegression(max_iter=10000),
"tree": DecisionTreeClassifier(max_depth=3,
random_state=0),
"5-nn": KNeighborsClassifier(n_neighbors=5)}
print(len(ya), "learn wines,", len(yb), "test wines")
for cols in ([6, 12], list(range(13))): # 6 flavanoids,
A, B = a[:, cols], b[:, cols] # 12 proline
for name, m in learners.items():
raw = m.fit(A, ya).score(B, yb)
z = make_pipeline(StandardScaler(), m) # z-scores
print(len(cols), f"{name:7}{raw:.3f}",
f"{z.fit(A, ya).score(B, yb):.3f}")
Output, as printed on the image
124 learn wines, 54 test wines
2 linear 0.926 0.926
2 tree 0.907 0.907
2 5-nn 0.704 0.944
13 linear 0.981 1.000
13 tree 0.870 0.870
13 5-nn 0.741 1.000
Run it. sprint28_three_classical_learners.py · versions, data and steps
Check: 50/54 = 0.926 · 49/54 = 0.907 · 51/54 = 0.944 · 38/54 = 0.704 · 47/54 = 0.870 · 1/54 = 0.019. scikit-learn 1.9.1
06 General Notes Practice
- Pick a learner for its assumption, then test it
- One wine in 54 moves accuracy by 0.019: a gap of one or two wines is noise
- Read the data's provenance, not only its rows
scikit-learn's wine notes repeat the iris file's creator, donor and date. UCI credits Aeberhard and Forina.
07 Mini Project Hands-on
- Rerun with random_state 1 to 5
- Try k = 1, 15 and 51 on the two columns
- Let the tree grow to full depth
- Print each tree's first question
Seeds 1 to 5 opened with colour intensity three times, proline once, alcohol once.
R Trusted Sources
- Cox (1958). The Regression Analysis of Binary Sequences. J. R. Stat. Soc. B 20(2), 215–242
- Breiman, Friedman, Olshen & Stone (1984). Classification and Regression Trees. Wadsworth
- Fix & Hodges (1951). Discriminatory Analysis. Nonparametric Discrimination: Consistency Properties. USAF School of Aviation Medicine, Report 4
- Cover & Hart (1967). Nearest Neighbor Pattern Classification. IEEE Trans. Inf. Theory 13(1), 21–27
- Aeberhard & Forina (1992). Wine. UCI ML Repository
- scikit-learn docs: Decision Trees · Importance of Feature Scaling · load_wine
