liveBook · Manning

Class imbalance

As explained in Graph Neural Networks in Action

Class imbalance is a property of a classification dataset in which the classes contain substantially different numbers of examples.

Class imbalance occurs when some classification labels have many more examples than others. In a graph, count the nodes assigned to each class before trusting an overall performance score: a model can appear strong while doing poorly on a rare class that matters. section 3.5 section 4.4.2

Spot it in node labels

The Amazon Products graph has 47 target categories. Books has 668,950 nodes, while Digital Music has six and one unknown category has just one. The mean is 52,107 nodes per category, but the median is 3,653. A class-count plot—or the class-count sidebar of a confusion matrix—makes this disparity visible. section 3.5 section 3.3.3

Imbalance becomes especially consequential when the minority class is the point of the task. In a fraud-detection graph, only 14.5% of nodes are labeled fraudulent. AUC alone can mask weak minority-class results, so inspect precision, recall, and F1 together. In one comparison, GATv2 reached recall of 1 but precision of 0.145 and F1 of 0.254; XGBoost reached precision of 0.855 and F1 of 0.734. section 4.4.2

Practical responses include:

  • Reshuffle training batches to include equal portions of majority- and minority-class nodes. A BalancedNodeSampler example samples majority indices randomly and minority indices with replacement. section 4.4.2
  • Consider over-sampling, under-sampling, or cost-sensitive learning. GraphSMOTE extends synthetic minority over-sampling by generating nodes and edges. section 4.4.2
  • Check the graph-specific costs: a change in samples must account for topology, node dependencies, and scalability. Also inspect how any selected subgraph represents the available classes. section 4.4.2 section 3.3.3
Table 4.10 Comparing F1, precision, recall, and AUC of the GATv2 model trained with a class reshuffling method
Metric
Value
Mean validation F1 score
0.809
Mean validation precision
0.878
Mean validation recall
0.781
Mean validation AUC
0.914

The reported validation precision, recall, F1, and AUC show what to inspect after class reshuffling, rather than relying on one score. section 4.4.2

How Graph Neural Networks in Action covers it

How deep it goes
Moves from class counts and diagnostic metrics to balanced-batch sampling, while outlining other remedies without implementing them.
Examples
Uses Amazon Products category counts, a fraud-detection GATv2–XGBoost comparison, and a balanced node sampler.
What it assumes
Assumes familiarity with node labels and classification metrics; illustrates imbalance through category counts and fraud prevalence.
What you can do after
Diagnose/debug with class counts and minority-class metrics, then apply balanced-batch sampling while checking graph-specific trade-offs.
How it connects
Links class imbalance to node classification, confusion matrices, precision, recall, F1, AUC, resampling, and graph topology.

Also explained in

Compare how 11 books explain class imbalance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Neural Networks in Action. The section links point to the parts of the book it draws on.