Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flatten an array like this using Python?

I have this array here:

a = np.array([[['1','2','3'],['10','11','12']],[['4','5','6'],['13','14','15']],[['7','8','9'],['16','17','18']]])

and I want to transform it like this:

>>>[['1' '2' '3'] ['4' '5' '6'] ['7' '8' '9'] ['13' '14' '15'] ['10' '11' 12'] ['16' '17' '18']]

i'm using Numpy's .flatten(), but it is not working

thanks

like image 511
Víctor Varela Avatar asked Apr 19 '26 23:04

Víctor Varela


1 Answers

Try:

a.transpose(1, 0, 2).reshape(-1, 3)
like image 182
Silver Avatar answered Apr 21 '26 13:04

Silver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!