BetterDocs
Home
Docs

Creation | np.asarray()

This method is similar to np.array() .

Method:

np.asarray(a, dtype=None, order='K', *, device=None, copy=True, like=None)

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

Returns:

np.ndarray

Parameters:

a: (list, tuple)-

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

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

If a is already a np.ndarray (NumPy array) with the requested data type and memory layout, np.asarray() will return it as is (without copying)

If a is not a np.ndarray or has a different data type, np.asarray() will create a new array based on a.

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.asarray([1, 2, 3], dtype=np.float32)
print(array)
# Output: [1., 2., 3.]

Values: +

device: None, Optional-

In NumPy 2.1, the device parameter in np.asarray() is still in an early stage and currently only accepts "cpu" as a valid option. At this time, it is primarily there for future development, allowing the function to eventually support GPUs or other devices, but for now, it doesn't support GPU IDs directly.

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.

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.

like: None, Optional-

The like parameter in np.asarray() 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