-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_pgm_lpf.py
114 lines (90 loc) · 3.37 KB
/
train_pgm_lpf.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
#!/usr/bin/env python
# coding: utf-8
# In[18]:
import cv2
from pgmpy.estimators import BdeuScore, K2Score, BicScore
from pgmpy.models import BayesianModel
from pgmpy.estimators import MaximumLikelihoodEstimator
import pandas as pd
import numpy as np
import time
import pickle
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import networkx as nx
from networkx.drawing.nx_pylab import draw_networkx
from networkx.algorithms.cycles import find_cycle
def get_accuracy(model,X_test,y_test):
pred = model.predict(X_test)
return accuracy_score(y_test,pred)
def check_dag(edge_list):
G = nx.DiGraph(edge_list)
try:
cycle = find_cycle(G, orientation='original')
except:
return False
return cycle
# In[11]:
def generate_edges(child,parents):
edge_list = []
for parent in parents:
edge_list.append([parent,child])
return edge_list
# In[12]:
def get_cycle_edge(cycle,score,list_of_edges):
cycle_edge_scores = []
for parent,child,direction in cycle:
cycle_edge_scores.append(score.local_score(child,[parent]))
min_index = cycle_edge_scores.index(min(cycle_edge_scores))
low_score_edge = cycle[min_index][:-1]
return low_score_edge
# In[13]:
def get_model_architecture(df):
score = K2Score(df)
list_of_edges = []
for i in df.columns:
edge_scores = []
for j in df.columns:
if i!=j:
sco =score.local_score(i,[j])
edge_scores.append((i,j,sco))
edge_scores.sort(key = lambda x:x[2],reverse = True)
parents = [edge_scores[0][1]]
best_score = edge_scores[0][2]
for v in range(1,10):
parents.append(edge_scores[v][1])
new_score = score.local_score(i,parents)
if new_score > best_score:
best_score = new_score
else:
parents = parents[:-1]
break
list_of_edges += generate_edges(i,parents)
cycle = check_dag(list_of_edges)
while cycle:
low_score_edge = get_cycle_edge(cycle,score,list_of_edges)
list_of_edges.remove(list(low_score_edge))
cycle = check_dag(list_of_edges)
return list_of_edges
# In[14]:
columns = ['Pleural Other', 'No Finding','Enlarged Cardiomediastinum', 'Cardiomegaly', 'Lung Opacity',
'Lung Lesion', 'Edema', 'Consolidation', 'Pneumonia', 'Atelectasis',
'Pneumothorax', 'Pleural Effusion', 'Fracture','Support Devices']
for column in columns:
filename = "_".join([w for w in column.split(" ")])
print(column+" Log")
new_df = pd.read_csv('lpf_'+filename.lower()+'_features.csv')
new_df = new_df.drop('Path', axis = 1)
feat_cols = new_df.columns[new_df.columns!='labels']
X_train,X_test,y_train,y_test = train_test_split(new_df[feat_cols],new_df["labels"],test_size = 0.2,random_state = 43)
X_train['labels'] = y_train
list_of_edges = get_model_architecture(X_train)
model = BayesianModel(list_of_edges)
model.fit(X_train)
feature_cols = new_df.columns[new_df.columns!='labels']
accuracy = get_accuracy(model,X_train[feature_cols],X_train['labels'])
print("Training Accuracy",accuracy)
with open('lpf_bayes_'+filename.lower()+'model.pkl','wb') as f:
pickle.dump(model,f,protocol=4)
accuracy = get_accuracy(model,X_test[feature_cols],y_test)
print("Testing Accuracy",accuracy)