杰瑞科技汇

python ndarray size

Of course! Let's break down how to work with the "size" of a NumPy ndarray. The term "size" can be a bit ambiguous, so it's important to know which specific property you need.

python ndarray size-图1
(图片来源网络,侵删)

There are three main attributes to understand for getting information about an array's dimensions:

  1. ndarray.size: The total number of elements in the array.
  2. ndarray.shape: A tuple representing the size of the array in each dimension.
  3. ndarray.ndim: The number of dimensions (or axes) of the array.

ndarray.size (Total Number of Elements)

This is the most direct answer to "what is the size of an ndarray". It gives you the total count of items in the array, regardless of how many dimensions it has.

How to use it: Access it as a property of the array object.

Example:

python ndarray size-图2
(图片来源网络,侵删)
import numpy as np
# Create a 1D array
a = np.array([1, 2, 3, 4, 5])
print(f"Array a: {a}")
print(f"a.size: {a.size}")  # Output: 5
# Create a 2D array (a matrix)
b = np.array([[1, 2, 3], [4, 5, 6]])
print(f"\nArray b:\n{b}")
print(f"b.size: {b.size}")  # Output: 6 (2 rows * 3 columns)
# Create a 3D array
c = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(f"\nArray c:\n{c}")
print(f"c.size: {c.size}")  # Output: 8 (2 layers * 2 rows * 2 columns)

Output:

Array a: [1 2 3 4 5]
a.size: 5
Array b:
[[1 2 3]
 [4 5 6]]
b.size: 6
Array c:
[[[1 2]
  [3 4]]
 [[5 6]
  [7 8]]]
c.size: 8

ndarray.shape (Dimensions)

This attribute is a tuple that contains the size of the array along each axis. It's extremely useful for understanding the array's structure.

  • For a 1D array, the shape is (n,), where n is the number of elements.
  • For a 2D array, the shape is (rows, columns).
  • For a 3D array, the shape is (layers, rows, columns).

How to use it: Access it as a property of the array object.

Example:

python ndarray size-图3
(图片来源网络,侵删)
import numpy as np
# 1D array
a = np.array([10, 20, 30])
print(f"Array a: {a}")
print(f"a.shape: {a.shape}")  # Output: (3,)
# 2D array
b = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(f"\nArray b:\n{b}")
print(f"b.shape: {b.shape}")  # Output: (2, 4) -> 2 rows, 4 columns
# 3D array
c = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(f"\nArray c:\n{c}")
print(f"c.shape: {c.shape}")  # Output: (2, 2, 2) -> 2 layers, 2 rows, 2 columns

Output:

Array a: [10 20 30]
a.shape: (3,)
Array b:
[[1 2 3 4]
 [5 6 7 8]]
b.shape: (2, 4)
Array c:
[[[1 2]
  [3 4]]
 [[5 6]
  [7 8]]]
c.shape: (2, 2, 2)

ndarray.ndim (Number of Dimensions)

This attribute simply tells you how many axes the array has. It's an integer.

  • A scalar (a single number) has ndim = 0.
  • A vector (1D array) has ndim = 1.
  • A matrix (2D array) has ndim = 2.
  • And so on.

How to use it: Access it as a property of the array object.

Example:

import numpy as np
# Scalar
scalar = np.array(42)
print(f"Scalar: {scalar}")
print(f"scalar.ndim: {scalar.ndim}") # Output: 0
# 1D array
a = np.array([1, 2, 3])
print(f"\nArray a: {a}")
print(f"a.ndim: {a.ndim}") # Output: 1
# 2D array
b = np.array([[1, 2], [3, 4]])
print(f"\nArray b:\n{b}")
print(f"b.ndim: {b.ndim}") # Output: 2
# 3D array
c = np.array([[[1, 2, 3]]])
print(f"\nArray c:\n{c}")
print(f"c.ndim: {c.ndim}") # Output: 3

Output:

Scalar: 42
scalar.ndim: 0
Array a: [1 2 3]
a.ndim: 1
Array b:
[[1 2]
 [3 4]]
b.ndim: 2
Array c: [[[1 2 3]]]
c.ndim: 3

Summary Table

Attribute Description Type Example for np.array([[1,2],[3,4]])
ndarray.size Total number of elements in the array. int 4
ndarray.shape A tuple with the size of the array in each dimension. tuple (2, 2)
ndarray.ndim The number of dimensions (axes) of the array. int 2

The Relationship Between Them

These three attributes are directly related. You can calculate one from the others.

  • size = shape[0] * shape[1] * ... * shape[n-1]
  • len(shape) == ndim

For our 2D array b with shape = (2, 4) and ndim = 2: b.size is 2 * 4 = 8.

How to Get the Size of a Specific Dimension?

If you want the size along a specific axis (e.g., the number of rows or columns), you use the .shape tuple.

  • To get the number of rows (axis 0): array.shape[0]
  • To get the number of columns (axis 1): array.shape[1]
import numpy as np
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])
num_rows = matrix.shape[0]
num_cols = matrix.shape[1]
print(f"Matrix:\n{matrix}")
print(f"Number of rows: {num_rows}")  # Output: 3
print(f"Number of columns: {num_cols}") # Output: 3

The Built-in len() Function

It's also important to know how the built-in Python len() function works with NumPy arrays. By default, len() returns the size of the first dimension (axis 0).

import numpy as np
# 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(f"Array:\n{arr}")
print(f"len(arr): {len(arr)}")      # Output: 2 (number of rows)
print(f"arr.shape[0]: {arr.shape[0]}") # Same as len(arr)
# 1D array
arr_1d = np.array([1, 2, 3])
print(f"\nlen(arr_1d): {len(arr_1d)}") # Output: 3 (number of elements)
print(f"arr_1d.size: {arr_1d.size}")   # Also 3, but conceptually different

Key Takeaway

  • Use .size when you need the total count of elements.
  • Use .shape to understand the structure and dimensions of the array.
  • Use .ndim to get the number of dimensions.
  • Use len(array) or array.shape[0] to get the size of the first dimension (e.g., number of rows in a matrix).
分享:
扫描分享到社交APP
上一篇
下一篇