import
numpy as np
x1
=
np.array([
1
,
1
,
1
,
1
,
1
,
1
]).reshape(
6
,
1
)
x2
=
np.array([
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
]).reshape(
6
,
1
)
x3
=
np.array([
1
,
1
,
-
1
,
-
1
,
1
,
1
]).reshape(
6
,
1
)
x4
=
np.array([
-
1
,
-
1
,
1
,
1
,
-
1
,
-
1
]).reshape(
6
,
1
)
y1
=
np.array([
1
,
1
,
1
]).reshape(
3
,
1
)
y2
=
np.array([
-
1
,
-
1
,
-
1
]).reshape(
3
,
1
)
y3
=
np.array([
1
,
-
1
,
1
]).reshape(
3
,
1
)
y4
=
np.array([
-
1
,
1
,
-
1
]).reshape(
3
,
1
)
inputSet
=
np.concatenate((x1, x2, x3, x4), axis
=
1
)
targetSet
=
np.concatenate((y1.T, y2.T, y3.T, y4.T), axis
=
0
)
print
(
"\nWeight matrix:"
)
weight
=
np.dot(inputSet, targetSet)
print
(weight)
print
(
"\n------------------------------"
)
print
(
"\nTesting for input patterns: Set A"
)
def
testInputs(x, weight):
y
=
np.dot(weight.T, x)
y[y <
0
]
=
-
1
y[y >
=
0
]
=
1
return
np.array(y)
print
(
"\nOutput of input pattern 1"
)
print
(testInputs(x1, weight))
print
(
"\nOutput of input pattern 2"
)
print
(testInputs(x2, weight))
print
(
"\nOutput of input pattern 3"
)
print
(testInputs(x3, weight))
print
(
"\nOutput of input pattern 4"
)
print
(testInputs(x4, weight))
print
(
"\nTesting for target patterns: Set B"
)
def
testTargets(y, weight):
x
=
np.dot(weight, y)
x[x <
=
0
]
=
-
1
x[x >
0
]
=
1
return
np.array(x)
print
(
"\nOutput of target pattern 1"
)
print
(testTargets(y1, weight))
print
(
"\nOutput of target pattern 2"
)
print
(testTargets(y2, weight))
print
(
"\nOutput of target pattern 3"
)
print
(testTargets(y3, weight))
print
(
"\nOutput of target pattern 4"
)
print
(testTargets(y4, weight))