liveBook · Manning

Balanced accuracy

As explained in Evolutionary Deep Learning: Genetic algorithms and neural networks

Balanced accuracy is the average of per-class recall values, giving equal weight to each class regardless of its frequency.

Balanced accuracy averages per-class recall, giving each class equal weight. It is useful when a random evaluation batch may contain uneven class representation and ordinary accuracy could be biased. section 10.4

A practical evaluation flow uses balanced accuracy as the genome’s fitness:

  • Sample a random batch of 256 images and labels. section 10.4
  • Flatten each 28×28 image into 784 inputs and activate the network. section 10.4
  • Apply SoftMax and argmax to collect predicted classes, then score predictions against labels. section 10.4

NEAT genome fitness

Use sklearn.metrics.balanced_accuracy_score(ys, yis) after collecting true labels and predicted classes; assign the result to genome.fitness. section 10.4

Listing 10.17 EDL_10_4_NEAT_Images.ipynb: Evaluating a genome fitness
from sklearn.metrics import balanced_accuracy_score    
yis = []
for x, y in zip(xs,ys):                      #1
  x = np.reshape(x, (784,))                  #2
  output = net.activate(x)                   #3
  class_ = softmax(output)                   #3
  yis.append(np.argmax(class_))              #3
print(ys, yis)  
fitness = balanced_accuracy_score(ys, yis)   #4
print(fitness)

Inspect the compact implementation that converts network outputs into class predictions and computes the score. section 10.4

How Evolutionary Deep Learning: Genetic algorithms and neural networks covers it

How deep it goes
Defines the balancing rationale and demonstrates a complete scoring call, but does not derive the metric mathematically.
Examples
Uses MNIST-like 28×28 images, random batches of 256, and NEAT genome predictions.
What it assumes
Assumes familiarity with image classification, network activation, SoftMax, and predicted class labels.
What you can do after
Apply it via scikit-learn to assign balanced classification fitness to a genome.
How it connects
Connects classification, recall, SoftMax, argmax, random batching, and evolutionary fitness evaluation.

Also explained in

Compare how 2 books explain balanced accuracy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Evolutionary Deep Learning: Genetic algorithms and neural networks. The section links point to the parts of the book it draws on.