Commit 8bd82e0e by Pamela Osuna

didnt change

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