简体   繁体   中英

Remove column from a 3D array with varied length for every first-level index (Python)

I got a np.ndarray with ~3000 trajectories. Each trajectory has x, y and z coordinates and a different length; between 150 and 250 (points in time). Now I want to remove the z coordinate for all of these trajectories.

So arr.shape gives me (3000,) ,(3000 trajectories) and (for example) arr[0].shape yields (3,178) (three axis of coordinates and 178 values).

I have found multiple explanations for removing lines in 2D-arrays and I found np.delete(arr[0], 2, axis=0) working for me. However, I don't just want to delete the z coordinates for the first trajectory; I want to do this for every trajectory.

If I want to do this with a loop for arr[i] I would need to know the exact length of every trajectory (It doesn't suit my purpose to just create the array with the length of the longest and fill it up with zeroes).

TL;DR: So how do I get from a ndarray with [amountOfTrajectories][3][value] to [amountOfTrajectories][2][value] ?

The purpose is to use these trajectories as labels for a neural net that creates trajectories. So I guess it's a entirely new question but is the shape I'm asking for suitable for usage as labels for tensorflow?

Also: What would have been a better title and some terms to find results for this with google? I just started with Python and I'm afraid I'm missing some keywords here...

If this comes from loadmat , the source is probably a MATLAB workspace with a cell , which contains these matrices.

loadmat has, evidently created a 1d array of object dtype (the equivalent of a cell , with squeeze on).

A 1d object array is similar to a Python list - it contains pointers to arrays else where in memory. Most operations on such an array use Python iteration. Iterating on the equivalent list is usually faster. ( arr.tolist() ).

alist = [a[:2,:] for a in arr]

should give you a list of arrays, each of shape (2, n) ( n varying). This makes new arrays - but then so does np.delete .

You can't operate on all arrays in the 1d array with one operation. It has to be iterative.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM