-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstore.py
More file actions
73 lines (61 loc) · 2.15 KB
/
Copy pathstore.py
File metadata and controls
73 lines (61 loc) · 2.15 KB
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
import numpy as np
import os
import itertools
import pandas as pd
from sklearn.externals import joblib
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
class store():
def __init__(self,static_dir,root_path):
self.static_dir=static_dir
self.root_path = root_path
def dump(self,file,string):
url = os.path.join(self.static_dir,string+'.pkl')
joblib.dump(file, url)
def load(self,file):
url = os.path.join(self.static_dir,str(file)+'.pkl')
return joblib.load(url)
def plot_confusion_matrix(self,cm, classes,cnmt_n,
normalize=True,
title='Confusion matrix',
cmap=plt.cm.Blues,):
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
plt.figure(figsize=(7, 7))
np.set_printoptions(precision=2)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
url = os.path.join(self.static_dir,cnmt_n)
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.savefig(url,dpi=100,transparent=True)
plt.close()
def plot_hist(self,data,hist_n):
url = os.path.join(self.static_dir,hist_n)
n_labels= np.unique(np.asarray(data))
np.set_printoptions(precision=2)
plt.figure(figsize=(7, 7))
plt.hist(data,bins =len(n_labels),alpha=1.0,facecolor ='#FF0000',rwidth=0.5,density=True)
plt.xticks(n_labels)
plt.xlabel("Labels")
plt.ylabel("Number of instances")
plt.savefig(url,dpi=100,transparent=True)
plt.close()
def save_pred (self,data,pred):
sh =pred.shape
df =pd.DataFrame({'text':data.tolist(),
'label':pred.T.tolist()})
url = os.path.join('pred.csv')
df.to_csv(url, index=False)