neural-networks-and-deep-learning mnist_average_darkness.py
来源:互联网 发布:python tkiner 编辑:程序博客网 时间:2024/05/17 04:05
就是根据每一幅图片的平均sum来计算。。正确率很低
"""mnist_average_darkness~~~~~~~~~~~~~~~~~~~~~~A naive classifier for recognizing handwritten digits from the MNISTdata set. The program classifies digits based on how dark they are--- the idea is that digits like "1" tend to be less dark than digitslike "8", simply because the latter has a more complex shape. Whenshown an image the classifier returns whichever digit in the trainingdata had the closest average darkness.The program works in two steps: first it trains the classifier, andthen it applies the classifier to the MNIST test data to see how manydigits are correctly classified.Needless to say, this isn't a very good way of recognizing handwrittendigits! Still, it's useful to show what sort of performance we getfrom naive ideas."""#### Libraries# Standard libraryfrom collections import defaultdict# My librariesimport mnist_loaderdef main(): training_data, validation_data, test_data = mnist_loader.load_data() # training phase: compute the average darknesses for each digit, # based on the training 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.iteritems(): avgs[digit] = darknesses[digit] / n return avgsdef 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.iteritems()} return min(distances, key=distances.get)if __name__ == "__main__": main()
0 0
- neural-networks-and-deep-learning mnist_average_darkness.py
- neural-networks-and-deep-learning mnist_pca.py
- neural-networks-and-deep-learning expand_mnist.py
- neural-networks-and-deep-learning mnist_svm.py
- neural-networks-and-deep-learning network.py
- neural-networks-and-deep-learning network2.py
- neural-networks-and-deep-learning test.py
- neural-networks-and-deep-learning network3.py
- neural-networks-and-deep-learning misleading_gradient.py
- neural-networks-and-deep-learning misleading_gradient_contours.py
- neural-networks-and-deep-learning backprop_magnitude_nabla.py
- neural-networks-and-deep-learning false_minimum.py
- neural-networks-and-deep-learning valley.py
- neural-networks-and-deep-learning valley2.py
- neural-networks-and-deep-learning weight_initialization.py
- neural-networks-and-deep-learning multiple_eta.py
- neural-networks-and-deep-learning more_data.py
- Neural Networks and Deep Learning
- 杭电-1070-Milk (结构体+sort)
- tjut 5400
- 关于事件总线EventBus的使用
- USB WIFI 腾达811M无线网卡 -RT5370移植
- 多项式求和 hd 2011
- neural-networks-and-deep-learning mnist_average_darkness.py
- Memory leak in Windows 8.1 with Killer E2200 and Windows Network Data Usage Monitoring
- UVA-621 Secret Research
- UVA - 748 Exponentiation
- UVA11988 模拟题,水
- UVA - 10494 If We Were a Child Again
- LeetCode-100:Same Tree
- 暑期总结——网络流
- CentOS 6.5命令行安装MATLAB2013