Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132923 views
License: OTHER
1
import matplotlib.pyplot as plt
2
import numpy as np
3
4
from sklearn.linear_model import Ridge, LinearRegression
5
from sklearn.model_selection import learning_curve, KFold
6
7
from .datasets import load_extended_boston
8
9
10
def plot_learning_curve(est, X, y):
11
training_set_size, train_scores, test_scores = learning_curve(
12
est, X, y, train_sizes=np.linspace(.1, 1, 20), cv=KFold(20, shuffle=True, random_state=1))
13
estimator_name = est.__class__.__name__
14
line = plt.plot(training_set_size, train_scores.mean(axis=1), '--',
15
label="training " + estimator_name)
16
plt.plot(training_set_size, test_scores.mean(axis=1), '-',
17
label="test " + estimator_name, c=line[0].get_color())
18
plt.xlabel('Training set size')
19
plt.ylabel('Score (R^2)')
20
plt.ylim(0, 1.1)
21
22
23
def plot_ridge_n_samples():
24
X, y = load_extended_boston()
25
26
plot_learning_curve(Ridge(alpha=1), X, y)
27
plot_learning_curve(LinearRegression(), X, y)
28
plt.legend(loc=(0, 1.05), ncol=2, fontsize=11)
29
30