-1

So I have a 2D Numpy array i want to modify with classical conditional indexing:

array[array>threshold] = np.nan

But i want to implement a conditional that utilizes the index of the element, so as to compare with a different array. For example:

arr[ arr[i] * scale[i] > threshold] = np.nan

How would i do that?

I tried something with enumeration, but the delays became enormous.

4
  • Could you give us a (preferably small) example of what you'd like to do? It's okay to give a slow pseudocode version. Commented Oct 31, 2023 at 11:21
  • I want to implement different thresholds for each element, sort of. Specifically, I want to scale each of my elements by a different scaler before comparing to the threshold. arr[ arr[i] * scale[i] > threshold] = np.nan Commented Oct 31, 2023 at 11:49
  • 3
    I think you might just be able to write arr[ arr * scale > threshold] = np.nan? It depends a bit on the shape of the scale array Commented Oct 31, 2023 at 12:29
  • Alternatively, you could also make the threshold an array instead of a scalar. Commented Oct 31, 2023 at 12:31

2 Answers 2

0

For one-dimensional arrays you could use np.where to get the indices of the array and apply those in the conditional indexing.

array = np.random.randn(5)
>>> [-0.65572047  0.39725663 -0.2727869  -1.62292975  0.12211058]

# Use np.where to return indices
np.where(array)[0]
>>> [0 1 2 3 4]

# Condition can include indices like this:
array[(array * np.where(array)[0]) > threshold] = np.nan

You can extend this solution to two-dimensional arrays, however you need to determine how you want to define a two-dimensional index in your condition.

Here are the two indices (one for each dimension) you could plug into the conditional indexing:

array = np.random.rand(5, 5)
indices = np.where(array)[0].reshape((5, 5))
print(indices)
>>> [[0 0 0 0 0]
     [1 1 1 1 1]
     [2 2 2 2 2]
     [3 3 3 3 3]
     [4 4 4 4 4]]
indices = np.where(array)[1].reshape((5, 5))
print(indices)
>>> [[0 1 2 3 4]
     [0 1 2 3 4]
     [0 1 2 3 4]
     [0 1 2 3 4]
     [0 1 2 3 4]]
Sign up to request clarification or add additional context in comments.

Comments

0

Based on comment and revised question, this might be what you need


import numpy as np

arr = np.random.rand(5, 5)
indices_x, indices_y = np.indices(arr.shape, sparse=True)

# return all elements from arr where x indices * 1.5 is greater than 2, similar for y indices
arr[indices_x[indices_x * 1.5 > 2], indices_y[indices_y * 1.3 > 2]]

2 Comments

Excuse me, I wasn't very clear. I want to compare each element to a threshold (number) and make any excessive elements NaN. But let's say that there is a different threshold for each element, so I need the variable index in the conditional, not slicing based on index.
I updated the answer, is this what you were looking for?

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.