Machine Learning
Line plot
A line plot shows change over time.
Line plot
A line plot shows change over an ordered variable (time, epoch, hyperparameter). Its most important use in ML is learning and validation curves:
derinlik = [1, 2, 3, 5, 8]
ax.plot(derinlik, egitim, marker="o", label="eğitim")
ax.plot(derinlik, dogrulama, marker="o", label="doğrulama")
ax.legend()
The point where the two curves separate is where overfitting starts; the plot shows this far faster than a table.
Learning curve
learning_curve gives how the scores change as the training set grows. If the validation score is still rising, more data helps; if the curves have merged and are low, a more complex model is needed:
from sklearn.model_selection import learning_curve
n, tr, va = learning_curve(model, X, y, cv=5, train_sizes=[0.2, 0.5, 1.0])
ax.plot(n, tr.mean(axis=1), label="eğitim")
ax.plot(n, va.mean(axis=1), label="doğrulama")
The answer to "more data or a better model?" is in this plot.
Exercise Learning curve
Run learning_curve on Iris for KNeighborsClassifier() (cv=5, train_sizes=[0.3, 0.6, 1.0], shuffle=True, random_state=0). Plot the validation means as a line plot and save it; print the training sizes and the validation means (round(3)) on separate lines.
Show example solution
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import learning_curve
from sklearn.neighbors import KNeighborsClassifier
X, y = load_iris(return_X_y=True)
n, tr, va = learning_curve(KNeighborsClassifier(), X, y, cv=5, train_sizes=[0.3, 0.6, 1.0], shuffle=True, random_state=0)
fig, ax = plt.subplots()
ax.plot(n, va.mean(axis=1), marker="o")
fig.savefig("ogrenme.png")
print(n)
print(va.mean(axis=1).round(3))
Test yourself
-
If the training curve rises while the validation curve starts to fall, what is happening?
- The data shrank
- Overfitting is starting
- Underfitting
- The model converged
Show answer
Overfitting is starting — The model has started learning details specific to the training data.
-
Show the points with circle markers.
ax.plot(x, y, ____="o")
Show answer
ax.plot(x, y, marker="o")
-
Match the curve situations.
- Diverging curves
- Overfitting
- Converged and low
- Underfitting
- Validation still rising
- More data helps
- learning_curve
- Score by data size
Next lesson: Box plot