The numpy.ascontiguousarray()
function is used to return a contiguous array where the dimension of the array is greater or equal to 1 and stored in memory (C order).
Note: A contiguous array is stored in an unbroken block of memory. To access the subsequent value in the array, we move to the next memory address.
numpy.ascontiguousarray(a, dtype=None)
The numpy.ascontiguousarray()
function takes the following parameter values:
a
: This represents the input array.dtype
: This represents the data type of the desired array. This is optional.The numpy.ascontiguousarray()
function returns a contiguous array with the same shape and data type as the input array.
import numpy as np# creating an input arraymyarray = np.arange(6).reshape(2,3)# implementing the numpy.ascontiguousarray() functioncontiguousarray = np.ascontiguousarray(myarray, dtype=np.float32)print(contiguousarray)
numpy
module.2
arrays with 3
elements. The output is assigned to a variable myarray
.numpy.ascontiguousarray()
function we make the array myarray
contiguous with the float data type. The output is assigned to a new variable contiguousarray
.contiguousarray
array.