Given a NumPy array consisting of Boolean values. How to convert it to an integer array?
- Convert each
Truevalue to integer
- Convet each
Falsevalue to integer
Here’s an example Boolean array:
>>> import numpy as np >>> a = np.array([True, False]) >>> a array([ True, False])
What you want is the following integer array:
>>> a array([1, 0])
Let’s examine some methods to accomplish this easily.
Method 1: Don’t Do It
Python internally represents Booleans using integers 1 and 0 for
False, respectively. This means that you may not even need to convert your Boolean array to an integer array because all integer operations already work on the Boolean array as well!
For example, you can add a Boolean array and an integer array and the result is an integer array already. In the following code snippet, you add the array
[True False False] to the array
[3 2 1] and obtain the result
[4 2 1] because each
True value is represented by integer 1 and each
False value is represented by integer 0.
>>> a = np.array([True, False, False]) >>> b = np.array([3, 2, 1]) >>> a + b array([4, 2, 1])
Okay, still here? 😉 If you actually want to convert your Boolean array to an integer array, read on! 🙂
Method 2: Multiplication with 1
Python NumPy uses broadcasting to allow array multiplication with a scalar. The scalar is then multiplied with every single value in the array. Data types of the scalar and the array are combined on a best-fit basis.
This means that you can simply multiply your Boolean array with an integer to obtain an array of integers! In particular, if you multiply with integer 1, each Boolean
True is transformed to integer 1 and
False to integer 0. For example, the expression
1 * np.array([True, False]) results in the integer array
Here’s the same example as a code snippet:
>>> import numpy as np >>> a = np.array([True, False]) >>> a * 1 array([1, 0])
You can see that Python creates a new integer array from the result of the multiplication of the Boolean array and the scalar 1.
This is the simplest way to convert a Boolean array to an integer array and it is what I would do if I actually had to convert the array—and couldn’t just work with the Boolean array in the first place. 😉
Method 3: Use array.astype(int)
Arguably, this is the most standard canonical way to do the job.
To convert a Boolean array a to an integer array, use the
a.astype(int) method call. The single argument
int specifies the desired data type of each array item. NumPy converts on a best-effort basis. If the original array only contains Booleans, it’ll result in an array of integers so that each
True value converts to integer
1 and each
False value to integer
This strategy is shown in the following code snippet:
>>> import numpy as np >>> a = np.array([True, False]) >>> a.astype(int) array([1, 0])
You can see that the method returns a new array of integers rather than working on an existing array. The original array remains unchanged:
>>> a array([ True, False])
This is a clean, readable, and canonical way to convert a Boolean array to an integer array in NumPy. And every reliable source would recommend this method as the best one. Yet, in this particular problem instance, I think the above two methods 1 and 2 are superior if applicable to your specific scenario. No conversion is better than a redundant one, don’t you think?
Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.