numpy.asfarray() – The NumPy asfarray Python Function

Introduction to NumPy asfarray

When we wish to convert the input to a float type array, we utilize the numpy.asfarray() function. Scalars, lists, lists of tuples, tuples, tuples of tuples, tuples of lists, and ndarrays are all possible inputs.

Syntax:

numpy.asfarray(arr, dtype=type ‘numpy.float64’)

Parameters:

  • arr : [array_like] Input data. In any form that can be converted to a float type array. This includes scalar, lists, lists of tuples, tuples, tuples of tuples, tuples of lists, and ndarrays.
  • dtype : Float type code to coerce input array arr. If dtype is one of the ‘int’ dtypes, it is replaced with float64.

Return: [ndarray] The input arr as a float ndarray.

Code Examples of NumPy asfarray

Example 01: List to float type array

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

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

print ("Input list : ", my_list)

	
out_arr = np.asfarray(my_list)
print ("output float type array from input list : \n", out_arr)

Output:

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

Example 02: Tuple to float type array

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

import numpy as np

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

print ("Input tuple : ", my_tuple)
	
out_arr = np.asfarray(my_tuple, dtype ='int8')
print ("output float type array from input tuple : \n", out_arr)

Output:

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

Example 03: Scalar to float type array

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

import numpy as np

my_scalar = 50

print ("Input scalar : ", my_scalar)
	
out_arr = np.asfarray(my_scalar, dtype ='float')
print ("output float type array from input scalar : ", out_arr)

Output:

Input scalar :  50
output float type array from input scalar :  50.0

FAQs

How do I turn a list into a NumPy array?

To convert a Python list to a NumPy array. Use either of the following two methods:

  1. The np.array() function returns a NumPy array from an iterable. Producing a new data structure in memory.
  2. The np.asarray() function converts an iterable to an array using the np.asarray() function. The distinction between np.array() and np.asarray() is that if you pass a NumPy array, np.asarray() does not construct a new copy in memory. The NumPy array reflects all modifications made to the original array.

How do you convert data to an array in Python?

  1. Using numpy.array(). This function of the NumPy library takes a list as an argument and returns an array that contains all the elements of the list.
  2. Using numpy.asarray(). This function calls the numpy.array() function inside itself.

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. The NumPy asarray Python Function
  2. The NumPy asanyarray Python Function
  3. NumPy asmatrix Python Function

Leave a Comment