Build numeric arrays
Converting Python sequences for vectorized numerical operations.
NumPy · Function
Creates an ndarray from an array-like object, such as a nested sequence or compatible array object. Optional arguments control dtype, copying, memory order, subclass handling, and dimensions.
numpy.array converts an array-like input into an ndarray, optionally controlling its data type, memory layout, subclass behavior, and dimensionality. It can accept nested sequences, existing arrays, array-interface objects, or objects implementing __array__.
numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, ndmax=0, like=None)| Name | Type | Description |
|---|---|---|
object | array_like | Input array, array-interface object, object with __array__, or nested sequence. |
dtype | data-type, optional | Desired data type; if omitted, NumPy infers one that represents the values. |
copy | bool, optional | Whether to copy the input; defaults to True. |
order | {'K', 'A', 'C', 'F'}, optional | Memory layout choice: K, A, C, or F; defaults to K. |
subok | bool, optional | Whether to pass through ndarray subclasses; defaults to False. |
ndmin | int, optional | Minimum number of dimensions in the result; defaults to 0. |
ndmax | int, optional | Maximum number of dimensions in the result; defaults to 0. |
like | array_like, optional | Reference object enabling array creation through a compatible array-like implementation; defaults to None. |
An ndarray containing the converted input values.
dtype to request the element type explicitly.ndmin adds dimensions when needed so the result reaches the requested minimum rank.order selects the array's memory-layout preference.K; choose another order when a specific layout is required.dtype, NumPy infers a type capable of representing the input values, which can promote mixed numeric inputs.import numpy as np
values = np.array([1, 2, 3.0], dtype=np.float32)
column = np.array([1, 2, 3], ndmin=2)
print(values)
print(values.dtype)
print(column.shape)Creates an explicitly typed one-dimensional array and a two-dimensional array with a minimum dimension.
Converting Python sequences for vectorized numerical operations.
Ensuring predictable element representation for computation or storage.
Turning nested sequences into matrices or higher-dimensional data.
Meeting downstream layout or shape requirements.
numpy.array convert?It accepts an array-like object and returns an ndarray. The input may be an array, an object exposing the array interface, an object whose __array__ method returns an array, or a nested sequence.
Follow-up: How would you ensure at least two dimensions? Use ndmin=2, which specifies the minimum number of dimensions for the result.
dtype requests the desired data type. If omitted, NumPy chooses a type that can represent the values, applying promotion rules when needed.
Follow-up: What happens with [1, 2, 3.0]? The result is promoted to a floating-point type unless an explicit dtype overrides that choice.
order argument control?order controls memory layout and accepts K, A, C, or F. For a non-array input, the result is C order unless F is specified.
Follow-up: When might layout matter? Algorithms or interoperability requirements may benefit from explicitly selecting C- or Fortran-order storage.
numpy.asarraynumpy.asanyarraynumpy.fromiternumpy.zerosnumpy.emptynumpy.ndarray