Convert a numpy array into an array of signs with 0 as positive
<p>I have a large numpy array with positive data, negative data and 0s. I want to convert it to an array with the signs of the current values such that 0 is considered positive. If I use <code>numpy.sign</code> it returns 0 if the current value is 0 but I want something that returns 1 instead. Is there an easy way to do this?</p>

<p>If <code>x</code> is the array, you could use <code>2*(x &gt;= 0) - 1</code>.</p>

<p><code>x &gt;= 0</code> will be an array of boolean values (i.e. <code>False</code> and <code>True</code>), but when you do arithmetic with it, it is effectively cast to an array of 0s and 1s.</p>

<p>You could also do <code>np.sign(x) + (x == 0)</code>.  (Note that <code>np.sign(x)</code> returns floating point values, even when <code>x</code> is an integer array.)</p>

python numpy