numpy.asanyarray() – The NumPy asanyarray Python Function

Introduction to NumPy asanyarray

When we wish to convert the input to an array, we utilize the numpy.asanyarray() function. Which allows ndarray subclasses to pass through. Scalars, lists, lists of tuples, tuples, tuples of tuples, tuples of lists, and ndarrays can all be used as input.

Syntax:

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

Parameters:

  • arr : [array_like] Input data, in any form that can be converted to an array. This includes scalars, lists, lists of tuples, tuples, tuples of tuples, tuples of lists, and ndarrays.
  • dtype : [data-type, optional] By default, the data-type is inferred from the input data.
  • order : Whether to use row-major (C-style) or column-major (Fortran-style) memory representation. Defaults to ‘C’.
  • like: [array_like] Reference object to allow the creation of arrays that are not NumPy arrays. If an array-like passed in as like supports the array_function protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument. New in version 1.20.0.

Return: [ndarray or a ndarray subclass] Array interpretation of arr. If arr is ndarray or a subclass of ndarray. It is return as-is and no copy is perform.

Code Examples of NumPy asanyarray

Example 01: List to array

# welcome to softhunt.net
# Python program explaining
# numpy.asanyarray() function

import numpy as np
my_list = [1, 2, 3, 4, 5, 6]

print ("Input list : ", my_list)

	
out_arr = np.asanyarray(my_list)
print ("output array from input list : ", out_arr)

Output:

Input list :  [1, 2, 3, 4, 5, 6]
output array from input list :  [1 2 3 4 5 6]

Example 02: Tuple to an array

# welcome to softhunt.net
# Python program explaining
# numpy.asanyarray() function

import numpy as np

my_tuple = ([1, 2, 3], [4, 5, 6])

print ("Input tuple : ", my_tuple)
	
out_arr = np.asanyarray(my_tuple)
print ("output array from input tuple : \n", out_arr)

Output:

Input tuple :  ([1, 2, 3], [4, 5, 6])
output array from input tuple : 
 [[1 2 3]
 [4 5 6]]

Example 03: Scalar to an array

# welcome to softhunt.net
# Python program explaining
# numpy.asanyarray() function

import numpy as np

my_scalar = 12

print ("Input scalar : ", my_scalar)
	
out_arr = np.asanyarray(my_scalar)
print ("output array from input scalar : ", out_arr)
print(type(out_arr))

Output:

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

FAQs

What are NumPy Ndarray attributes?

  1. ndarray.ndim
  2. ndarray.shape
  3. ndarray.size
  4. ndarray.dtype
  5. ndarray.itemsize

What is an array in Python?

A Python Array is a collection of data structures of the same type with the same data type. It is use to store data collections. The “array” module in Python is responsible for dealing with arrays. When using the array module to generate arrays. All of the array’s elements must be of the same numeric type.

Conclusion

That’s all for this article. If you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.asarray() – The NumPy asarray Python Function

Leave a Comment