Numpy "Where" function can not avoid evaluate Sqrt(negative)
numpy @ operator
It seems that the
np.where function evaluates all the possible outcomes first, then it evaluates the condition later. This means that, in my case, it will evaluate square root of -5, -4, -3, -2, -1 even though it will not be used later on.
My code runs and works. But my problem is the warning. I avoided using a loop to evaluate each element, because it will run much slower than
So, here, I am asking
- Is there any way to make
np.whereevaluate the condition first?
- Can I turn off just this specific warning? How?
- Another better way to do it if you have a better suggestion.
Here just a short example code corresponding my real code which is gigantic. But essentially has the same problem.
import numpy as np c=np.arange(10)-5 d=np.where(c>=0, np.sqrt(c) ,c )
RuntimeWarning: invalid value encountered in sqrt d=np.where(c>=0,np.sqrt(c),c)
There is a much better way of doing this. Let's take a look at what your code is doing to see why.
np.where accepts three arrays as inputs. Arrays do not support lazy evaluation.
d = np.where(c >= 0, np.sqrt(c), c)
This line is therefore equivalent to doing
a = (c >= 0) b = np.sqrt(c) d = np.where(a, b, c)
Notice that the inputs are computed immediately, before
where ever gets called.
Luckily, you don't need to use
where at all. Instead, just use a boolean mask:
mask = (c >= 0) d = np.empty_like(c) d[mask] = np.sqrt(c[mask]) d[~mask] = c[~mask]
If you expect a lot of negatives, you can copy all the elements instead of just the negative ones:
d = c.copy() d[mask] = np.sqrt(c[mask])
An even better solution might be to use masked arrays:
d = np.ma(c, c < 0) d = np.ma.sqrt(d)
To access the whole data array, with the masked portion unaltered, use
NumPy, numpy 1.18.4. pip install numpy. Copy PIP instructions. Latest version. Released: May 3, 2020. NumPy is the fundamental package for array computing with Python. Project description. Project details. Release history. Project description. a powerful N-dimensional array object. sophisticated (broadcasting) functions. tools for integrating C/C++ and Fortran code. useful linear algebra, Fourier transform, and random number capabilities.
This is answer to your 2nd question.
Yes you can turn off the warnings. Use warnings module.
import warnings warnings.filterwarnings("ignore")
Numpy and Scipy, NumPy is a python library used for working with arrays. It also has functions for working in domain of linear algebra, fourier transform, and matrices. NumPy was NumPy (pronounced / ˈ n ʌ m p aɪ / (NUM-py) or sometimes / ˈ n ʌ m p i / (NUM-pee)) is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
One solution is to not use
np.where, and use indexing instead.
c = np.arange(10)-5 d = c.copy() c_positive = c > 0 d[c_positive] = np.sqrt(c[c_positive])
numpy/numpy: The fundamental package for scientific , We can create a NumPy ndarray object by using the array() function. Example. import numpy as np arr = np.array([1, 2, NumPy, which stands for Numerical Python, is a library consisting of multidimensional array objects and a collection of routines for processing those arrays. Using NumPy, mathematical and logical operations on arrays can be performed. This tutorial explains the basics of NumPy such as its
np.sqrt is a
ufunc and accepts a
where parameter. It can be used as a mask in this case:
In : c = np.arange(10)-5.0 In : d = c.copy() In : np.sqrt(c, where=c>=0, out=d); In : d Out: array([-5. , -4. , -3. , -2. , -1. , 0. , 1. , 1.41421356, 1.73205081, 2. ])
In contrast to the
np.where case, this does not evaluate the function at the ~where elements.
Python Numpy Tutorial (with Jupyter and Colab), What is NumPy? NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with The most up-to-date NumPy documentation can be found at Latest (development) version. It includes a user guide, full reference documentation, a developer guide, and meta information. Other links: NumPy Enhancement Proposals (which include the NumPy Roadmap and detailed plans for major new features).
numpy · PyPI, NumPy is a module for Python. The name is an acronym for "Numeric Python" or "Numerical Python". It is pronounced /ˈnʌmpaɪ/ (NUM-py) or less often Python Numpy. Numpy is a general-purpose array-processing package.It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python.
Introduction to NumPy, NumPy is a Python Library/ module which is used for scientific calculations in Python programming. In this tutorial, you will learn how to perform many operations on NumPy arrays such as adding, removing, sorting, and manipulating elements in many ways. NumPy provides a multidimensional array object and other derived arrays such as masked
NumPy Creating Arrays, For each official release of NumPy and SciPy, we provide source code (tarball), as well as binary wheels for several major platforms (Windows, OSX, Linux). Available packages. Download location. Official source code (all platforms) and binaries for Windows , Linux and Mac OS X. PyPI page for NumPy. Official source code (all platforms) and
- According to numpy documentation, the statement
d=np.where(c>=0, np.sqrt(c) ,c )is equivalent to
[sqcv if cond else cv for (cond, sqcv, cv) in zip(c>=0, np.sqrt(c), c)]. In other words, the term
np.sqrt(c)is evaluated regardless of the condition
- Hi, Thanks. It works well. But when I try your last method. I still need the negative value.
d2=np.ma.masked_array(c.copy(), c>0). Then d=d1+d2. It do not work. Would you help help bit more?
- I've added a note at the end