Numpy asarray() Function



The Numpy asarray() function is used to convert the given input to an array. The input data can be in the form of list, tuple, scalar, string or set. The data-tye of the array is inferred from the input data.

In Numpy, numpy.array() and numpy.asarray() both the functions are used to create or convert the given data into numpy array. The key difference between these functions is that numpy.array(), when we made changes in the original array it will not reflect in the original array. Whereas, the numpy.asarray() it would reflect all the changes made to the original array.

Syntax

Following is the syntax of the Numpy asarray() function −

numpy.asarray(arr, dtype=None, order=None, device=None, copy=None, like=None)

Parameters

Following are the parameters of the Numpy asarray() function −

  • arr:This is the required input parameter. It can be any array-like object or any nested sequences. If object is a scalar, a 0-dimensional array containing object is returned.
  • dtype: It specifies the desired data type of the array elements (e.g., int, float, complex, str). If not specified, NumPy will automatically consider the data type based on the input object.
  • copy (optional): If True(default value), a new copy of the object is always created, even if the object is already a NumPy array. If False, the function will try to avoid copying the data and just create a view of the input object if possible.
  • device(optional): It is the device on which to place the created array. Default value is None.
  • like (optional): It allows the creation of an array which is like the input object but uses an existing array-like object (like another NumPy array).
  • order (optional): It specifys the memory layout of the array. If object is not an array, the newly created array will be in C order (row major) unless F is specified, in which case it will be in Fortran order (column major) −
  • 'C': C-style row-major order.
  • 'F': Fortran-style column-major order.
  • 'A': 'F' if the input is Fortran contiguous, 'C' otherwise.
  • 'K': This is the default value keep the order as close as possible to the input.

Return Value

This function returns a NumPy array with the same data type as the input data, unless explicitly specified using the dtype parameter.

Example

Following is a basic example to create a numpy array using Python numpy.asarray() function −

import numpy as np
my_array = np.asarray([1,2,3,4],)
print("Array :",my_array)

Output

Following is the output of the above code −

Numpy Array : [1 2 3 4]

Example : Creating an array Using Sequences

A NumPy array can be created using sequences such as lists, tuples, or sets by passing them as an arguments to the numpy.array() function.

Here, we have passed my_tuple as an argument to the numpy.asarray() function which resulted an array −

import numpy as np  
my_tuple = (5, 10,15, 20) 
print(type(my_tuple))
my_array = np.asarray(my_tuple);  
print("Numpy Array :",my_array) 
print(type(my_array))

Output

Following is the output of the above code −

<class 'tuple'>
Numpy Array : [ 5 10 15 20]
<class 'numpy.ndarray'>

Example : Creating n Dimension array

The numpy.asarray() function can create an N-dimensional array by passing multiple sequences, such as lists or tuples, of the same size. If the sequences are of different sizes, it will raise a ValueError

import numpy as np
my_array = np.array([[10,20,30,40],[11,22,33,44]])
print("Numpy 2D Array :")
print(my_array)

Output

Numpy 2D Array :
[[10 20 30 40]
 [11 22 33 44]]

Example : Numpy array() Vs Numpy asarray()

In numpy.asarray(), any changes made to the resulting array will be reflected in the original array if the input is already a NumPy array, as it does not create a copy. However, in numpy.array(), changes made to the new array will not affect the original array because it creates a copy of the data.

In the following example, we can find the difference between numpy.array() and numpy.asarray() −

import numpy as np
# creating array
Original_Array = np.array([ 2, 3, 4, 5, 6])
print("Original array : ", Original_Array)
# assigning value to np.array
np_array = np.array(Original_Array)
Original_Array[3] = 0
print("np.array Array : ",np_array)

# assigning value to np.asarray
np_array = np.asarray(Original_Array)
print("np.asarray Array : ",np_array)

Output

Following is the output of the above code −

Original array :  [2 3 4 5 6]
np.array Array :  [2 3 4 5 6]
np.asarray Array :  [2 3 4 0 6]
numpy_array_creation_routines.htm
Advertisements