-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPINaiveBayes.py
84 lines (72 loc) · 2.28 KB
/
MPINaiveBayes.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
from collections import Counter
import pandas as pd
import numpy as np
from mpi4py import MPI
import random
import time
start_time = time.time()
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
df = pd.read_csv('out.csv')
df.drop("Unnamed: 0", axis=1, inplace=True)
cls = list(df.columns)
lcls = len(cls) - 1
ratio = (len(cls)-1) // 4
def fit(data,classname,alpha,k):
pm={}
parametric_table={}
yy=alpha*k
columns=list(data.columns)
columns.remove(classname)
for i in columns:
pm[i]=Counter(data[i])
class_names=list(Counter(data[classname]).keys())
for i in pm:
temp={}
for j in pm[i].keys():
for k in class_names:
a=data[i]==j
b=data[classname]==k
c=np.logical_and(a,b).sum()
xx=np.array(b).sum()
temp[j,k]=(c+alpha)/(xx+yy)
parametric_table[i]=temp
return parametric_table
def predict(q,parametric_table,columns,class_names):
res=[]
for i in class_names:
t=1
for j in range(len(q)):
t=t*parametric_table[columns[j]][(q[j],i)]
res.append(t)
res=np.array(res)
return class_names[np.argmax(res)]
if rank == 0:
temp=[]
for i in range(4):
temp.append(cls[i*ratio:(i+1)*ratio])
v2 = comm.sendrecv(df[temp[1]+["Class"]],dest=1,source=1)
v3 = comm.sendrecv(df[temp[2]+["Class"]],dest=2,source=2)
v4 = comm.sendrecv(df[temp[3]+["Class"]],dest=3,source=3)
V = {}
v1 = res = fit(df[temp[0]+["Class"]],"Class",1,lcls)
for d in [v1, v2, v3, v4]:
V.update(d)
#print(V)
end_time = time.time()
results = predict(["Weekday","Winter","High","Heavy"],V,cls,list(set(df["Class"])))
print(results)
print("Time taken: ", end_time - start_time)
if rank == 1:
d2 = comm.recv(source=0)
res = fit(d2,"Class",1,lcls)
comm.send(res,dest=0)
if rank == 2:
d3 = comm.recv(source=0)
res = fit(d3,"Class",1,lcls)
comm.send(res,dest=0)
if rank == 3:
d4 = comm.recv(source=0)
res = fit(d4,"Class",1,lcls)
comm.send(res,dest=0)