0

I have a numpy array t_i of shape (6, 2000, 2). I need to delete column slices from the array if all the elements in that slice below that row is -1. Let's say I am in ith row of t_i. Now for those columns below the ith row where every element in the column is [-1,-1] I need to delete the complete column slice from t_i.

I tried this:

t_iF = t_i[:, t_i[i+1:] != -1]

But I am getting this error:

IndexError: too many indices for array: array is 3-dimensional, but 4 were indexed

Can you all please point me to the right direction? Thanks!

Updated: Let t_i be:

t1=np.arange(32).reshape(4,4,2)
t1[1:,[1,3]] = -1

In this case the whole 1 and 3 column slices need to be deleted.

7
  • Look at : t_i[i+1:] != -1. That will give you a clue. Commented Apr 9, 2021 at 17:36
  • 1
    Your description is unclear. Please add an example of source data and the expected result. Another problem is that your code looks rather like pseudo-code, e.g. value of i is unknown. Commented Apr 9, 2021 at 17:36
  • @adr, yes t_i[i+1:] != -1 returns a 3D tensor while I require only the column indices which satisfy the inequality. Can you suggest how I can do that? Thanks! Commented Apr 9, 2021 at 17:46
  • I am trying to add one. Actually I need to do this update over a loop(i.e. perform this step for a range of values of i). So i varies over a range. Thanks! Commented Apr 9, 2021 at 17:47
  • Define and input and output to go with the prose. Commented Apr 9, 2021 at 18:48

1 Answer 1

1

np.any can be very helpful for tasks like these:

import numpy as np

t1 = np.arange(32).reshape(4, 4, 2)
t1[1:, [1, 3]] = -1

t_filtered = t1[:, [np.any(t1[i, i:] != -1) for i in range(t1.shape[0])], :]
print(t_filtered.shape) # (4, 3, 2)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Stefan Thanks for the help. Actually its t1[1:,[1,3]] = -1 so we don't have the whole column slice as -1 instead we have all elements below a particular row in that slice as -1.
The problem description is still somewhat unclear. I have updated the answer now though, hoping this is what you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.