BetterDocs
Home
Docs

Creation | np.array()

Method:

np.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)

Creates a NumPy array with the elements passed to object parameter.

Returns:

np.ndarray

Parameters:

object: (list, tuple)-

The input array-like data to be converted into a NumPy array.

import numpy as np
array = np.array(object=[1, 2, 3])
print(array)
#Output: [1, 2, 3]

dtype: data-type, Optional-

Specifies the data-type of the array elements. If not provided, it’s inferred from the input.

import numpy as np
array = np.array(object=[1, 2, 3], dtype=np.float32)
print(array)
#Output: [1., 2., 3.]

Values: +

copy: (True or False) Optional-

The copy parameter specifies whether to create a new array (copy) or if NumPy can reuse the existing memory from the input array (i.e., return a view ).

copy = True (default) +

copy = False +

Modification to the original_array or new_array will affect both the arrays.

order: {'C', 'F', 'K', 'A'}, Optional-

Specifies the memory layout order for multi-dimensional arrays.

order = K (default) +

order = C +

order = F +

order = A +

.ravel()  is a NumPy function used to flatten a multi-dimensional array into a 1D array.

subok: (True or False), Optional-

It controls whether the output array will retain the subclass type of the input array (if it’s a subclass of np.ndarray) or be cast to a base np.ndarray.

subok = False (default) +

subok = True +

In NumPy, the .view()  method creates a new "view" of an existing array with a different data type or subclass without copying the data.

This means the new array points to the same data as the original array but can be accessed or interpreted differently.

ndmin: int, Optional-

Ensures that the created array has at least a specified number of dimensions. If the input does not meet the minimum number of dimensions, it will be automatically padded with extra dimensions (of size 1) at the beginning to meet the requirement.

import numpy as np

# A 1D array
arr_1d = np.array([1, 2, 3])
print("Original array:", arr_1d) # Output: [1 2 3]
print("Original array dimensions:", arr_1d.ndim) # Output: 1

# Using ndmin=2 to make sure the array has at least 2 dimensions
arr_2d = np.array([1, 2, 3], ndmin=2)
print("
Array with ndmin=2:", arr_2d) # Output: [[1 2 3]]
print("Array with ndmin=2 dimensions:", arr_2d.ndim) # Output: 2

# Using ndmin=3 to make sure the array has at least 3 dimensions
arr_3d = np.array([1, 2, 3], ndmin=3)
print("
Array with ndmin=3:", arr_3d) # Output: [[[1 2 3]]]
print("Array with ndmin=3 dimensions:", arr_3d.ndim) # Output: 3

If the actual_dimension > ndmin then the output will have actual_dimension.

like: None, Optional-

The like parameter in np.array() was introduced to make array creation more flexible, particularly for compatibility with libraries that extend NumPy, like Dask, CuPy, or other libraries that create arrays compatible with np.ndarray but optimized for different backends (e.g., parallel processing or GPU-based arrays).


Logo

BetterDocs

Support

EmailDiscordForms

Documentations

Python

Company

AboutDocs

Policies

Terms of ServicePrivacy Policy