Reputation: 1273
I have a working neural network loop so I can run neural networks using a predetermined number of nodes in my hidden layer ('nodes_list'). I then calculate the area under the ROC curve for each number of nodes and put this in a list ('roc_outcomes') for plotting purposes. However, I would like to loop over this loop 5 times to get an average area under the ROC curve for each of the three models (model 1: 20 nodes in hidden layer, model 2: 28 nodes in hidden layer, model 3: 38 nodes in hidden layer). This works fine when I am only trying it on one model, but when I iterate over more than one model instead of iterating over model 1 5 times, then model 2 5 times, then model 3 5 times....it iterates over model 1, then model 2, then model 3, and it does this 5 times. The purpose of this nested loop is for me to iterate over each neural network model 5 times, put the area under the ROC curve for each iteration into a list, calculate a mean of that list, and put the mean into a new list. Ultimately, I would like to have a list of three numbers (1 for each model) that is the mean area under the ROC curve for the 5 iterations of that model. Hopefully, I explained this well. Please ask for any clarification.
Here is my code:
nodes_list = [20, 28, 38] # list with number of nodes in hidden layer per model
roc_outcomes = [] # list of ROC AUC
for i in np.arange(1,6):
for nodes in nodes_list:
# Add first layer
model.add(Dense(units=n_cols, activation='relu', input_shape=(n_cols,)))
# Add hidden layer
model.add(Dense(units=nodes, activation='relu'))
# Add output layer
model.add(Dense(units=2, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit model
model.fit(X, y, validation_split=0.33, epochs=epochs, callbacks=early_stopping_monitor, verbose=True)
# Get predicted probabilities
pred_prob = model.predict_proba(X)[:,1]
# Calculate area under the curve (logit_roc_auc)
logit_roc_auc = roc_auc_score(y[:,1], pred_prob)
# Append roc scores to the roc_outcomes list
roc_outcomes.append(logit_roc_auc)
# Get the mean of that list
mean_roc = np.mean(roc_outcomes)
# Append to another list
mean_roc_outcomes = []
mean_roc_outcomes.append(mean_roc)
Upvotes: 2
Views: 1588
Reputation: 393
Construct your loops like this:
for nodes in node_list:
for i in range(0,5):
#do your stuff
example:
myList = ['a', 'b', 'c']
for item in myList:
for i in range(0,5):
print(item, end=", ")
output:
a, a, a, a, a, b, b, b, b, b, c, c, c, c, c,
Upvotes: 2