利用图片的灰度平均值来进行分类实现手写图片识别(数据集50000张图片)——Jason niu
from collections import defaultdict import mnist_loader def main(): training_data, validation_data, test_data = mnist_loader.load_data()
avgs = avg_darknesses(training_data) # testing phase: see how many of the test images are classified # 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]))) def avg_darknesses(training_data): """ Return a defaultdict whose keys are the digits 0 through 9. For each digit we compute a value which is the average darkness of training images containing that digit. The darkness for any particular image is just the sum of the darknesses for each pixel.""" digit_counts = defaultdict(int) darknesses = defaultdict(float) for image, digit in zip(training_data[0], training_data[1]): digit_counts[digit] += 1 darknesses[digit] += sum(image) avgs = defaultdict(float) for digit, n in digit_counts.items(): avgs[digit] = darknesses[digit] / n return avgs def guess_digit(image, avgs): """Return the digit whose average darkness in the training data is closest to the darkness of ``image``. Note that ``avgs`` is 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.items()} return min(distances, key=distances.get) if __name__ == "__main__": main()
不念过去,不畏将来!
理想,信仰,使命感……
愿你出走半生,归来仍是少年……