Commit 8bd82e0e by Pamela Osuna

didnt change

parent 54d3d4ff
...@@ -69,7 +69,7 @@ def find_classes(x): ...@@ -69,7 +69,7 @@ def find_classes(x):
Parameters Parameters
---------- ----------
x : iterable x : iterable
heterogeneous memberships. heterogeneous memberships.
Returns Returns
...@@ -141,29 +141,29 @@ def balance(X,y,method): ...@@ -141,29 +141,29 @@ def balance(X,y,method):
elif method == 'undersampling': elif method == 'undersampling':
print('UNDERSAMPLING') print('UNDERSAMPLING')
return undesample(X,y),None return undesample(X,y),None
class CNN_Antifrag: class CNN_Antifrag:
""" """
Convolutional Neural Network Convolutional Neural Network
For predicting Robustness and Evolvability For predicting Robustness and Evolvability
Based on antifragility estimations Based on antifragility estimations
""" """
def __init__(self,name='CNN',K = 5,N = 4): def __init__(self,name='CNN',K = 5,N = 4):
""" """
Creates a Convolutional Neural Network Creates a Convolutional Neural Network
Modeling experiment Modeling experiment
Parameters Parameters
---------- ----------
name: str,optional name: str,optional
prefix for history files prefix for history files
The default is 'CNN' The default is 'CNN'
K : int, optional K : int, optional
Number of folds in the cross validation. Number of folds in the cross validation.
The default is 5. The default is 5.
N : int, optional N : int, optional
Number of classes. Number of classes.
The default is 4. The default is 4.
Returns Returns
...@@ -175,10 +175,10 @@ class CNN_Antifrag: ...@@ -175,10 +175,10 @@ class CNN_Antifrag:
self.K = K self.K = K
self.N = N self.N = N
ioff() ioff()
def save_history_plots(self,history,outer,inner=None,name=None): def save_history_plots(self,history,outer,inner=None,name=None):
""" """
Parameters Parameters
---------- ----------
...@@ -221,11 +221,11 @@ class CNN_Antifrag: ...@@ -221,11 +221,11 @@ class CNN_Antifrag:
bs, ep = m.choose_batch_epochs(b,e) bs, ep = m.choose_batch_epochs(b,e)
o = m.choose_balancing_method(o) o = m.choose_balancing_method(o)
K,N = self.K,self.N K,N = self.K,self.N
# random states are defined for reproducibility of results # random states are defined for reproducibility of results
outer = StratifiedKFold(K,shuffle=True,random_state=0xBBBB) outer = StratifiedKFold(K,shuffle=True,random_state=0xBBBB)
inner = StratifiedKFold(K-1,shuffle=True,random_state=0xCCCC) inner = StratifiedKFold(K-1,shuffle=True,random_state=0xCCCC)
total_acc, total_auc = 0,0 total_acc, total_auc = 0,0
# outer loop splits test sets # outer loop splits test sets
# Test data is never used in training or cross validation # Test data is never used in training or cross validation
...@@ -239,45 +239,45 @@ class CNN_Antifrag: ...@@ -239,45 +239,45 @@ class CNN_Antifrag:
for (train_idx,val_idx),j in zip(inner.split(X_D,y_D),range(K-1)): for (train_idx,val_idx),j in zip(inner.split(X_D,y_D),range(K-1)):
X_train,y_train = dims(X_D[train_idx],2),encode(y_D[train_idx]) X_train,y_train = dims(X_D[train_idx],2),encode(y_D[train_idx])
X_val,y_val = dims(X_D[val_idx],2),encode(y_D[val_idx]) X_val,y_val = dims(X_D[val_idx],2),encode(y_D[val_idx])
# creating a new instance of the architecture # creating a new instance of the architecture
model = m.model_architecture(c) model = m.model_architecture(c)
# compile the keras model # compile the keras model
model.compile(loss = 'categorical_crossentropy', model.compile(loss = 'categorical_crossentropy',
optimizer = 'adam', optimizer = 'adam',
metrics = ['accuracy']) metrics = ['accuracy'])
# model training, 3D expansion of the input required # model training, 3D expansion of the input required
# for convolutional layers # for convolutional layers
history = model.fit(X_train, history = model.fit(X_train,
y_train, y_train,
batch_size = bs, batch_size = bs,
epochs = ep, epochs = ep,
verbose = 2, verbose = 2,
class_weight=weights, class_weight=weights,
validation_data = (X_val, y_val)) validation_data = (X_val, y_val))
# save history of accuracy and loss # save history of accuracy and loss
self.save_history_plots(history,i,j) self.save_history_plots(history,i,j)
# calculate accuracy # calculate accuracy
_,accuracy = model.evaluate(X_val, y_val,verbose = 0) _,accuracy = model.evaluate(X_val, y_val,verbose = 0)
total_acc += accuracy total_acc += accuracy
print("t_set = " + str(i) + " v_set = " + str(j)) print("t_set = " + str(i) + " v_set = " + str(j))
print('Test accuracy:', accuracy) print('Test accuracy:', accuracy)
# calculate area under the curve # calculate area under the curve
y_pred = model.predict(X_val, batch_size = bs) y_pred = model.predict(X_val, batch_size = bs)
fpr, tpr, auc = ra.roc_auc(N, y_val, y_pred) fpr, tpr, auc = ra.roc_auc(N, y_val, y_pred)
total_auc += auc total_auc += auc
print("Area under the curve:", auc) print("Area under the curve:", auc)
total_acc = total_acc/(K*(K-1)) total_acc = total_acc/(K*(K-1))
total_auc = total_auc/(K*(K-1)) total_auc = total_auc/(K*(K-1))
print("Average accuracy: ", total_acc) print("Average accuracy: ", total_acc)
print("Average area under the curve: ", total_auc) print("Average area under the curve: ", total_auc)
return total_acc, total_auc return total_acc, total_auc
def run_kfold(self,X, y, params): def run_kfold(self,X, y, params):
...@@ -286,10 +286,10 @@ class CNN_Antifrag: ...@@ -286,10 +286,10 @@ class CNN_Antifrag:
bs, ep = m.choose_batch_epochs(b,e) bs, ep = m.choose_batch_epochs(b,e)
o = m.choose_balancing_method(o) o = m.choose_balancing_method(o)
K,N = self.K,self.N K,N = self.K,self.N
# random states are defined for reproducibility of results # random states are defined for reproducibility of results
kfold = StratifiedKFold(K,shuffle=True,random_state=0xBBBB) kfold = StratifiedKFold(K,shuffle=True,random_state=0xBBBB)
precs_k, recs_k, avgs_k = [], [], [] precs_k, recs_k, avgs_k = [], [], []
total_acc, total_auc = 0,0 total_acc, total_auc = 0,0
...@@ -302,33 +302,33 @@ class CNN_Antifrag: ...@@ -302,33 +302,33 @@ class CNN_Antifrag:
X_train,y_train = dims(X_train,2),encode(y_train) X_train,y_train = dims(X_train,2),encode(y_train)
# test set is left imbalanced, one hot encoding for output # test set is left imbalanced, one hot encoding for output
(X_test,y_test) = dims(X[test_idx],2),encode(y[test_idx]) (X_test,y_test) = dims(X[test_idx],2),encode(y[test_idx])
# creating a new instance of the architecture # creating a new instance of the architecture
model = m.model_architecture(c) model = m.model_architecture(c)
# compile the keras model # compile the keras model
model.compile(loss = 'categorical_crossentropy', model.compile(loss = 'categorical_crossentropy',
optimizer = 'adam', optimizer = 'adam',
metrics = ['accuracy']) metrics = ['accuracy'])
# model training, 3D expansion of the input required # model training, 3D expansion of the input required
# for convolutional layers # for convolutional layers
history = model.fit(X_train, history = model.fit(X_train,
y_train, y_train,
batch_size = bs, batch_size = bs,
epochs = ep, epochs = ep,
verbose = 2, verbose = 2,
class_weight=weights, class_weight=weights,
validation_data = (X_test, y_test)) validation_data = (X_test, y_test))
# save history of accuracy and loss # save history of accuracy and loss
self.save_history_plots(history,i,name='Eval_'+self.name) self.save_history_plots(history,i,name='Eval_'+self.name)
# calculate accuracy # calculate accuracy
_,accuracy = model.evaluate(X_test, y_test,verbose = 0) _,accuracy = model.evaluate(X_test, y_test,verbose = 0)
total_acc += accuracy total_acc += accuracy
print("fold = " + str(i)) print("fold = " + str(i))
print('Test accuracy:', accuracy) print('Test accuracy:', accuracy)
# calculate area under the curve # calculate area under the curve
y_pred = model.predict(X_test, batch_size = bs) y_pred = model.predict(X_test, batch_size = bs)
fpr, tpr, auc = ra.roc_auc(N, y_test, y_pred) fpr, tpr, auc = ra.roc_auc(N, y_test, y_pred)
...@@ -342,14 +342,14 @@ class CNN_Antifrag: ...@@ -342,14 +342,14 @@ class CNN_Antifrag:
else: else:
cm += confusion_matrix( cm += confusion_matrix(
y_test.argmax(axis=1), y_pred.argmax(axis=1)) y_test.argmax(axis=1), y_pred.argmax(axis=1))
#pr curve (contains 4 pr curves: one for each class) #pr curve (contains 4 pr curves: one for each class)
recall, precision, average_prec = create_pr(N, y_test, y_pred) recall, precision, average_prec = create_pr(N, y_test, y_pred)
recs_k.append(recall) recs_k.append(recall)
precs_k.append(precision) precs_k.append(precision)
avgs_k.append(average_prec) avgs_k.append(average_prec)
total_acc = total_acc/K total_acc = total_acc/K
total_auc = total_auc/K total_auc = total_auc/K
cm = cm/K cm = cm/K
...@@ -358,4 +358,3 @@ class CNN_Antifrag: ...@@ -358,4 +358,3 @@ class CNN_Antifrag:
print("Average area under the curve: ", total_auc) print("Average area under the curve: ", total_auc)
return total_acc, total_auc, cm, pr return total_acc, total_auc, cm, pr
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment