w3resource

NumPy: numpy.ascontiguousarray() function

numpy.ascontiguousarray() function

The function numpy.ascontiguousarray() is used to get a contiguous array in memory (C order).

Syntax:

numpy.ascontiguousarray(a, dtype=None)
NumPy manipulation: ascontiguousarray() function

Parameters:

Name Description Required /
Optional
a Input array. Required
dtype Data-type of returned array. Optional

Return value:

out : ndarray - Contiguous array of same shape and content as a, with type dtype if specified.

Example: numpy.ascontiguousarray()

>>> import numpy as np
>>> a = np.arange(9).reshape(3, 3)
>>> np.ascontiguousarray(a, dtype=np.float32)
array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.],
       [ 6.,  7.,  8.]], dtype=float32)
>>> a.flags['C_CONTIGUOUS']
True

Pictorial Presentation:

NumPy manipulation: ascontiguousarray() function

Python - NumPy Code Editor:

Previous: asfortranarray()
Next: asarray_chkfinite()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/numpy/manipulation/ascontiguousarray.php