-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEXP-8.py
157 lines (113 loc) · 3.34 KB
/
EXP-8.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import prettytable
print('\n----- Classification using Naïve Bayes -----\n')
total_documents = int(input("Enter the Total Number of documents: "))
doc_class = []
i = 0
keywords = []
while not i == total_documents:
doc_class.append([])
text = input(f"\nEnter the text of Doc-{i+1}: ").lower()
clas = input(f"Enter the class of Doc-{i+1}: ")
doc_class[i].append(text.split())
doc_class[i].append(clas)
keywords.extend(text.split())
i += 1
keywords = set(keywords)
keywords = list(keywords)
keywords.sort()
to_find = input("\nEnter the Text to classify using Naive Bayes: ").lower().split()
probability_table = []
for i in range(total_documents):
probability_table.append([])
for j in keywords:
probability_table[i].append(0)
doc_id = 1
for i in range(total_documents):
for k in range(len(keywords)):
if keywords[k] in doc_class[i][0]:
probability_table[i][k] += doc_class[i][0].count(keywords[k])
print('\n')
keywords.insert(0, 'Document ID')
keywords.append("Class")
Prob_Table = prettytable.PrettyTable()
Prob_Table.field_names = keywords
Prob_Table.title = 'Probability of Documents'
x = 0
for i in probability_table:
i.insert(0, x + 1)
i.append(doc_class[x][1])
Prob_Table.add_row(i)
x += 1
print(Prob_Table)
print('\n')
for i in probability_table:
i.pop(0)
totalpluswords = 0
totalnegwords = 0
totalplus = 0
totalneg = 0
vocabulary = len(keywords) - 2
for i in probability_table:
if i[len(i) - 1] == "+":
totalplus += 1
totalpluswords += sum(i[0:len(i) - 1])
else:
totalneg += 1
totalnegwords += sum(i[0:len(i) - 1])
keywords.pop(0)
keywords.pop(len(keywords) - 1)
# For positive class
temp = []
for i in to_find:
count = 0
x = keywords.index(i)
for j in probability_table:
if j[len(j) - 1] == "+":
count += j[x]
temp.append(count)
count = 0
for i in range(len(temp)):
temp[i] = format((temp[i] + 1) / (vocabulary + totalpluswords), ".4f")
temp = [float(f) for f in temp]
print("\nProbabilities of Each word to be in '+' class are: ")
h = 0
for i in to_find:
print(f"P({i}/+) = {temp[h]}")
h += 1
print()
pplus = float(format((totalplus) / (totalplus + totalneg), ".8f"))
for i in temp:
pplus *= i
pplus = format(pplus, ".8f")
print("Probability of Given text to be in '+' class is:", pplus)
print()
# For Negative class
temp = []
for i in to_find:
count = 0
x = keywords.index(i)
for j in probability_table:
if j[len(j) - 1] == "-":
count += j[x]
temp.append(count)
count = 0
for i in range(len(temp)):
temp[i] = format((temp[i] + 1) / (vocabulary + totalnegwords), ".4f")
temp = [float(f) for f in temp]
print("\nProbabilities of Each word to be in '-' class are: ")
h = 0
for i in to_find:
print(f"P({i}/-) = {temp[h]}")
h += 1
print()
pneg = float(format((totalneg) / (totalplus + totalneg), ".8f"))
for i in temp:
pneg *= i
pneg = format(pneg, ".8f")
print("Probability of Given text to be in '-' class is:", pneg)
print('\n')
if pplus > pneg:
print(f"Using Naive Bayes Classification, We can clearly say that the given text belongs to '+' class with probability {pplus}")
else:
print(f"Using Naive Bayes Classification, We can clearly say that the given text belongs to '-' class with probability {pneg}")
print('\n')