Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Is there an inverse for ndarray.flatten('F')?

Tags:

For example:

from numpy import * x = array([[1,2], [3, 4], [5, 6]]) print x.flatten('F') >>>[1 3 5 2 4 6] 

Is it possible to get [[1,2], [3, 4], [5, 6]] from [1 3 5 2 4 6]?

like image 449
chimichanga Avatar asked Feb 23 '11 03:02

chimichanga


People also ask

What does flatten () do in numpy?

The flatten() function is used to get a copy of an given array collapsed into one dimension. 'C' means to flatten in row-major (C-style) order. 'F' means to flatten in column-major (Fortran- style) order. 'A' means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise.

How do you Inverse an array in numpy?

We use numpy. linalg. inv() function to calculate the inverse of a matrix. The inverse of a matrix is such that if it is multiplied by the original matrix, it results in identity matrix.

How do you flatten an array of arrays in python?

Flatten a NumPy array with reshape(-1) You can also use reshape() to convert the shape of a NumPy array to one dimension. If you use -1 , the size is calculated automatically, so you can flatten a NumPy array with reshape(-1) . reshape() is provided as a method of numpy.


2 Answers

>>> a = numpy.array((1, 3, 5, 2 ,4, 6)) >>> a.reshape(2, -1).T array([[1, 2],        [3, 4],        [5, 6]]) >>>  
like image 62
senderle Avatar answered Jan 15 '23 20:01

senderle


This seems a little more straightforward. Just pass the original shape back into reshape.

import numpy as np np.array([[1,2], [3, 4], [5, 6]]).flatten().reshape((3, 2)) 

array([[1, 2],        [3, 4],        [5, 6]]) 

And for your Fortran ordering, pass 'F' for the reshape order:

import numpy as np np.array([[1,2], [3, 4], [5, 6]]).flatten('F').reshape((3, 2), order='F') 

array([[1, 2],        [3, 4],        [5, 6]]) 
like image 22
crizCraig Avatar answered Jan 15 '23 21:01

crizCraig