Created
June 15, 2012 05:02
-
-
Save fxsjy/2934746 to your computer and use it in GitHub Desktop.
bpnn solve kindergarten problem(numpy)
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 | |
# Problem: http://www.weibo.com/2090476735/ynJtWvN1K | |
# junyi sun (weibo.com/treapdb) | |
from numpy import * | |
def Q(L): | |
H = [0]*11 | |
H[0] = 1 #bias item | |
for x in L: | |
H[x+1]+=1 | |
return H | |
def demo_digit(): | |
X = array([ | |
Q([7,1,1,1]), | |
Q([8,8,0,9]), | |
Q([2,1,7,2]), | |
Q([6,6,6,6]), | |
Q([1,1,1,1]), | |
Q([2,2,2,2]), | |
Q([7,6,6,2]), | |
Q([9,3,1,3]), | |
Q([0,0,0,0]), | |
Q([5,5,5,5]), | |
Q([8,1,9,3]), | |
Q([8,0,9,6]), | |
Q([4,3,9,8]), | |
Q([9,4,7,5]), | |
Q([9,0,3,8]), | |
Q([3,1,4,8]) | |
]) | |
Y = array([0,6,0,4,0,0,2,1,4,0,3,5,3,1,4,2])[:,None] | |
X_ = linalg.pinv(X) #pseudo inverse matrix | |
theta = dot(X_,Y) | |
input = array([ | |
Q([2,8,8,9]), | |
Q([2,4,6,7]), | |
Q([1,2,3,4]), | |
]) | |
output = dot(input,theta) | |
print output.round() | |
demo_digit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment