Numpy asanyarray() Function



The Numpy asanyarray() function is used to convert the input to an array, but keeps ndarray subclasses unchanged, which means if the input data is not array it will convert into a standard numpy array.

If the input is already a NumPy ndarray subclass (e.g., numpy.matrix or other custom subclasses), it is returned as is without converting it to a plain ndarray.

Apart from array, Following are other input values to the numpy.asanyarray() function −

  • scalars- It can be any numeric value. For an example, 3.4,5,112...
  • Sequences- It can be list, tuple datatypes. For an example, [1,2,3,4], ('a', 'b', 'c', 'd')
  • Nested Sequences- It can be nested tuples or lists. For an example, [[1,2,3], [4,5,6],[7,8,9]], ((12,10,45),(90,23,56))

Syntax

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

numpy.asanyarray(arr, dtype=None, order=None, like=None)

Parameters

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

  • arr:This is the required input parameter. It can be any array-like object (like a list, tuple, or another NumPy array) that we want to convert into a NumPy array.
  • dtype(optional): 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.
  • 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 ndarray if the input data is an ndarray or a subclass of ndarray, it is returned as-is and no copy is performed.

Example

Following is a basic example to create a numpy array using Numpy asanyarray() function −

import numpy as np
my_Array=np.asanyarray([1, 2, 3, 4, 5])
print("Numpy Array:",my_Array)
print(type(my_Array))

Output

Following is the output of the above code −

Numpy Array: [1 2 3 4 5]
<class 'numpy.ndarray'>

Example : Creating a Array Using Sequences

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

Here, we have passed my_tuple as an argument to the numpy.asanyarray() function converted it into numpy array −

#importing numpy module
import numpy as np
#defined tuple
my_tuple=(15,25,35,45)
my_Array=np.asanyarray(my_tuple)
print("Numpy Array:",my_Array)
print(type(my_Array))

Output

Following is the output of the above code −

Numpy Array: [15 25 35 45]
<class 'numpy.ndarray'>

Example : Converting Scalar to Array

Using numpy.asanyarray() function is used to convert scalar value to a numpy array.

In the following example, we have converted my_scalar i.e 100 is passed as an argument to the numpy asanyarray() , which result a numpy array −

import numpy as np
#initalizing a scalar value
my_scalar = 100
print ("Input scalar : ", my_scalar) 	
out_arr = np.asanyarray(my_scalar) 
print ("output array from input scalar : ", out_arr) 
print(type(out_arr)) 

Output

Following is the output of the above code −

Input scalar :  100
output array from input scalar :  100
<class 'numpy.ndarray'>

Example : Creating n Dimensional Array

We can create an n-dimensional NumPy array by passing a nested sequences such as nested list(list of lists), nested tuple(tuple of tuples) as an argument to the numpy.asanyarray() function. However, if the nested sequences have unequal sizes, a ValueError will be raised.

Here, we have created 2D dimensional numpy array using numpy.asanyarray()

import numpy as np
#initalizing a scalar value
my_list = [[1,2,3],[10,20,30],[100,200,300]]
out_arr = np.asanyarray(my_list) 
print ("2D Numpy Array - ")
print(out_arr) 

Output

Following is the output of the above code −

2D Numpy Array - 
[[  1   2   3]
 [ 10  20  30]
 [100 200 300]]

Example: Passing an Array as an Argument

When a standard NumPy array is passed as an argument to the numpy.asanyarray() function, it remains unchanged. However, if a subclass of numpy.ndarray (numpy.matrix) is passed, it is returned as it is, preserving the subclass type −

import numpy as np
# Input: NumPy matrix (a subclass of ndarray)
mat = np.matrix([[1, 2], [3, 4]])
arr1 = np.asanyarray(mat)
print(type(arr1))  
# Input: Standard ndarray
arr = np.array([5, 6, 7])
arr2 = np.asanyarray(arr)
print(type(arr2)) 

Output

Following is the output of the above code −

<class 'numpy.matrix'>
<class 'numpy.ndarray'>
numpy_array_creation_routines.htm
Advertisements