CodeBlanks Logo Icon CodeBlanks

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.

Practice 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.

Starter code

model.py
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)

# buraya yaz

Sample solution

model.py
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))

Expected output

[ 36  72 120]
[0.947 0.953 0.973]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 If the training curve rises while the validation curve starts to fall, what is happening?

  • The data shrank
  • Overfitting is starting Correct answer
  • Underfitting
  • The model converged

Explanation: The model has started learning details specific to the training data.

2 Show the points with circle markers.

Question
ax.plot(x, y, ____="o")
Answer
ax.plot(x, y, marker="o")

Explanation: marker puts a symbol on every data point.

3 Match the curve situations.

Term Meaning
Diverging curves Overfitting
Converged and low Underfitting
Validation still rising More data helps
learning_curve Score by data size

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation