Related
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)
I have a variable named mesh of type object extracted from a MATLAB .mat file. (EDIT: Reproducible example below)
In [1]: mesh
Out[1]:
array([[ array([[ (array([[ 89, 108]], dtype=uint8),
array([[-131.659809],
[-131.659809],
[-131.659809],
...,
[ 52.022239],
[ 52.022239],
[ 52.022239]]),
array([[ 189.358345],
[ 187.271049],
[ 185.183753],
...,
[ -29.807736],
[ -31.895032],
[ -33.982328]]))]],
dtype=[('dim', 'O'), ('x', 'O'), ('y', 'O')])]], dtype=object)
How can I access the individual arrays dim, x and y?
Reproducible example:
I am unable to assign dtype=uint8 to an array as in the imported object.
Also, the imported object has object size (1,1) while this example has object size (1,1,1,1).
import numpy as np
mesh = np.array([[ np.array([[ (np.array([[ 6, 6]], dtype=float),
np.array([[-131.659809],
[-131.659809],
[-131.659809],
[ 52.022239],
[ 52.022239],
[ 52.022239]]),
np.array([[ 189.358345],
[ 187.271049],
[ 185.183753],
[ -29.807736],
[ -31.895032],
[ -33.982328]]))]],
dtype=[('dim', 'O'), ('x', 'O'), ('y', 'O')])]], dtype=object)
To access the separate arrays above, one could do the following:
>>> mesh[0][0][0][0][0] # dim
array([[ 6., 6.]])
>>> mesh[0][0][0][0][1] # x
array([[-131.659809],
[-131.659809],
[-131.659809],
[ 52.022239],
[ 52.022239],
[ 52.022239]])
>>> mesh[0][0][0][0][2] # y
array([[ 189.358345],
[ 187.271049],
[ 185.183753],
[ -29.807736],
[ -31.895032],
[ -33.982328]])
And to set a value, one could for example do:
>>> mesh[0][0][0][0][0][0][0]=1
>>> mesh
array([[[[ (array([[ 1., 6.]]), array([[-131.659809],
[-131.659809],
[-131.659809],
[ 52.022239],
[ 52.022239],
[ 52.022239]]), array([[ 189.358345],
[ 187.271049],
[ 185.183753],
[ -29.807736],
[ -31.895032],
[ -33.982328]]))]]]], dtype=object)
Disclaimer
This answer is a hack, not a solution. It is not meant to be interpreted as a general answer, but rather is specific to the exact example above. It does not deal with numpy, just with being able to access the specific arrays.
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?
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]]])
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