-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeed_network_maker.py
202 lines (160 loc) · 6.24 KB
/
Feed_network_maker.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 12 13:15:11 2018
@author: travisbarton
"""
import numpy as np
import itertools
from sklearn.model_selection import train_test_split
from sklearn import svm
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.callbacks import ModelCheckpoint
from keras.layers.advanced_activations import LeakyReLU, PReLU
import math
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
import spacy
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import confusion_matrix
from random import choice, sample
import warnings
from progress.bar import ChargingBar
warnings.simplefilter(action='ignore', category=FutureWarning)
nlp = spacy.load('en_vectors_web_lg')
RS = 69
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
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")
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.tight_layout()
def Sub_treater(vec, sub):
holder = []
for i in range(len(vec)):
if str(vec[i]) not in str(sub):
#holder.append('Not_{}'.format(sub))
holder.append('other')
else:
holder.append(str(vec[i]))
return(holder)
def Binary_network(X, Y, X_test, label, val_split, nodes, epochs, batch_size, verbose = 0):
model = Sequential()
model.add(Dense(nodes, input_dim = X.shape[1], activation = 'linear'))
model.add(LeakyReLU(alpha=.001))
model.add(Dropout(.4))
model.add(Dense(nodes, activation = 'linear'))
model.add(LeakyReLU(alpha = .001))
model.add(Dense(2, activation = 'softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
#filepath="Best_{}.hdf5".format(label)
#checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1,
# save_best_only=True, mode='max')
#callbacks_list = [checkpoint]
model_history = model.fit(X, Y,
epochs=epochs, batch_size=batch_size,
verbose = verbose, validation_split = val_split)
#physpreds = model.predict(X)
#confm = confusion_matrix(Pred_to_num(Y), Pred_to_num(physpreds))
#plot_confusion_matrix(confm, [0,1], normalize = True, title = "?")
#print(X_test)
if (X_test.ndim == 1):
X_test = np.array([X_test])
return([model.predict(X)[:,0], model.predict(X_test)[:,0]])
'''
def Binary_network(X, Y, X_test, label, val_split, nodes, epochs, batch_size, verbose = 0):
model = Sequential()
model.add(Dense(nodes, input_dim = X.shape[1], activation = 'linear'))
model.add(LeakyReLU(alpha=.01))
model.add(Dropout(.5))
model.add(Dense(nodes, activation = 'linear'))
model.add(LeakyReLU(alpha=.01))
model.add(Dropout(.4))
model.add(Dense(nodes, activation = 'linear'))
model.add(LeakyReLU(alpha=.01))
model.add(Dense(2, activation = 'softmax'))
model.compile(loss='binary_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
#filepath="Best_{}.hdf5".format(label)
#checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1,
# save_best_only=True, mode='max')
#callbacks_list = [checkpoint]
model_history = model.fit(X, Y,
epochs=epochs, batch_size=batch_size,
verbose = verbose, validation_split = val_split)
physpreds = model.predict(X)
confm = confusion_matrix(Pred_to_num(Y), Pred_to_num(physpreds))
plot_confusion_matrix(confm, [0,1], normalize = True, title = "?")
print(X_test)
plt.figure()
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=2)
plt.subplot(211)
plt.plot(model_history.history['acc'])
plt.plot(model_history.history['val_acc'])
plt.title("Accuracy")
plt.xticks(range(20))
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=2)
plt.subplot(212)
plt.title("\nLoss")
plt.plot(model_history.history['loss'])
plt.plot(model_history.history['val_loss'])
plt.xticks(range(20))
if (X_test.ndim == 1):
X_test = np.array([X_test])
return([model.predict(X)[:,0], model.predict(X_test)[:,0]])
'''
def Feed_reduction(X, Y, X_test, labels = None, val_split = .1, nodes = None, epochs = 15, batch_size = 30, verbose = 0):
if nodes == None:
nodes = np.round(X.shape[0]/4)
labels = np.unique(Y)
onehot_encoder = OneHotEncoder(sparse=False)
finaltrain = np.empty([X.shape[0], len(labels)])
finaltest = np.empty([X_test.shape[0], len(labels)])
i = 0
how_many = len(labels)
bar = ChargingBar('Networks Loaded', max=how_many)
for label in labels:
x = X.copy()
y = Y.copy()
x_test = X_test.copy()
y = Sub_treater(y, (label))
y = pd.factorize(y)[0]
y = y.reshape(len(y), 1).astype(int)
y = onehot_encoder.fit_transform(y)
temp = Binary_network(x, y, x_test, label, val_split, nodes, epochs, batch_size, verbose)
finaltrain[:,i] = temp[0]
finaltest[:,i] = temp[1]
bar.next()
i +=1
bar.finish()
return([finaltrain, finaltest])