-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_operators.py
58 lines (40 loc) · 1.16 KB
/
binary_operators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
import matplotlib.pyplot as plt
'''
1)Get the results of a Least Square Linear Regression to model a binary
operator using the normal equations.
2) Check whether the operator can actually be modelled with a linear neural
network
'''
X1 = np.zeros( (4,1) )
X1[0,0] = 0
X1[1,0] = 0
X1[2,0] = 1
X1[3,0] = 1
X2 = np.zeros( (4,1) )
X2[0,0] = 0
X2[1,0] = 1
X2[2,0] = 0
X2[3,0] = 1
y = np.zeros( (4,1) )
y[0,0] = 0
y[1,0] = 1
y[2,0] = 1
y[3,0] = 1
X = np.concatenate( (X1,X2,), axis=1)
X = np.concatenate( (np.ones((X.shape[0],1) ),X) ,axis=1)
wLS = np.linalg.solve(np.dot(X.T,X),np.dot(X.T,y))
print('Using LS normal equations we obtain wLS = \n',wLS)
fig = plt.figure()
for i in range(0,len(y)):
if y[i]:
plt.plot(X1[i],X2[i],marker='o',color="r")
else:
plt.plot(X1[i],X2[i],marker='o',color="b")
simArtist = plt.Line2D((0,1),(0,0), color='b', marker='o', linestyle='')
anyArtist = plt.Line2D((0,1),(0,0), color='r',marker='o', linestyle='')
plt.legend([simArtist,anyArtist],['y = 0', 'y = 1'])
plt.gca().set_aspect('equal', adjustable='box')
plt.tight_layout()
plt.xlabel('x1')
plt.ylabel('x2')