Python
NumPy
AxisError
Array Indexing
Debugging

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.

python
1import numpy as np
2
3labels = np.array([1, 0, 1, 1])
4print(labels.ndim)
5print(labels.shape)

The shape is (4,), so there is no axis 1 to operate on.

That is why this fails:

python
1import numpy as np
2
3labels = np.array([1, 0, 1])
4print(np.argmax(labels, axis=1))

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.

python
1import numpy as np
2
3scores = np.array([
4    [0.2, 0.8],
5    [0.9, 0.1],
6    [0.4, 0.6],
7])
8
9predicted_classes = np.argmax(scores, axis=1)
10print(predicted_classes)

If your model already returns class ids, there is nothing to reduce across columns. Just compare the predicted labels directly to the true labels.

python
1import numpy as np
2
3y_pred = np.array([1, 0, 1, 1])
4y_true = np.array([1, 0, 0, 1])
5
6accuracy = (y_pred == y_true).mean()
7print(accuracy)

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.

python
1import numpy as np
2
3scores = np.array([0.91, 0.20, 0.73, 0.48])
4y_true = np.array([1, 0, 1, 0])
5
6y_pred = (scores >= 0.5).astype(int)
7accuracy = (y_pred == y_true).mean()
8print(accuracy)

That is usually the right fix when switching from a softmax output to a sigmoid-style binary output.

The fastest debugging move is to inspect the array before deciding how to process it.

python
print(y_pred.shape)
print(y_pred.ndim)
print(y_pred[:5])

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_true may be one-hot encoded while y_pred is integer class ids'

If y_true is one-hot encoded, convert it before comparing.

python
1import numpy as np
2
3y_true_one_hot = np.array([
4    [0, 1],
5    [1, 0],
6    [0, 1],
7])
8y_pred = np.array([1, 0, 1])
9
10y_true = np.argmax(y_true_one_hot, axis=1)
11print((y_pred == y_true).mean())

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=1 is 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.

Course illustration
Course illustration

All Rights Reserved.