Reshape/resize(pivot?) N-dimensional numpy array column-wise - python

I need to reshape/resize(pivot?) [sorry I am fairly new to numpy, working with it about 6 weeks] an numpy array based on column.
The source numpy array is this:
[[[-0.98261404]
[-0.98261404]
[-0.95991508]
...,
[-0.92496699]
[-0.92731224]
[-0.926328 ]]
[[-0.91894622]
[-0.91894622]
[-0.92171439]
...,
[-1.02966519]
[-1.03908464]
[-1.03527072]]
[[-0.92201427]
[-0.92201427]
[-0.93004196]
...,
[-1.06750448]
[-1.07838491]
[-1.07398661]]
[[-0.9233676 ]
[-0.9233676 ]
[-0.93250255]
...,
[-1.07617807]
[-1.08736608]
[-1.08284474]]
[[-0.91913077]
[-0.91913077]
[-0.92023803]
...,
[-1.01886934]
[-1.02782743]
[-1.02419806]]]
I like to reshape/resize(pivot?) above as follows:
[[[-0.98261404]
[-0.91894622]
[-0.92201427]
[-0.9233676 ]
[-0.91913077]]
...,
[[-0.926328 ]
[-1.03527072]
[-1.07398661]
[-1.08284474]
[-1.02419806]]]
What is the best way to do this?
Thanks!

I believe what you want is (considering I did understood it properly):
>>> B = np.transpose(A, (0, 2, 1))
being A your data and B the resulting array. That will transpose/pivot the last 2 axes. Alternatively, you can write
>>> B = np.swapaxes(A, 1, 2)
which is equivalent (and probably easier to read). Extended for an N-dimensional arrays:
>>> B = np.swapaxes(A, a, b) # being `a` and `b` the axes
An example:
>>> import numpy as np
>>> A = np.random.rand(1, 2, 3)
>>> A
array([[[ 0.54766263, 0.95017886, 0.32949198],
[ 0.76255173, 0.88943131, 0.78594731]]])
>>> np.swapaxes(A, 1, 2)
array([[[ 0.54766263, 0.76255173],
[ 0.95017886, 0.88943131],
[ 0.32949198, 0.78594731]]])
Alternatively, you can just transpose the array:
>>> A.T # equivalent to np.transpose(A, (2, 1, 0))
array([[[ 0.54766263],
[ 0.76255173]],
[[ 0.95017886],
[ 0.88943131]],
[[ 0.32949198],
[ 0.78594731]]])
Reordering the dimensions in the opposite order (2, 1, 0).

Let's say your array is a.
Then this should do the trick :
x,y,z = a.shape
b = a.T #Transpose - get the indices grouped along the other axis
b = b.reshape(y, x, z) #Interchange the axes.
For example :
In [58]: a = np.random.random(20)
In [59]: a = a.reshape(4,5,1)
In [60]: a
Out[60]:
array([[[ 0.40906066],
[ 0.57160002],
[ 0.22642471],
[ 0.35845352],
[ 0.26999423]],
[[ 0.91962882],
[ 0.62664991],
[ 0.21286972],
[ 0.39995373],
[ 0.1141539 ]],
[[ 0.03040894],
[ 0.79666903],
[ 0.72822631],
[ 0.84388555],
[ 0.23265895]],
[[ 0.63548896],
[ 0.50314843],
[ 0.88547892],
[ 0.49824574],
[ 0.55835843]]])
In [61]: b = b.reshape(y, x, z)
In [62]: x,y,z = a.shape
In [63]: b = a.T
In [64]: b = b.reshape(y,x,z)
In [65]: b
Out[65]:
array([[[ 0.40906066],
[ 0.91962882],
[ 0.03040894],
[ 0.63548896]],
[[ 0.57160002],
[ 0.62664991],
[ 0.79666903],
[ 0.50314843]],
[[ 0.22642471],
[ 0.21286972],
[ 0.72822631],
[ 0.88547892]],
[[ 0.35845352],
[ 0.39995373],
[ 0.84388555],
[ 0.49824574]],
[[ 0.26999423],
[ 0.1141539 ],
[ 0.23265895],
[ 0.55835843]]])

Related

Replace elements in an Nx3x2 numpy array with elements located in an Mx2 numpy array

I have the following xy numpy array which represents the locations of the vertices of some triangles:
array([[[ 0.30539728, 49.82845203],
[ 0.67235022, 49.95042185],
[ 0.268982 , 49.95195348]],
[[ 0.268982 , 49.95195348],
[ 0.67235022, 49.95042185],
[ 0.27000135, 50.16334035]],
...
[[ 1.00647459, 50.25958169],
[ 0.79479121, 50.3010079 ],
[ 0.67235022, 49.95042185]],
[[ 0.79479121, 50.3010079 ],
[ 0.6886783 , 50.25867683],
[ 0.67235022, 49.95042185]]])
Here, it's an array of shape (10, 3, 2) but it could as well be (5, 3, 2) or (18, 3, 2), you name it. In any case it's of shape (N, 3, 2).
I have another numpy array to_replace of shape (4, 2) but it could as well be (6, 2) or (7, 2), but always of shape (M, 2):
array([[ 1.08267406, 49.88690993],
[ 1.1028248 , 50.01440407],
[ 0.74114309, 49.73183549],
[ 1.08267406, 49.88690993]])
It represents the locations of pairs of coordinates that can be found in my first array. Note that each of these pairs is present at least once in xy but could be present more than once.
Finally, I have a third array replace_by of which shape (8,) (or of shape (M*2) based on the indication above) and which values are meant to replace exactly those contained in to_replace in my first xy array. It looks like this:
array([ 0.87751214, 49.91866589, 0.88758751, 49.98241296, 0.70674665, 49.84112867, 0.87751214, 49.91866589])
So basically all pairs [1.08267406, 49.88690993] in xy should be replaced by [0.87751214, 49.91866589] for example.
My current code looks like this but it works only if to_replace and replace_by are strictly of shape (2, 2).
indices = (xy == to_replace[:, None][:, None])[0]
xy[indices] = replace_by
I already looked at a number of answers and actually got inspired by some of them but I still can't get it to work.
You can use numpy.isclose to compare rows and then use .all(axis=2) to find where all last rows are the same. Numpy will broadcast each row to fit xy shape.
import numpy as np
xy = np.array([[[ 0.30539728, 49.82845203],
[ 0.67235022, 49.95042185],
[ 0.268982 , 49.95195348]],
[[ 0.268982 , 49.95195348],
[ 0.67235022, 49.95042185],
[ 0.27000135, 50.16334035]],
[[ 1.00647459, 50.25958169],
[ 0.79479121, 50.3010079 ],
[ 0.67235022, 49.95042185]],
[[ 0.79479121, 50.3010079 ],
[ 0.6886783 , 50.25867683],
[ 0.67235022, 49.95042185]]])
xy_start = xy.copy()
to_replace = np.array([[ 1.08267406, 49.88690993],
[ 1.1028248 , 50.01440407],
# [ 0.74114309, 49.73183549],
[ 0.6886783 , 50.25867683],
[ 1.08267406, 49.88690993]])
replace_by = np.array([ 0.87751214, 49.91866589, 0.88758751, 49.98241296, 0.70674665, 49.84112867, 0.87751214, 49.91866589])
replace_by_reshaped = replace_by.reshape(-1, 2)
for i, row in enumerate(to_replace):
xy[np.isclose(xy, row).all(axis=2)] = replace_by_reshaped[i]
print(xy_start)
# [[[ 0.30539728 49.82845203]
# [ 0.67235022 49.95042185]
# [ 0.268982 49.95195348]]
# [[ 0.268982 49.95195348]
# [ 0.67235022 49.95042185]
# [ 0.27000135 50.16334035]]
# [[ 1.00647459 50.25958169]
# [ 0.79479121 50.3010079 ]
# [ 0.67235022 49.95042185]]
# [[ 0.79479121 50.3010079 ]
# [ 0.6886783 50.25867683]
# [ 0.67235022 49.95042185]]]
print(xy)
# [[[ 0.30539728 49.82845203]
# [ 0.67235022 49.95042185]
# [ 0.268982 49.95195348]]
# [[ 0.268982 49.95195348]
# [ 0.67235022 49.95042185]
# [ 0.27000135 50.16334035]]
# [[ 1.00647459 50.25958169]
# [ 0.79479121 50.3010079 ]
# [ 0.67235022 49.95042185]]
# [[ 0.79479121 50.3010079 ]
# [ 0.70674665 49.84112867]
# [ 0.67235022 49.95042185]]]
EDIT
.all(axis=2) shrink axis=2 to True if all values along axis=2 are True and False else. I think little 2d example made it clear what is happening here.
>>> import numpy as np
>>> a = np.array([[0, 1], [0, 2], [3, 4]])
>>> a
array([[0, 1],
[0, 2],
[3, 4]])
>>> np.isclose(a, [0, 1])
array([[ True, True],
[ True, False],
[False, False]])
>>> np.isclose(a, [0, 1]).all(axis=1)
array([ True, False, False])
>>> a[np.isclose(a, [0, 1]).all(axis=1)]
array([[0, 1]])
>>> a[np.isclose(a, [0, 1]).all(axis=1)] = [12, 14]
>>> a
array([[12, 14],
[ 0, 2],
[ 3, 4]])
The numpy-indexed package (disclaimer: I am its author) contains functionality to solve this problem in a vectorized and elegant manner.
Given the arrays as you have defined them, this one-liner should do the trick:
import numpy_indexed as npi
npi.remap(xy.reshape(-1, 2), to_replace, replace_by.reshape(-1, 2)).reshape(-1, 3, 2)

