Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions learning_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ def display_digits():

def train_model():
"""Train a model on pictures of digits.

Read in 8x8 pictures of numbers and evaluate the accuracy of the model
when different percentages of the data are used as training data. This function
plots the average accuracy of the model as a function of the percent of data
used to train it.
"""
data = load_digits()
num_trials = 10
num_trials = 100
train_percentages = range(5, 95, 5)
test_accuracies = numpy.zeros(len(train_percentages))

Expand All @@ -38,8 +38,15 @@ def train_model():
# variability.
# For consistency with the previous example use
# model = LogisticRegression(C=10**-10) for your learner

# TODO: your code here
for idx, train_size in enumerate(train_percentages):
trial_accuracies = []
for i in range(num_trials):
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, train_size=train_size/100)
model = LogisticRegression(C=10**-1)
model.fit(X_train, y_train)
trial_accuracies.append(model.score(X_test, y_test))
average = sum(trial_accuracies)/num_trials
test_accuracies[idx] = average

fig = plt.figure()
plt.plot(train_percentages, test_accuracies)
Expand All @@ -51,4 +58,4 @@ def train_model():
if __name__ == "__main__":
# Feel free to comment/uncomment as needed
display_digits()
# train_model()
train_model()
Binary file added questions.txt.pdf
Binary file not shown.