How to Use Conditional Expressions With NumPy where() :
by:
blow post content copied from Real Python
click here to view original post
The NumPy where()
function is a powerful tool for filtering array elements in lists, tuples, and NumPy arrays. It works by using a conditional predicate, similar to the logic used in the WHERE or HAVING clauses in SQL queries. It’s okay if you’re not familiar with SQL—you don’t need to know it to follow along with this tutorial.
You would typically use np.where()
when you have an array and need to analyze its elements differently depending on their values. For example, you might need to replace negative numbers with zeros or replace missing values such as None
or np.nan
with something more meaningful. When you run where()
, you’ll produce a new array containing the results of your analysis.
You generally supply three parameters when using where()
. First, you provide a condition against which each element of your original array is matched. Then, you provide two additional parameters: the first defines what you want to do if an element matches your condition, while the second defines what you want to do if it doesn’t.
If you think this all sounds similar to Python’s ternary operator, you’re correct. The logic is the same.
Note: In this tutorial, you’ll work with two-dimensional arrays. However, the same principles can be applied to arrays of any dimension.
Before you start, you should familiarize yourself with NumPy arrays and how to use them. It will also be helpful if you understand the subject of broadcasting, particularly for the latter part of this tutorial.
In addition, you may want to use the data analysis tool Jupyter Notebook as you work through the examples in this tutorial. Alternatively, JupyterLab will give you an enhanced notebook experience, but feel free to use any Python environment.
The NumPy library is not part of core Python, so you’ll need to install it. If you’re using a Jupyter Notebook, create a new code cell and type !python -m pip install numpy
into it. When you run the cell, the library will install. If you’re working at the command line, use the same command, only without the exclamation point (!).
With these preliminaries out of the way, you’re now good to go.
Get Your Code: Click here to download the free sample code that shows you how to use conditional expressions with NumPy where().
Take the Quiz: Test your knowledge with our interactive “How to Use Conditional Expressions With NumPy where()” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
How to Use Conditional Expressions With NumPy where()This quiz aims to test your understanding of the np.where() function. You won't find all the answers in the tutorial, so you'll need to do additional research. It's recommended that you make sure you can do all the exercises in the tutorial before tackling this quiz. Enjoy!
How to Write Conditional Expressions With NumPy where()
One of the most common scenarios for using where()
is when you need to replace certain elements in a NumPy array with other values depending on some condition.
Consider the following array:
>>> import numpy as np
>>> test_array = np.array(
... [
... [3.1688358, 3.9091694, 1.66405549, -3.61976783],
... [7.33400434, -3.25797286, -9.65148913, -0.76115911],
... [2.71053173, -6.02410179, 7.46355805, 1.30949485],
... ]
... )
To begin with, you need to import the NumPy library into your program. It’s standard practice to do so using the alias np
, which allows you to refer to the library using this abbreviated form.
The resulting array has a shape of three rows and four columns, each containing a floating-point number.
Now suppose you wanted to replace all the negative numbers with their positive equivalents:
>>> np.where(
... test_array < 0,
... test_array * -1,
... test_array,
... )
array([[3.1688358 , 3.9091694 , 1.66405549, 3.61976783],
[7.33400434, 3.25797286, 9.65148913, 0.76115911],
[2.71053173, 6.02410179, 7.46355805, 1.30949485]])
The result is a new NumPy array with the negative numbers replaced by positives. Look carefully at the original test_array
and then at the corresponding elements of the new all_positives
array, and you’ll see that the result is exactly what you wanted.
Note: The above example gives you an idea of how the where()
function works. If you were doing this in practice, you’d most likely use either the np.abs()
or np.absolute()
functions instead. Both do the same thing because the former is shorthand for the latter:
>>> np.abs(test_array)
array([[3.1688358 , 3.9091694 , 1.66405549, 3.61976783],
[7.33400434, 3.25797286, 9.65148913, 0.76115911],
[2.71053173, 6.02410179, 7.46355805, 1.30949485]])
Once more, all negative values have been removed.
Before moving on to other use cases of where()
, you’ll take a closer look at how this all works. To achieve your aim in the previous example, you passed in test_array < 0
as the condition. In NumPy, this creates a Boolean array that where()
uses:
>>> test_array < 0
array([[False, False, False, True],
[False, True, True, True],
[False, True, False, False]])
The Boolean array, often called the mask, consists only of elements that are either True
or False
. If an element matches the condition, the corresponding element in the Boolean array will be True
. Otherwise, it’ll be False
.
Read the full article at https://realpython.com/numpy-where-conditional-expressions/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
September 11, 2024 at 07:30PM
Click here for more details...
=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================
Post a Comment