OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

compare numpy array against list of values

  • Thread starter Thread starter toto
  • Start date Start date
T

toto

Guest
I would like to have your help about a problem I have with a numpy array.

In my code I have a huge array made of integers, this is the result of a skimage labelling process.

The idea is that I want to generate a boolean array with same shape as the input array containing a True value if the cell value belong to a given list of good labels, and false otherwise.

It is much easier with a small example:

Code:
import numpy as np
input_array = np.arange(15, dtype=np.int64).reshape(3, 5)
good_labels = [1, 5, 7]
mask_output = np.zeros(input_array.shape).astype(int)
for l in good_labels:
    masked_input = (input_array == l).astype(int)
    mask_output += masked_input
mask_output = mask_output > 0

This code works, but it is extremely slow because input_array is huge and good_labels is also very long, so I need to loop too much.

Is there a way to make it faster?

I was thinking to replace the whole loop with something like

Code:
mask_output = input_array in good_labels

But this does not work as expected.

Can you help me?
<p>I would like to have your help about a problem I have with a numpy array.</p>
<p>In my code I have a huge array made of integers, this is the result of a skimage labelling process.</p>
<p>The idea is that I want to generate a boolean array with same shape as the input array containing a True value if the cell value belong to a given list of good labels, and false otherwise.</p>
<p>It is much easier with a small example:</p>
<pre class="lang-py prettyprint-override"><code>import numpy as np
input_array = np.arange(15, dtype=np.int64).reshape(3, 5)
good_labels = [1, 5, 7]
mask_output = np.zeros(input_array.shape).astype(int)
for l in good_labels:
masked_input = (input_array == l).astype(int)
mask_output += masked_input
mask_output = mask_output > 0
</code></pre>
<p>This code works, but it is extremely slow because input_array is huge and good_labels is also very long, so I need to loop too much.</p>
<p>Is there a way to make it faster?</p>
<p>I was thinking to replace the whole loop with something like</p>
<pre><code>mask_output = input_array in good_labels
</code></pre>
<p>But this does not work as expected.</p>
<p>Can you help me?</p>
 

Latest posts

S
Replies
0
Views
1
Sergey Bakaev Rettley
S
Top