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

In NumPy, dimensions are called axes. Check an array’s shape to see the length of each axis and ndim to count them; then choose the operation that matches your goal: reshape elements, insert or remove an axis, reorder axes, or make shapes compatible through broadcasting.

Inspect an array’s dimensions

Start by checking shape, ndim, and often size. These tell you the array’s structure, the number of axes, and the total number of elements.

import numpy as np

x = np.array([1, 2, 3])
print(x.shape)  # (3,)
print(x.ndim)   # 1
print(x.size)   # 3

A shape of (2, 3) means two axes: the first has length 2 and the second length 3. A one-dimensional array with shape (3,) is not automatically a row or column vector. Those have distinct two-dimensional shapes, (1, 3) and (3, 1).

Choose the operation that matches the change you need

Goal Use Effect
Change how elements are grouped reshape Changes the shape while retaining a compatible number of elements.
Add a length-one axis np.newaxis or np.expand_dims Inserts a singleton dimension at a chosen position.
Remove length-one axes np.squeeze Removes singleton dimensions; specify an axis to be precise.
Change the order of axes transpose, moveaxis, or swapaxes Reorders existing axes rather than regrouping elements.
Perform an elementwise operation on compatible shapes Broadcasting Aligns dimensions from the right under NumPy’s equal-or-one rule.

Reshape to change element grouping

Use reshape when you want a different arrangement of the same elements. The requested shape must be compatible with the array’s element count. Use -1 for one dimension that NumPy should infer.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
x = np.arange(6)
matrix = x.reshape(2, 3)  # shape (2, 3)
flat = matrix.reshape(-1)  # shape (6,)

For example, reshape(2, 3) groups six elements into two rows of three. It does not mean “swap the axes”; use a transpose or axis-moving operation for that. A reshape result does not change the original array’s shape in place.

Insert a dimension with newaxis or expand_dims

Insert a singleton axis when a later operation needs a row-shaped or column-shaped array. np.newaxis is the same object as None in indexing.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
x = np.array([1, 2, 3])
row = x[np.newaxis, :]       # shape (1, 3)
column = x[:, np.newaxis]    # shape (3, 1)
column2 = np.expand_dims(x, axis=1)  # shape (3, 1)

np.expand_dims inserts an axis at the specified position and returns a view. It accepts one axis or a tuple of axes. Supply valid axis positions rather than relying on out-of-range behavior, which NumPy documents as deprecated.

Remove singleton dimensions with squeeze

np.squeeze(a) removes all axes of length one. If only one particular axis should disappear, specify it with axis; NumPy will raise an error if that axis is not length one.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
row = np.array([[1, 2, 3]])
vector = np.squeeze(row, axis=0)  # shape (3,)

Be deliberate about squeezing: removing every singleton axis can discard a dimension that downstream code expects.

Reorder axes with transpose or axis-moving functions

Transpose changes the order of existing axes. For a two-dimensional array, .T swaps the two axes. For arrays with more dimensions, provide an intentional axis order to transpose, or use moveaxis or swapaxes when those operations express the change more clearly.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
matrix = np.array([[1, 2, 3], [4, 5, 6]])
transposed = matrix.T  # shape (3, 2)

Use an axis permutation when the data’s existing dimensions are in the wrong order. Reshape changes grouping, so it is not a substitute for permuting axes.

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

Use broadcasting to match shapes in operations

Broadcasting lets elementwise operations work with certain differently shaped arrays without first manually repeating values. NumPy compares dimensions from right to left. Each aligned pair is compatible when the lengths are equal or at least one length is 1; missing leading dimensions are treated as length 1. If a pair meets neither condition, the operation raises a ValueError.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

Apply one scale per image channel

An image with shape (height, width, 3) can be multiplied elementwise by channel scales shaped (3,): the trailing dimensions match, and the shorter shape is treated as if it had leading singleton dimensions.

Make row and column axes explicit for pairwise results

To add each value in a four-element vector to each value in a three-element vector, insert a singleton axis in the first vector. The result has shape (4, 3).

a = np.array([0, 10, 20, 30])
b = np.array([1, 2, 3])
outer_sum = a[:, np.newaxis] + b  # shape (4, 3)

Broadcasting is designed to avoid needless copies, but the resulting array can be much larger than either input. Work out the expected shape and element count before creating an outer-style result, especially when the input arrays are large.

Diagnose common dimension mistakes

  • Confusing ndim with shape: ndim is a single count; shape lists the length of every axis.
  • Assuming a 1-D array is a row or column: shape (n,) has one axis. Insert an axis to get (1, n) or (n, 1) when that distinction matters.
  • Using reshape to swap dimensions: transpose or move an axis instead.
  • Squeezing more than intended: pass a specific axis when only one singleton dimension should be removed.
  • Getting a broadcasting error: compare shapes from the right and check each aligned pair against the equal-or-one rule.
  • Creating an unexpectedly large result: calculate the output shape and element count before broadcasting.
  • Passing an invalid axis to expand_dims: use a valid position rather than depending on deprecated out-of-range behavior.

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.