Transposing a 3d matrix twice?

Given a 3d matrix in numpy, how do i transpose matrix A to matrix B, and then transpose B back to A.
I seem to be able to transpose A to matrix be as such:
>>> import numpy
>>> a = numpy.random.rand(3,3,3)
>>> print a
[[[ 0.69774026 0.14510969 0.12476703]
[ 0.02631041 0.61497379 0.86985018]
[ 0.14114129 0.87731903 0.76020481]]
[[ 0.66507441 0.16093391 0.19799549]
[ 0.56614043 0.17268869 0.39641231]
[ 0.61712168 0.71372674 0.32510021]]
[[ 0.50997888 0.75687504 0.69902029]
[ 0.50366012 0.22187467 0.58894526]
[ 0.8726912 0.4319045 0.43687999]]]
>>> b = a.transpose(0,2,1)
>>> print b
[[[ 0.69774026 0.02631041 0.14114129]
[ 0.14510969 0.61497379 0.87731903]
[ 0.12476703 0.86985018 0.76020481]]
[[ 0.66507441 0.56614043 0.61712168]
[ 0.16093391 0.17268869 0.71372674]
[ 0.19799549 0.39641231 0.32510021]]
[[ 0.50997888 0.50366012 0.8726912 ]
[ 0.75687504 0.22187467 0.4319045 ]
[ 0.69902029 0.58894526 0.43687999]]]
But how do i return matrix b to matrix a?

How to subtract from previous row?

