New Post: Processing satellite conjunctions with numpy efficiently
Original Post:
I have a numpy array of shape n x m x r, where the n axis represents an object, the m axis represents a timestep and the r axis represents a position vector in 3-d space. I have an array containing three (x, y and z position values) of m objects at n points in time. This is the format my data is delivered in (from the python module sgp4
, specifically a Satrec_Array
if anyone's interested) so I can't move this further up the data processing chain.
I want to be able to represent this as an m x n array of position vectors, so essentially "collapsing" the position axis into a single array, so an array containing m x n elements, each of which is a position vector relating to an object at a time.
I'm struggling to find a way to do this efficiently - I can do it with bog standard python loops, but as I scale up the number of objects and timesteps this becomes massively inefficient.
I'm not very well versed in numpy, but none of the solutions I've searched or attempts at using methods such as [h/v/d]stack
etc. have given me the right final shape. I also looked into vectorization but as far as I can see that just implements a loop under the hood?
Example with random numbers and an input array of shape (3,2,3)
In[1]: m = 3
n = 2
r = 3
In[2]: a = np.random.random((m,n,r))
In[3]: a
Out[3]:
array([[[0.8416, 0.3694, 0.5708],
[0.3779, 0.579 , 0.207 ]],
[[0.7871, 0.6547, 0.0047],
[0.1115, 0.1445, 0.6147]],
[[0.8538, 0.2821, 0.8094],
[0.6214, 0.0147, 0.5852]]])
In[4]: a.shape
Out[4]: (3, 2, 3)
In[4]: new_a = np.empty(shape=(m,n), dtype=object)
for i in range(m):
for j in range(n):
new_a[i,j] = a[i,j,:]
In[5]: new_a
Out[5]:
array([[array([0.8416, 0.3694, 0.5708]), array([0.3779, 0.579 , 0.207 ])],
[array([0.7871, 0.6547, 0.0047]), array([0.1115, 0.1445, 0.6147])],
[array([0.8538, 0.2821, 0.8094]), array([0.6214, 0.0147, 0.5852])]],
dtype=object)
In[6]: new_a.shape
Out[6]: (3, 2)
r == 3
and the[x, y, z]
coordinates go along ther
axis? And you want to change it into what?for
loop solution. I think you can do this with amoveaxis
andreshape
, but it's not clear whether the notation is consistent anove.m*n*r
won't in general equalm*n
, so you can't collapse an array with shape(m, n, r)
to an array with shape(m, n)
as requested (unless you want an object array in which element is itself an array).[n, m, 3] dtype=float
you want an array of size[n, m] dtype=array
? Why would you want to do that?