Say I have a two dimensional array of 4 columns, let's name the columns x1, x2, y1, y2, the data of the array are floats between 0 and 1, x2 is always larger than x1 and y2 is always larger than y1.
Each row represents a rectangle whose lower left corner is at (x1, y1) and whose width is x2-x1 and whose height is y2-y1.
The array is created like this:
import numpy as np
bounds = np.random.uniform(size=(100, 4))
bounds[:,:2].sort(1)
bounds[:,2:].sort(1)
Now how do I create rectangular vertices from the array in this order: lower left, lower right, upper right, upper left, without using for loops?
I can do this:
rectangles = [[(x1, y1), (x2, y1), (x2, y2), (x1, y2)] for x1, x2, y1, y2 in bounds]
But that defeats the purpose of using arrays in the first place.
Instead I can do these:
lower_left = np.array((bounds[:, 0], bounds[:, 2])).T
lower_right = np.array((bounds[:, 1], bounds[:, 2])).T
upper_right = np.array((bounds[:, 1], bounds[:, 3])).T
upper_left = np.array((bounds[:, 0], bounds[:, 3])).T
Now I only need to figure out how to join the columns horizontally so that each row in the columns stays a sub row.
I have tried to do this:
np.hstack((lower_left, lower_right, upper_right, upper_left))
But it isn't exactly working as intended, the fields are joined, I need the points to stay separate for reasons that are irrelevant to the question.
How can I achieve exactly the same result as my list comprehension using vectorization?
Edit
I just figured out how to unpack array to columns:
lower_left, lower_right, upper_right, upper_left = bounds.T
Keep in mind I wrote all of these on an Android phone and I don't have physical access to actual computer on which I can do programming during nighttime (it is nighttime at my locale at time of writing), it is a complicated matter I won't discuss here.
The thing is I can't run complex code, for simple code using only built-in functions or standard library I can use w3c school's Python TryIt editor to run the code, but for anything requiring imports of any library installed via pip, I can't run the code, so I can't know for sure if my example will run.
Edit
Fixed bug in example code, I wrote all of these on a phone so I can't run the code.
CodePudding user response:
I have already done it, I will post the code below just so that it may help anyone who stumbles here by Google searching:
import numpy as np
values = np.random.random((100, 4))
values[:, :2].sort(1)
values[:, 2:].sort(1)
x1, x2, y1, y2 = values.T
ll = np.array((x1, y1)).T
lr = np.array((x2, y1)).T
ur = np.array((x2, y2)).T
ul = np.array((x1, y2)).T
rects = np.stack((ll, lr, ur, ul), axis=1)
rects.shape
It produces this:
(100, 4, 2)
Sample:
array([[[0.0296138 , 0.29131621],
[0.3693961 , 0.29131621],
[0.3693961 , 0.97544314],
[0.0296138 , 0.97544314]],
[[0.51630027, 0.39519657],
[0.59897507, 0.39519657],
[0.59897507, 0.76210139],
[0.51630027, 0.76210139]],
[[0.84594749, 0.42784315],
[0.86691432, 0.42784315],
[0.86691432, 0.79314211],
[0.84594749, 0.79314211]],
[[0.34412745, 0.21132195],
[0.68466905, 0.21132195],
[0.68466905, 0.25444418],
[0.34412745, 0.25444418]],
[[0.08766792, 0.39237762],
[0.89154757, 0.39237762],
[0.89154757, 0.44256892],
[0.08766792, 0.44256892]],
[[0.05022344, 0.54610173],
[0.73931811, 0.54610173],
[0.73931811, 0.96692603],
[0.05022344, 0.96692603]],
[[0.51423357, 0.48105812],
[0.90815268, 0.48105812],
[0.90815268, 0.93234461],
[0.51423357, 0.93234461]],
[[0.18887874, 0.28019687],
[0.59905412, 0.28019687],
[0.59905412, 0.61594758],
[0.18887874, 0.61594758]],
[[0.00589912, 0.50042431],
[0.02952724, 0.50042431],
[0.02952724, 0.93056696],
[0.00589912, 0.93056696]],
[[0.2267573 , 0.04920763],
[0.43967406, 0.04920763],
[0.43967406, 0.92892994],
[0.2267573 , 0.92892994]],
[[0.2885361 , 0.70178778],
[0.73626986, 0.70178778],
[0.73626986, 0.71951917],
[0.2885361 , 0.71951917]],
[[0.50806757, 0.2228232 ],
[0.64801743, 0.2228232 ],
[0.64801743, 0.58853764],
[0.50806757, 0.58853764]],
[[0.52227217, 0.08670711],
[0.79866514, 0.08670711],
[0.79866514, 0.95127646],
[0.52227217, 0.95127646]],
[[0.09714329, 0.38051135],
[0.68042731, 0.38051135],
[0.68042731, 0.6425565 ],
[0.09714329, 0.6425565 ]],
[[0.28889578, 0.83151436],
[0.58681713, 0.83151436],
[0.58681713, 0.94071716],
[0.28889578, 0.94071716]],
[[0.2109561 , 0.05147464],
[0.52700784, 0.05147464],
[0.52700784, 0.21706122],
[0.2109561 , 0.21706122]]])
CodePudding user response:
With fancy indexes, you can avoid unnecessary stacked arrays:
>>> rand = np.random.rand(10, 4)
>>> rand
array([[0.9975164 , 0.24502806, 0.39619553, 0.07442783],
[0.57017416, 0.08316826, 0.16822264, 0.93233668],
[0.62260765, 0.77285638, 0.30093986, 0.76024642],
[0.26245902, 0.42329171, 0.31591031, 0.1685193 ],
[0.55910866, 0.33132002, 0.37912662, 0.99307642],
[0.02038662, 0.11176963, 0.72928163, 0.6766679 ],
[0.59143993, 0.03891871, 0.80613796, 0.34442057],
[0.47100568, 0.98703327, 0.60235766, 0.97791171],
[0.63499569, 0.94171562, 0.06308767, 0.78651194],
[0.679388 , 0.0464196 , 0.37712365, 0.76514397]])
>>> rand[:, :2].sort(1)
>>> rand[:, 2:].sort(1)
>>> rand[:, [0, 2, 1, 2, 1, 3, 0, 3]].reshape(-1, 4, 2)
array([[[0.24502806, 0.07442783],
[0.9975164 , 0.07442783],
[0.9975164 , 0.39619553],
[0.24502806, 0.39619553]],
[[0.08316826, 0.16822264],
[0.57017416, 0.16822264],
[0.57017416, 0.93233668],
[0.08316826, 0.93233668]],
[[0.62260765, 0.30093986],
[0.77285638, 0.30093986],
[0.77285638, 0.76024642],
[0.62260765, 0.76024642]],
[[0.26245902, 0.1685193 ],
[0.42329171, 0.1685193 ],
[0.42329171, 0.31591031],
[0.26245902, 0.31591031]],
[[0.33132002, 0.37912662],
[0.55910866, 0.37912662],
[0.55910866, 0.99307642],
[0.33132002, 0.99307642]],
[[0.02038662, 0.6766679 ],
[0.11176963, 0.6766679 ],
[0.11176963, 0.72928163],
[0.02038662, 0.72928163]],
[[0.03891871, 0.34442057],
[0.59143993, 0.34442057],
[0.59143993, 0.80613796],
[0.03891871, 0.80613796]],
[[0.47100568, 0.60235766],
[0.98703327, 0.60235766],
[0.98703327, 0.97791171],
[0.47100568, 0.97791171]],
[[0.63499569, 0.06308767],
[0.94171562, 0.06308767],
[0.94171562, 0.78651194],
[0.63499569, 0.78651194]],
[[0.0464196 , 0.37712365],
[0.679388 , 0.37712365],
[0.679388 , 0.76514397],
[0.0464196 , 0.76514397]]])