AxisError axis 1 is out of bounds for array of dimension 1 when calculating accuracy of classes
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This NumPy error means your code tried to use a second dimension on data that only has one dimension. In classification code, that usually happens when you assume predictions are shaped like a class-score matrix even though the model already returned a flat array of class labels.
Why axis=1 Fails on a One-Dimensional Array
NumPy arrays are zero-indexed by dimension. A one-dimensional array has only axis 0.
The shape is (4,), so there is no axis 1 to operate on.
That is why this fails:
argmax(axis=1) is valid only when the array actually has a second dimension.
Distinguish Class Labels From Class Scores
The correct fix depends on what the prediction array represents.
If your model returns class scores or probabilities for multiple classes, the shape is usually (n_samples, n_classes). Then argmax(axis=1) makes sense.
If your model already returns class ids, there is nothing to reduce across columns. Just compare the predicted labels directly to the true labels.
That is the correct pattern for one-dimensional predicted-label arrays.
Binary Classification Often Causes This
Binary models frequently return one score per sample instead of two columns. That shape might be (n_samples,) or (n_samples, 1), depending on the library.
For a flat score array, use a threshold rather than argmax.
That is usually the right fix when switching from a softmax output to a sigmoid-style binary output.
Print the Shape Before Writing the Metric Code
The fastest debugging move is to inspect the array before deciding how to process it.
Those three lines usually tell you whether you are dealing with:
- one-dimensional class labels
- two-dimensional class scores
- a squeezed array that lost a dimension unexpectedly
Once the real shape is visible, the choice between direct comparison, thresholding, and argmax becomes straightforward.
Watch Out for squeeze() and One-Hot Labels
Two extra mistakes appear often:
- '
squeeze()removes a singleton dimension and turns(n_samples, 1)into(n_samples,)' - '
y_truemay be one-hot encoded whiley_predis integer class ids'
If y_true is one-hot encoded, convert it before comparing.
The important part is to make both arrays represent labels in the same format before computing accuracy.
Common Pitfalls
The biggest mistake is assuming all prediction arrays are two-dimensional. Different models return different shapes.
Another issue is using argmax(axis=1) on binary outputs that contain only one score per sample.
A third problem is debugging the metric line without first printing shape and ndim, which hides the real cause.
Summary
- '
axis=1is invalid on a one-dimensional array.' - Use
argmax(axis=1)only for class-score arrays shaped like(n_samples, n_classes). - If predictions are already class labels, compare them directly to
y_true. - For binary score outputs, threshold the scores instead of using
argmax. - Print the array shape first whenever accuracy code fails unexpectedly.

