numpy.ascontiguousarray() – NumPy ascontiguousarray Python Function

Introduction to NumPy ascontiguousarray

When we wish to return a contiguous array in memory (C order), we utilize the numpy.ascontiguousarray() function.

Syntax:

`numpy.ascontiguousarray(arr, dtype=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 : [str or dtype object, optional] Data-type of returned array.

Return: ndarray Contiguous array of same shape and content as arr, with type dtype if specified.

Code Examples of NumPy ascontiguousarray

Example 01: List to array

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

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

print ("Input list : ", my_list)

out_arr = np.ascontiguousarray(my_list, dtype = np.float32)
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.]```

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

Example 02: Tuple to an array

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

import numpy as np

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

print ("Input tuple : \n", my_tuple)

out_arr = np.ascontiguousarray(my_tuple, dtype = np.int32)
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.ascontiguousarray() function

import numpy as np

my_scalar = 50

print ("Input scalar : ", my_scalar)

out_arr = np.ascontiguousarray(my_scalar, dtype = np.float32)
print ("output array from input scalar : ", out_arr)
print(type(out_arr))``````

Output:

```Input scalar :  50
output array from input scalar :  [50.]
<class 'numpy.ndarray'>```

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

FAQs

What is a contiguous array?

The ContiguousArray type is a customized array that keeps its items in a memory space that is always contiguous. When the element type is a class or the @objc protocol, Array can store its elements in either a contiguous piece of memory or an NSArray instance.

If the Element type of your array is a class or the @objc protocol, and you don’t need to bridge it to NSArray or send it to Objective-C APIs, ContiguousArray may be more efficient and predictable than Array. Array and ContiguousArray should be equally efficient if the array’s Element type is a struct or enumeration.

What is C order?

C order means that operating row-rise on the array will be slightly quicker. F order means that column-wise operations will be faster.

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.asfortranarray() – The NumPy asfortranarray Python Function