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]
?
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.
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.
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.
>>> a = numpy.array((1, 3, 5, 2 ,4, 6)) >>> a.reshape(2, -1).T array([[1, 2], [3, 4], [5, 6]]) >>>
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]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With