numpy.asfortranarray() – The NumPy asfortranarray Python Function

Introduction to NumPy asfortranarray

When we wish to convert the input to a memory array that is set out in Fortran order, we utilize the numpy.asfortranarray() function. Scalars, lists, lists of tuples, tuples, tuples of tuples, tuples of lists, and ndarrays are all possible inputs.

Syntax:

numpy.asfortranarray(arr, dtype=None)

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 : By default, the data type is infer from the input data.

Return: The input arr in Fortran, or column-major, order.

Code Examples of NumPy asfortranarray

Example 01: List to fortranarray

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

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

print ("Input list : ", my_list)

	
out_arr = np.asfortranarray(my_list)
print ("output fortanarray from input list : \n", out_arr)

Output:

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

Example 02: Tuple to fortanarray

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

import numpy as np

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

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

Output:

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

Example 03: Scalar to fortanarray

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

import numpy as np

my_scalar = 50

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

Output:

Input scalar :  50
output fortan array from input scalar :  [50.]

Example 04: array to fortanarray

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

import numpy as np

in_arr = np.arange(16).reshape(4, 4)

print ("Input array : \n", in_arr)

# checking if it is fortanarray
print(in_arr.flags['F_CONTIGUOUS'])

out_arr = np.asfortranarray(in_arr, dtype ='float')
print ("output array from input array : \n", out_arr)

# checking if it has become fortanarray
print(out_arr.flags['F_CONTIGUOUS'])

Output:

Input array : 
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
False
output array from input array : 
 [[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]
 [12. 13. 14. 15.]]
True

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

What is a tuple in NumPy?

A NumPy array is a grid of identical-type items indexed by a tuple of nonnegative integers. The array’s rank is the number of dimensions; the shape is a tuple of numbers indicating the array’s size along each dimension.

What is a tuple in Python?

Tuples are a type of variable that allows you to store several elements in a single variable. A tuple is one of Python’s four built-in data types for storing collections of data; the other three are List, Set, and Dictionary, all of which have various properties and applications. A tuple is a collection of items that is both order and immutable.

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 asfarray Python Function

Leave a Comment