Skip to content

Commit

Permalink
updated for Python 3.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
anandman committed Feb 4, 2017
1 parent e99416e commit 3ef0f0b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions mnist_average_darkness.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def main():
# correctly
num_correct = sum(int(guess_digit(image, avgs) == digit)
for image, digit in zip(test_data[0], test_data[1]))
print "Baseline classifier using average darkness of image."
print "%s of %s values correct." % (num_correct, len(test_data[1]))
print("Baseline classifier using average darkness of image.")
print("{0} of {1} values correct.".format(num_correct, len(test_data[1])))

def avg_darknesses(training_data):
""" Return a defaultdict whose keys are the digits 0 through 9.
Expand All @@ -47,7 +47,7 @@ def avg_darknesses(training_data):
digit_counts[digit] += 1
darknesses[digit] += sum(image)
avgs = defaultdict(float)
for digit, n in digit_counts.iteritems():
for digit, n in digit_counts.items():
avgs[digit] = darknesses[digit] / n
return avgs

Expand All @@ -57,7 +57,7 @@ def guess_digit(image, avgs):
assumed to be a defaultdict whose keys are 0...9, and whose values
are the corresponding average darknesses across the training data."""
darkness = sum(image)
distances = {k: abs(v-darkness) for k, v in avgs.iteritems()}
distances = {k: abs(v-darkness) for k, v in avgs.items()}
return min(distances, key=distances.get)

if __name__ == "__main__":
Expand Down

0 comments on commit 3ef0f0b

Please sign in to comment.