Multidimensional numeric storage
Representing numeric data with multiple dimensions while keeping one consistent element type.
NumPy · Class
numpy.ndarray is a multidimensional, homogeneous array whose elements have a fixed-size representation described by a dtype. Its constructor exposes shape, storage, strides, offset, and memory-order controls, but ordinary arrays should usually be created with array, zeros, or empty.
An ndarray stores a multidimensional collection whose elements share one fixed-size data type. Its shape describes dimensions, while dtype describes each element’s representation; although the class can be instantiated directly with memory-layout controls, ordinary arrays are generally created with array, zeros, or empty.
numpy.ndarray(shape, dtype=np.float64, buffer=None, offset=0, strides=None, order=None)| Name | Type | Description |
|---|---|---|
shape | tuple of ints | Shape of created array. |
dtype | data-type, optional | Any object that can be interpreted as a NumPy data type. |
buffer | object exposing buffer interface, optional | Used to fill the array with data. |
offset | int, optional | Offset of array data in buffer. |
strides | tuple of ints, optional | Strides of data in memory. |
order | {'C', 'F'}, optional | Row-major (C-style) or column-major (Fortran-style) order. |
An ndarray object representing a multidimensional, homogeneous array of fixed-size items.
import numpy as np
values = np.array([[1, 2], [3, 4]], dtype=np.int64)
print(values.shape)
print(values.dtype)
print(values[0, 1])Creates a two-dimensional homogeneous array with an explicit integer data type, then inspects its shape, data type, and an indexed element.
Representing numeric data with multiple dimensions while keeping one consistent element type.
Inspecting or controlling an array’s memory representation when interoperating with buffer-backed data.
Choosing C-style or Fortran-style layout for algorithms or interoperability that depend on memory order.
ndarray is NumPy’s multidimensional, homogeneous array object. Homogeneous means every element has the same fixed-size representation, described by the associated dtype.
Follow-up: How would you inspect those properties? Use the array’s shape for its dimensions and dtype for the element representation.
The constructor accepts shape, dtype, buffer, offset, strides, and order. Shape defines the array dimensions; dtype defines the element type, while the remaining arguments provide low-level storage and layout control.
Follow-up: Why avoid direct construction for routine arrays? The documentation recommends array, zeros, or empty instead.
dtype can be any object interpretable as a NumPy data type. order selects C-style row-major or Fortran-style column-major layout, and strides describe data spacing in memory.
Follow-up: What role does offset play? It specifies the starting offset of array data within the supplied buffer.
numpy.arraynumpy.zerosnumpy.emptynumpy.dtype