0 votes
in Python by
explain numpy.ascontiguousarray() in Python with Example

1 Answer

0 votes
by
numpy.ascontiguousarray() in Python The ascontiguousarray() returns a contiguous array (ndim>= 1) in memory (C order). Syntax

numpy.ascontiguousarray(a, dtype=None)

Parameter arr : This parameter includes the input data, in any form that can be converted to an array. This includes lists, tuples, lists of tuples, tuples of tuples, tuples of lists etc. dtype: It is an optional parameter. It depicts the data type of returned array, and by default, it is a float. Return This function returns a contiguous array (ndarray) of same shape and content as ‘arr’, with type specified in ‘dtype’. Example 1

# Python Programming giving an example for

# numpy.asanyarray() function

importnumpy as numpy

inp_list = [500, 1000, 1500, 2000]

print ("Input  list : ", inp_list)    

arr = numpy.ascontiguousarray(inp_list, dtype = numpy.float32)

print ("output array from input list : ", arr)

Output

Input  list :  [500, 1000, 1500, 2000]

output array from input list :  [  500.  1000.  1500.  2000.]

Example 2

# Python Programming giving an example for

# numpy.asanyarray() function

importnumpy as numpy

inp_tuple = ([1, 6, 0], [8, 2, 46])

print ("Input  touple : ", inp_tuple)

arr = numpy.ascontiguousarray(inp_tuple, dtype = numpy.int32)

print ("output array from input touple : ", arr)

Output

Input  touple :  ([1, 6, 0], [8, 2, 46])

output array from input touple :  [[ 1  6  0]

[ 8  2 46]]

Related questions

0 votes
asked May 19, 2022 in Python by john ganales
0 votes
asked May 19, 2022 in Python by john ganales
...