Numpy recarray.flatten () function | python



numpy.recarray.flatten() returns arrays of records in one dimension.

Syntax: numpy.recarray.flatten(order=`C`)

Parameters:

order: [[& # 39; C & # 39 ;, & # 39; F & # 39 ;, & # 39; A & # 39;, & # 39; K & # 39;], optional]
“C” stands for major alignment (C style). 
“F” stands for major alignment (Fortran style). 
“A” means alignment in a major column if a is contiguous in Fortran memory, otherwise — in major order. 
“K” means alignment in the order of the elements in memory. The default is “C”.

Returns: a copy of the input array collapsed into one dimension.

Code # 1:

# Python program explaining
# numpy.recarray.flatten () method

 
# import numy as a geek

import numpy as geek

 
# create an input array with two different fields

in_arr = geek.array ([[( 5.0 , 2 ), ( 3.0 , - 4 ), ( 6.0 , 9 )],

[( 9.0 , 1 ), ( 5.0 , 4 ), ( - 12.0 , - 7 )]],

dtype = [( `a` , float ), ( `b` , int )])

  

print ( " Input array : " , in_arr)

  
# convert it to an array of posts,
# using arr.view (np.recarray)

rec_arr = in_arr.view (geek.recarray)

print ( "Record array of float:" , rec_arr.a)

print ( " Record array of int: " , rec_arr.b)

  
# using recarray.flatten methods
# expose post in Fortan order

out_arr1 = rec_arr.a.flatten (order = `F` )

print ( "Output float flattened array in Fortan order:" , out_arr1) 

 
# using recarray.flatten methods
# place array of records in default order

out_arr2 = rec_arr.a.flatten ()

print ( "Output float flattenedarray in default order:" , out_arr2)

 
# recarray methods .flatten
# to write int in order & # 39; A & # 39;

out_arr3 = rec_arr.b.flatten (order = `A` )

print ( "Output int flattened array in A order:" , out_arr3) 

 
# using recarray.flatten methods
# place an array of posts in the order & # 39; K & # 39;

out_arr4 = rec_arr.b.flatten (order = `K` )

print ( " Output intt flattened array in K order: " , out_arr4) 

Output:

 Input array: [[(5., 2) (3 ., -4) (6., 9)] [(9., 1) (5., 4) (-12., -7)]] Record array of float: [[5. 3. 6.] [ 9. 5. -12.]] Record array of int: [[2 -4 9] [1 4 -7]] Output float flattened array in Fortan order: [5. 9. 3. 5. 6. -12. ] Output float flattenedarray in default order: [5. 3. 6. 9. 5. -12.] Output int flattened array in A order: [2 -4 9 1 4 -7] Output intt flattened array in K order: [ 2 -4 9 1 4 -7]