CoCalc Shared FilesA3 / ORTest.ipynbOpen in CoCalc with one click!
Author: Sean Paradiso
Views : 9
Description: Introductory Data Mining Assignment
In [99]:
import numpy as np import random def step(x): if x < 0: res = 0 else: res = 1 return res
In [100]:
step(0)
1
In [101]:
step(-1)
0
In [102]:
step(1)
1
In [103]:
train = [(np.array([0, 0, 1]), 0), (np.array([0, 1, 1]), 1), (np.array([1, 0, 1]), 1), (np.array([1, 1, 1]), 1)] train2 = [(np.array([0, 0, 1]), 0), (np.array([0, 1, 1]), 0), (np.array([1, 0, 1]), 0), (np.array([1, 1, 1]), 1)]
In [104]:
train
[(array([0, 0, 1]), 0), (array([0, 1, 1]), 1), (array([1, 0, 1]), 1), (array([1, 1, 1]), 1)]
In [105]:
train2
[(array([0, 0, 1]), 0), (array([0, 1, 1]), 0), (array([1, 0, 1]), 0), (array([1, 1, 1]), 1)]
In [106]:
randNumWeight = np.random.rand(3) randNumWeight2 = np.random.rand(3) print "OR weights (random number array)", randNumWeight print "AND weights(random number array)", randNumWeight2
OR weights (random number array) [ 0.8014153 0.8160564 0.69702759] AND weights(random number array) [ 0.80838801 0.19442953 0.16959215]
In [107]:
learnRate = 0.1 itter = 10000
In [108]:
a = random.choice(train) a2 = random.choice(train2) print "OR:", a print "AND:", a2 print "\n" print randNumWeight print randNumWeight2
OR: (array([0, 0, 1]), 0) AND: (array([1, 1, 1]), 1) [ 0.8014153 0.8160564 0.69702759] [ 0.80838801 0.19442953 0.16959215]
In [109]:
for i in xrange(itter): a, b = random.choice(train) a2, b2 = random.choice(train2) c = np.dot(randNumWeight, a) c2 = np.dot(randNumWeight2, a2) err = b - step(c) err2 = b2 - step(c2) randNumWeight += learnRate * err * a randNumWeight2 += learnRate * err2 * a2
In [110]:
print "\n" print "Results for logical OR" print "\n" for x, c in train: c = dot(x, randNumWeight) print "Logical Input: ", x[:2] print "Update Weight: ", c print "Logical Result: ", step(c) print "\n" print "\n" print "Results for logical AND" print "\n" for x, c2 in train2: c2 = dot(x, randNumWeight2) print "Logical Input: ", x[:2] print "Update Weight: ", c2 print "Logical Result: ", step(c2) print "\n"
Results for logical OR Logical Input: [0 0] Update Weight: -0.00297240662151 Logical Result: 0 Logical Input: [0 1] Update Weight: 0.813083995874 Logical Result: 1 Logical Input: [1 0] Update Weight: 0.798442892002 Logical Result: 1 Logical Input: [1 1] Update Weight: 1.6144992945 Logical Result: 1 Results for logical AND Logical Input: [0 0] Update Weight: -0.530407845884 Logical Result: 0 Logical Input: [0 1] Update Weight: -0.335978318098 Logical Result: 0 Logical Input: [1 0] Update Weight: -0.122019832266 Logical Result: 0 Logical Input: [1 1] Update Weight: 0.0724096955205 Logical Result: 1