Recommended Free Tools
A NumPy 3D array has three axes: its shape tells you how long each axis is, indexing selects elements or slices, and reductions such as sum collapse an axis. The reliable way to reason about it is to read the shape tuple from left to right, then check the resulting .shape after an operation.
What does a 3D array’s shape mean?
Start with a small array whose dimensions are easy to inspect:
import numpy as np
x = np.arange(24).reshape(2, 3, 4)
print(x.shape) # (2, 3, 4)
print(x.ndim) # 3
print(x.size) # 24
x.shape is a tuple giving the length of each dimension, in order. Here, axis 0 has length 2, axis 1 has length 3, and axis 2 has length 4. x.ndim counts the axes; x.size is the total number of elements, or 2 × 3 × 4. See NumPy’s ndarray reference.
For this example, you could call the dimensions groups, rows, and columns: there are two groups, each containing three rows of four values. Those labels are only an interpretation of this particular arrangement. NumPy does not assign universal meanings such as “depth,” “height,” or “width” to axes; the data convention determines what each axis represents.
#1 Best Overall
How do you select values and slices?
Use one index for each axis to select a single value. Python indexing starts at zero, so the first element along an axis has index 0. A negative index counts backward from the end.
x[1, 2, 3] # scalar: group 1, row 2, column 3
x[0, 0, 0] # first value: 0
x[-1, -1, -1] # last value: 23
Use a colon to keep a range of values along an axis. An integer index removes that axis from the result; a slice keeps it, even if the slice selects just one position.
x[1, :, :] # shape (3, 4)
x[:, 1, :] # shape (2, 4)
x[:, :, 1:3] # shape (2, 3, 2)
x[0:1, :, :] # shape (1, 3, 4)
For example, x[1, :, :] selects one group. Its first dimension disappears because axis 0 was selected with an integer. By contrast, x[0:1, :, :] uses a slice, so axis 0 remains with length 1. NumPy also treats omitted trailing dimensions as full slices, which means x[1] and x[1, :, :] select the same plane. The indexing documentation describes these indexing rules.
Rank #2
Basic slices are generally views into the original array rather than independent copies. If you change a value in a slice, the corresponding value in x may change too. Call .copy() when you need separate data:
plane = x[1, :, :].copy()
What does axis mean in a reduction?
For a reduction such as sum, the axis argument identifies the dimension to collapse. The other dimensions remain, in their existing order. With x.shape == (2, 3, 4):
| Expression | Result shape | What is collapsed |
|---|---|---|
x.sum(axis=0) |
(3, 4) |
Axis 0, length 2 |
x.sum(axis=1) |
(2, 4) |
Axis 1, length 3 |
x.sum(axis=2) |
(2, 3) |
Axis 2, length 4 |
x.sum() |
Scalar | All axes |
A dependable rule is “collapse axis N,” not “sum the rows” or “sum the depth.” Those informal descriptions can be ambiguous unless you have already defined what the axes represent. NumPy’s reductions guide explains how an integer axis reduction operates across subarrays along that dimension.
If you are unsure what remains, inspect the result directly:
result = x.sum(axis=1)
print(result.shape) # (2, 4)
How are reshape and axis movement different?
These operations all affect dimensions, but they do different jobs. Choose one according to whether you want to regroup values, reorder existing axes, or add or remove a dimension.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11| Goal | Operation | Effect on shape |
|---|---|---|
| Regroup the same elements | reshape |
Uses a new shape with the same element count. |
| Reorder every axis | transpose |
Permutes the shape tuple according to the specified axis order. |
| Move or swap selected axes | moveaxis or swapaxes |
Reorders selected dimensions. |
| Insert a length-one dimension | None, np.newaxis, or expand_dims |
Adds an axis of length 1. |
| Remove length-one dimensions | squeeze |
Drops axes whose length is 1; specify an axis when you need precise control. |
Reshape to regroup
reshape changes how the existing elements are grouped into dimensions. The target shape must have the same total number of elements; it does not swap axes or create additional values.
x.reshape(6, 4).shape # (6, 4)
Transpose or move axes to reorder
transpose changes axis order. Its arguments specify the old axes in the order they should appear in the result:
x.transpose(2, 0, 1).shape # (4, 2, 3)
moveaxis can be more direct when only one axis needs to move:
np.moveaxis(x, 0, -1).shape # (3, 4, 2)
These operations reorder existing dimensions rather than regrouping elements as reshape does. A transpose result can be a view, so edits may be reflected in the original array. NumPy lists these operations in its array manipulation reference.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsBest Value
Insert an axis when dimensions need to line up
A singleton dimension has length 1. Inserting one can make an array’s dimensions compatible with a later operation:
x[:, None, :, :].shape # (2, 1, 3, 4)
None is an alias for np.newaxis in indexing. Use np.expand_dims when you want to state the axis position explicitly; use np.squeeze to remove singleton dimensions when appropriate.
How can you get more confident with 3D indexing?
- Write down the shape and label its axes using the meaning of your data, rather than assuming the labels from another example.
- For indexing, count how many axes use integers and how many use slices. Integer-indexed axes disappear; sliced axes remain.
- For a reduction, identify the collapsed axis and verify the shape of the result.
- Print
.shapeafter unfamiliar indexing, reduction, or axis movement. - Use
.copy()if a basic slice or transpose must be independent of its source.
Advanced integer and boolean indexing have different shape and copy behavior from basic slicing. Learn those cases separately once integer indexing and slices are predictable; NumPy covers them in its indexing reference.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




