-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic_gates_classifier.py
84 lines (50 loc) · 1.02 KB
/
logic_gates_classifier.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
59
60
61
62
63
64
65
66
67
68
# Classifying outputs of and, or and xor logic
# Husain Shaikh
# 25 Mar 19
from sklearn import tree
import random
model = tree.DecisionTreeClassifier()
features = []
labels = []
# third feature and meaning
# 1 ---- and
# 2 ---- or
# 3 ---- xor
# Collecting data
for i in range(100):
a = random.randint(0,1)
b = random.randint(0,1)
c = random.randint(1,3)
flist = [a, b, c]
features.append(flist)
# AND logic
if(c==1):
if(a==1 and b==1):
labels.append([1])
else:
labels.append([0])
elif(c==2):
#or logic
if(a==0 and b==0):
labels.append([0])
else:
labels.append([1])
elif(c==3):
#xor logic
if(a==b):
labels.append([0])
else:
labels.append([1])
# Training the model
model.fit(features, labels)
test = []
# generating test data
for j in range(20):
a = random.randint(0,1)
b = random.randint(0,1)
c = random.randint(1,3)
test.append([a, b ,c])
# making predictions
predictions = model.predict(test)
for k in range(len(test)-1):
print(test[k], " : ", predictions[k])