I have a np.array stored in a variable, x, which looks like below:
array([[ 956],
[ 929],
[1083],
[1074],
[ 922]]
I want to subtract every number from the previous number, and I want a new variable, y, to look like below:
array([[ -27],
[ 154],
[ -9],
[-152]]
import numpy as np
x = np.array([[ 956],
[ 929],
[1083],
[1074],
[ 922]]
out = np.diff(out, axis=0)
Out: array([[ -27],
[ 154],
[ -9],
[-152]]

eigenvectors created by numpy.linalg.eig don't seem correct

I create an arbitrary 2x2 matrix:
In [87]: mymat = np.matrix([[2,4],[5,3]])
In [88]: mymat
Out[88]:
matrix([[2, 4],
[5, 3]])
I attempt to calculate eigenvectors using numpy.linalg.eig:
In [91]: np.linalg.eig(mymat)
Out[91]:
(array([-2., 7.]),
matrix([[-0.70710678, -0.62469505],
[ 0.70710678, -0.78086881]]))
In [92]: eigvec = np.linalg.eig(mymat)[1][0].T
In [93]: eigvec
Out[93]:
matrix([[-0.70710678],
[-0.62469505]])
I multiply one of my eigenvectors with my matrix expecting the result to be a vector that is a scalar multiple of my eigenvector.
In [94]: mymat * eigvec
Out[94]:
matrix([[-3.91299375],
[-5.40961905]])
However it is not. Can anyone explain to me what is going wrong here?
From the documentation for linalg.eig:
v : (..., M, M) array
The normalized (unit "length") eigenvectors, such that the
column v[:,i] is the eigenvector corresponding to the
eigenvalue w[i].
You want the columns, not the rows.
>>> mymat = np.matrix([[2,4],[5,3]])
>>> vals, vecs = np.linalg.eig(mymat)
>>> vecs[:,0]
matrix([[-0.70710678],
[ 0.70710678]])
>>> (mymat * vecs[:,0])/vecs[:,0]
matrix([[-2.],
[-2.]])
>>> vecs[:,1]
matrix([[-0.62469505],
[-0.78086881]])
>>> (mymat * vecs[:,1])/vecs[:,1]
matrix([[ 7.],
[ 7.]])
No, it's true. numpy does not work correctly. Example:
A
Out[194]:
matrix([[-3, 3, 2],
[ 1, -1, -2],
[-1, -3, 0]])
E = np.linalg.eig(A)
E
Out[196]:
(array([ 2., -4., -2.]),
matrix([[ -2.01889132e-16, 9.48683298e-01, 8.94427191e-01],
[ 5.54700196e-01, -3.16227766e-01, -3.71551690e-16],
[ -8.32050294e-01, 2.73252305e-17, 4.47213595e-01]]))
A*E[1] / E[1]
Out[205]:
matrix([[ 6.59900617, -4. , -2. ],
[ 2. , -4. , -3.88449298],
[ 2. , 8.125992 , -2. ]])

Removing "rows" containing negative values in 3D numpy array

What I have:
>>> forces
array([[[ 63.82078252, 0.63841691],
[ -62.45826693, 7.11946976],
[ -87.85946925, 15.1988562 ],
[-120.49417797, -16.31785819],
[ -81.36080338, 6.45074645]],
[[ 364.99959095, 4.92473888],
[ 236.5762723 , -7.22959548],
[ 69.55657789, 1.20164815],
[ -22.1684177 , 13.42611095],
[ -91.19739147, -16.15076634]]])
forces[0] and forces [1] each contain a list of paired values, e.g. 63.82078252 & 0.63841691 are one data point.
I want to remove all pairs where the first value is negative:
>>> forces
array([[[ 63.82078252, 0.63841691]],
[[ 364.99959095, 4.92473888],
[ 236.5762723 , -7.22959548],
[ 69.55657789, 1.20164815]]])
But this type of structure is not possible since the two slices of forces have different sizes: (1, 2) and (3, 2) respectively.
My sloppy attempt:
>>> forces[:,:,0][forces[:,:,0] < 0] = np.nan
>>> forces
array([[[ 63.82078252, 0.63841691],
[ nan, 7.11946976],
[ nan, 15.1988562 ],
[ nan, -16.31785819],
[ nan, 6.45074645]],
[[ 364.99959095, 4.92473888],
[ 236.5762723 , -7.22959548],
[ 69.55657789, 1.20164815],
[ nan, 13.42611095],
[ nan, -16.15076634]]])
and then using isnan to remove the relevant entries:
>>> forces = forces[~np.isnan(forces).any(axis=2)]
>>> forces
array([[ 63.82078252, 0.63841691],
[ 364.99959095, 4.92473888],
[ 236.5762723 , -7.22959548],
[ 69.55657789, 1.20164815]])
So these are the correct values but they are now lumped together into a 2D array.
How can I create a heterogeneously sized "array" that will contain two slices of size (1, 2) and (3, 2) respectively, for this simplified example?
Also any pointers on accomplishing the task more elegantly would be much appreciated!
It is simply
forces[forces[..., 0] >= 0]
Read more here: http://scipy-lectures.github.io/intro/numpy/array_object.html#fancy-indexing

Categories

Resources