Last active
December 27, 2015 20:09
-
-
Save mmtootmm/7382602 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
''' | |
最近傍法による単純な数字認識のサンプルプログラム | |
''' | |
import numpy as np | |
prototype_1 = np.array([[0., 0., 1., 0., 0.], | |
[0., 0., 1., 0., 0.], | |
[0., 0., 1., 0., 0.], | |
[0., 0., 1., 0., 0.], | |
[0., 0., 1., 0., 0.]]) | |
prototype_9 = np.array([[0., 1., 1., 1., 0.], | |
[1., 0., 0., 0., 1.], | |
[0., 1., 1., 1., 1.], | |
[0., 0., 0., 1., 0.], | |
[0., 0., 1., 0., 0.]]) | |
def recognize(weight, pattern): | |
# retrieve nearness | |
return np.vdot(weight, pattern) - 0.5 * np.vdot(weight, weight) | |
def run(): | |
input_pattern = np.array([[0., 1., 1., 1., 0.], | |
[0., 0., 0., 0., 0.], | |
[0., 1., 1., 1., 0.], | |
[0., 0., 0., 0., 0.], | |
[0., 0., 0., 0., 0.]]) | |
d1 = recognize(prototype_1, input_pattern) | |
d9 = recognize(prototype_9, input_pattern) | |
print d1 | |
print d9 | |
# select nearest neighbor (or reject) | |
if d1 > d9: | |
print "pattern likes 1" | |
elif d1 < d9: | |
print "pattern likes 9" | |
else: | |
print "pattern rejected" | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment