Changing values of indexes by magnitude
<p>I'm trying to make a flexible algorithm that will take values out of a 50 by 50 array (which contains pixel values from a fits image) if they are too high. 
they are too high (in python). The first thing I tried to do was this:</p>

<pre><code>file = pf.open('/Users/Vofun/desktop/file.fits')
data = np.array(file[0].data)
for pixellist in range (len(data)):
        if data[pixellist] &gt; 50:
           data[pixellist] = 10:
</code></pre>

<p>of course, that didn't work, and I got</p>

<blockquote>
  <p>ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()</p>
</blockquote>

<p>the problem is, if I do that, I have no idea how to tell it which value to replace if I use a.any(). So far I think what I'll need is:</p>

<pre><code>if a.any(data) &gt; 50:
</code></pre>

<p>and then a line of code telling it to replace the value with ten, but I'm not sure how to tell it to do that for the pixel it found, because I'm kind of terrible at coding. How would I reduce the value of the indexes based on if their value exceeds 50?</p>

<p>You can use <a href="http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#boolean-array-indexing" rel="nofollow">boolean indexing</a> to change your data.</p>

<pre><code>import numpy as np
a = np.random.random_integers(40, 60, (5,5))

&gt;&gt;&gt; a
array([[58, 58, 43, 56, 54],
       [59, 40, 42, 52, 45],
       [50, 60, 43, 48, 52],
       [55, 48, 57, 41, 47],
       [57, 55, 43, 54, 42]])
&gt;&gt;&gt;
&gt;&gt;&gt; a &gt; 50
array([[ True,  True, False,  True,  True],
       [ True, False, False,  True, False],
       [False,  True, False, False,  True],
       [ True, False,  True, False, False],
       [ True,  True, False,  True, False]], dtype=bool)
&gt;&gt;&gt;
&gt;&gt;&gt; a[a &gt; 50] = 0
&gt;&gt;&gt; a
array([[42,  0,  0,  0, 48],
       [ 0, 46,  0,  0, 49],
       [50,  0,  0, 44, 43],
       [46, 46,  0, 43, 49],
       [ 0,  0,  0,  0, 48]])
&gt;&gt;&gt; 
</code></pre>

python python-2.7 numpy multidimensional-array