-
Notifications
You must be signed in to change notification settings - Fork 0
/
learning_curve_play.py
48 lines (39 loc) · 1.96 KB
/
learning_curve_play.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# In this exercise we'll examine a learner which has high variance, and tries to learn
# nonexistant patterns in the data.
# Use the learning curve function from sklearn.learning_curve to plot learning curves
# of both training and testing error.
from sklearn.tree import DecisionTreeRegressor
import matplotlib.pyplot as plt
from sklearn.learning_curve import learning_curve
from sklearn.cross_validation import KFold
from sklearn.metrics import explained_variance_score, make_scorer
import numpy as np
# Set the learning curve parameters; you'll need this for learning_curves
size = 1000
cv = KFold(size,shuffle=True)
score = make_scorer(explained_variance_score)
# Create a series of data that forces a learner to have high variance
X = np.round(np.reshape(np.random.normal(scale=5,size=2*size),(-1,2)),2)
y = np.array([[np.sin(x[0]+np.sin(x[1]))] for x in X])
def plot_curve():
reg = DecisionTreeRegressor()
reg.fit(X,y)
print "Regressor score: {:.4f}".format(reg.score(X,y))
# TODO: Use learning_curve imported above to create learning curves for both the
# training data and testing data. You'll need 'size', 'cv' and 'score' from above.
training_sizes, training_scores, testing_scores = learning_curve(reg, X, y, train_sizes=[100,200,300,400,500], cv=cv, scoring=score)
# TODO: Plot the training curves and the testing curves
# Use plt.plot twice -- one for each score. Be sure to give them labels!
#for idx in range(len(training_scores)):
#plt.plot(training_scores[idx], label='training_scores {}'.format(idx))
#plt.plot(testing_scores[idx], label='testing_scores {}'.format(idx))
plt.plot(training_scores, label='training_scores')
plt.plot(testing_scores, label='testing_scores')
plt.plot_date(X, y, label="data")
# Plot aesthetics
plt.ylim(-0.1, 1.1)
plt.ylabel("Curve Score")
plt.xlabel("Training Points")
plt.legend(bbox_to_anchor=(1.1, 1.1))
plt.show()
plot_curve()