Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

NumPy 3D Arrays in Python: Shape, Indexing, Slicing, and Axes

A practical guide to reading a NumPy 3D shape, indexing values and slices, understanding axis reductions, and choosing the right dimension operation.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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 .shape after 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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.