Views : 49
Description: Introductory Data Mining Assignment
Compute Environment: Ubuntu 18.04 (Deprecated)
In :
import numpy as np
import random

def step(x):
if x < 0:
res = 0
else:
res = 1

return res

In :
step(0)

1
In :
step(-1)

0
In :
step(1)

1
In :
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 :
train

[(array([0, 0, 1]), 0), (array([0, 1, 1]), 1), (array([1, 0, 1]), 1), (array([1, 1, 1]), 1)]
In :
train2

[(array([0, 0, 1]), 0), (array([0, 1, 1]), 0), (array([1, 0, 1]), 0), (array([1, 1, 1]), 1)]
In :
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 :
learnRate = 0.1
itter = 10000

In :
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 :
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 :
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