-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.py
65 lines (47 loc) · 1.65 KB
/
functions.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
import numpy as np
def train_test_split(X,y,ratio=0.2,shuffle = True):
m = X.shape[0]
if shuffle==True:
index = np.arange(m)
np.random.shuffle(index)
X = X[index]
y = y[index]
split = np.round((1-ratio)*m).astype(int)
X_train = X[:split]
y_train = y[:split]
X_test = X[split:]
y_test = y[split:]
return X_train, y_train, X_test, y_test
def one_hot(y, nclass):
n = y.size
Y = np.zeros((n,nclass))
Y[np.arange(n),y] = 1
return Y
def he_normal(out_shape, fan_in):
return np.random.randn(*out_shape) * np.sqrt(2./fan_in)
def pickle_compress(file_path, data=None, operation='load'):
import bz2
import pickle
import _pickle as cPickle
a = None
if operation != 'load':
with bz2.BZ2File(file_path, 'wb') as f:
cPickle.dump(data, f)
else:
with bz2.BZ2File(file_path, 'rb') as f:
a = cPickle.load(f)
return a
def plot_confusion_matrix(y_actu, y_pred, title='Confusion matrix'):
cmap=plt.cm.gray_r
import matplotlib.pyplot as plt
import pandas as pd
df_confusion = pd.crosstab(y_actu, y_pred.reshape(y_pred.shape[0],), rownames=['Actual'], colnames=['Predicted'], margins=True)
df_conf_norm = df_confusion / df_confusion.sum(axis=1)
plt.matshow(df_confusion, cmap=cmap) # imshow
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(df_confusion.columns))
plt.xticks(tick_marks, df_confusion.columns, rotation=45)
plt.yticks(tick_marks, df_confusion.index)
plt.ylabel(df_confusion.index.name)
plt.xlabel(df_confusion.columns.name)