-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
151 lines (121 loc) · 4.06 KB
/
utils.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
import os
import warnings
import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import sqlite3
from sklearn.metrics import confusion_matrix
from settings import base_dir
database_file=os.path.join(base_dir,"results.db")
# Function for saving results
def create_table3():
conn=sqlite3.connect(database_file)
c=conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS table3 (
model TEXT NOT NULL,
precision NUMERIC,
recall NUMERIC ,
specificity NUMERIC ,
auc NUMERIC,
details TEXT NOT NULL ,
other TEXT,
PRIMARY KEY (model,details)
)
""")
conn.commit()
conn.close()
def save_table3(model,precision,recall,specificity,auc,details,other=None):
create_table3()
conn = sqlite3.connect(database_file)
c = conn.cursor()
c.execute("""
INSERT OR REPLACE INTO table3 (
model,
precision,
recall,
specificity,
auc,
details,
other
) VALUES(?,?,?,?,?,?,?)
""", (model, precision, recall,specificity, auc, details, other))
conn.commit()
conn.close()
def create_regrssion_table():
conn=sqlite3.connect(database_file)
c=conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS regression (
model TEXT NOT NULL,
rmse NUMERIC,
r2 NUMERIC ,
details TEXT NOT NULL ,
other TEXT,
PRIMARY KEY (model,details)
)
""")
conn.commit()
conn.close()
def save_regression(model,rmse,r2,details,other=None):
create_regrssion_table()
conn = sqlite3.connect(database_file)
c = conn.cursor()
c.execute("""
INSERT OR REPLACE INTO regression (
model,
rmse,
r2,
details,
other
) VALUES(?,?,?,?,?)
""", (model, rmse, r2, details, other))
conn.commit()
conn.close()
def admission_confusion_matrix(ytrue,ypred,labels=['Not admitted','Admitted']):
cf_matrix=confusion_matrix(ytrue,ypred,labels=[0,1])
cf_matrix_norm=cf_matrix/np.reshape(cf_matrix.sum(axis=1),[-1,1])
fig,ax=plt.subplots(1,1,figsize=(10,8))
img=ax.matshow(cf_matrix_norm,cmap=plt.cm.get_cmap("Greys"),vmin=0,vmax=1)
for i in [0,1,]:
for j in [0,1,]:
ax.text(j,i,"%d(%.2f)" % (cf_matrix[i,j],cf_matrix_norm[i,j]),ha='center',fontsize=16,weight='bold',color='blue')
ax.set_xticks([0,1,])
ax.set_xticklabels(labels,rotation=45,ha="left",rotation_mode="anchor",fontsize=12)
# ax.setp(ax.get_xticklabels(), rotation=45, ha="right",
# rotation_mode="anchor")
ax.set_yticks([0, 1,])
ax.set_yticklabels(labels,rotation=45,fontsize=12)
# ax.set_ylim(4.5,-0.5)
ax.set_xlabel("Predicted Class",fontsize=12,fontweight='bold')
ax.set_ylabel("Target Class",fontsize=12,fontweight='bold')
fig.colorbar(img)
fig.tight_layout()
plt.show()
return fig
def admission_distplot(samples,ytrue,ypred):
indices,axes,labels={},{},{}
indices['tp']=np.where((ytrue==1) & (ypred==1))[0]
indices['tn'] = np.where((ytrue == 0) & (ypred == 0))[0]
indices['fp'] = np.where((ytrue == 0) & (ypred == 1))[0]
indices['fn'] = np.where((ytrue == 1) & (ypred == 0))[0]
axes['tn']=0
axes['fp']=1
axes['fn']=2
axes['tp']=3
labels['tn']="True negative"
labels['fp'] = "False positive"
labels['fn'] = "False negative"
labels['tp'] = "True positive"
fig,axs=plt.subplots(1,4,sharex=True,figsize=(12,4))
for p in ['tn','fp','fn','tp']:
axs[axes[p]].set_xlabel("P")
axs[axes[p]].set_ylabel("Density")
axs[axes[p]].set_ylim((0,6))
axs[axes[p]].set_title(labels[p])
for i in indices[p]:
sns.kdeplot(samples[:,i].reshape(-1),ax=axs[axes[p]])
plt.show()
return fig