# Boolean Operation in NumPy

Hi Learning Enthusiasts! In this article we learn about Boolean Operation in NumPy. Boolean Operations in NumPy includes everything that we need to do manipulation on provided data or arrays — it is the foundation of data manipulation in NumPy. Now, let’s learn about Boolean Operations in NumPy.

More importantly, to learn more about NumPy’s functions & operations you can read some of these articles:

**Topics to be covered in this article are as follows:**

- Comparison Operators
- Filtering values using Universal Functions
- Boolean Operators

## Comparison operators for Boolean operations

Because NumPy, just like any other programming language, uses standard Comparison or Boolean operations similar to mathematical operations, it returns `TRUE`

or `FALSE`

depending upon our condition.

**“Greater Than” operator** — `>`

`>`

For value_1 greater than value_2 we show it as follows:

`value_1`

`>`

`value_2`

Now, lets apply same logic on an array and see at which location values are actually greater than given scalar value.

Let’s begin with creating a NumPy array.

```
import numpy as np
# Base array
x = np.arange(0,10,2)
x
```

```
# Checking at which locations values are greater than given scalar value
x > 3
```

As you can see, in above example we are getting `TRUE`

or `FALSE`

depending upon our condition. That, is it is returning true TRUE for locations having value greater than `3`

and FALSE otherwise.

It tells us an important fact, that Comparison Operators returns us only BOOLEAN VALUES.

Now, let’s try out other operations too.

**“Less Than” operator** — `<`

`<`

For value_1 less than value_2 we show it as follows:

`value_1`

`<`

`value_2`

```
# Checking at which locations values are smaller than given scalar value
x < 3
```

**“Equal To” operator** — `==`

`==`

For value_1 equal to value_2 we show it as follows:

`value_1`

`==`

`value_2`

```
# Checking at which locations values are equal to given scalar value
x == 4
```

**“Greater Than Equal To” operator** — `>=`

`>=`

For value_1 greater than equal to value_2 we show it as follows:

`value_1`

`>=`

`value_2`

```
# Checking at which locations values are greater than equal to given scalar value
x >= 6
```

**“Less Than Equal To” operator** — `<=`

`<=`

For value_1 less than equal to value_2 we show it as follows:

`value_1`

`<=`

`value_2`

```
# Checking at which locations values are less than equal to given scalar value
x <= 6
```

**“Not-Equal To” operator** — `!=`

`!=`

For value_1 not equal to value_2 we show it as follows:

`value_1`

`!=`

`value_2`

```
# Checking at which locations values are not equal to given scalar value
x != 4
```

That covers all our Comparison Operators.

## Filtering values using Universal Functions

Let’s move on to our next topic of filtering values from a given set of numbers or an array using Universal Functions. To learn more about Universal Functions please check following article Universal Functions in NumPy.

To count total number of values greater than any value in an array you can use following function `np.count_nonzero()`

. That is, we will be counting number `TRUE`

values after performing a comparison on those values.

Let’s count number of values greater than `3`

in our array `x`

.

```
x
```

```
# Compare values greater than 3
y = x > 3
y
```

```
# Count values in array greater than 3
np.count_nonzero(y)
```

As you can see, in above example we merged 2 things together, that is, first compared values and then passed that result in our function `np.count_nonzero()`

. You can also do this in a single step, as shown below.

```
# Count values in array greater than 3
np.count_nonzero(x > 3)
```

** Count Number of Values Greater than 3 in each Row**

It can also be achieved in similar manner, only change we will be doing here will be use of a new function called `np.sum()`

```
# Calculate Sum of Values Greater than 5
np.sum(x > 5)
```

It clearly indicated that we have 2 values greater than 5.

**Why it didn’t calculate sum of values?**

— Because we are not applying `np.sum()`

on numerical values, however on an array consisting of Boolean values `TRUE`

& `FALSE`

Now, let’s try calculating sum of those values.

**Calculate Sum of Values Greater than 5**

We will be same function `np.sum()`

only. To access value from each of those locations we will be using following format `x[x > 5]`

Now, what we have done here is:

- Applied comparison operator get an Array of Boolean values indicating
`TRUE`

for locations for which condition satisfies - Then to access those
`TRUE`

valued locations we have passed it in our array X.

Let’s see same in steps through example.

```
x
```

```
# Location of Values Greater than 5
x > 5
```

```
# Fetching values at those locations having value = TRUE
x[ x > 5 ]
```

```
# Calculate Sum of All Values Greater than 5
np.sum(x[ x > 5 ])
```

**Are ALL Values Greater than 5 **

For this we will use following function `np.all()`

. It will return `TRUE`

if all values passed in it correspond to TRUE.

```
# Are ALL Values Greater than 5
np.all(x > 5)
```

We received `FALSE`

because we also have values smaller than 5.

**ANY value Greater than 5 **

For this we will use following function `np.any()`

. It will return `TRUE`

if we have even single TRUE value (any TRUE value) among values passed in it.

```
# Any values Greater than 5
np.any(x > 5)
```

It returned `TRUE`

because we have at least 2 values (6 & 8) which are greater than 5.

Similarly, you can use any Comparison Operator and pass that output to above functions to get desired result.

## Boolean Operators

What if you want to calculcate sum of values greater than 3 & less than 20? What if you want calculate sum of values which either smaller than 3 or greater than 20?

These problems can be solved by combining multiple Comparison Operations using Boolean Operators.

Boolean Operators are in general of 4 types:

`&`

— it’s a Bitwise AND operator — function`np.bitwise_and`

`|`

— the Bitwise OR operator — function`np.bitwise_or`

`^`

— Bitwise XOR operator — function`np.bitwise_xor`

`~`

— Bitwise NOT operator — function`np.bitwise_not`

In this article we will be using Universal Functions only, that is, symbolic representation as they are more handy to use & faster to type.

Let’s create a sample array first

```
x = np.arange(1, 40)
x
```

**Calculate sum of all values which are smaller than 5 or greater than 38**

For this we will be using following Boolean Operator — `|`

— bitwise OR operator.

First let’s get result without using Boolean Operator — many steps.

```
# values smaller than 5
y_small = x [x < 5]
print("-- Values Smaller than 5")
print(y_small)
# values greater than 38
y_big = x [x > 38]
print("\n-- Values Greater than 38")
print(y_big)
print("\n--Calculating Sum of Smaller values")
z_small = np.sum(y_small)
print(z_small)
print("\n--Calculating Sum of Larger values")
z_big = np.sum(y_big)
print(z_big)
print("\n--Sum of desired values")
print(z_small + z_big )
```

**Using Boolean Operator** to obtain same *result in a single line of code*.

*result in a single line of code*

```
# Calculate sum of all values which are smaller than 5 & greater than 38
np.sum(x[ (x < 5) | (x > 38)])
```

So, you can clearly see the advantage of Boolean Operator over standard procedure.

Similarly you can combine any numbers of equations together and calculate desired value.

For example, if we want to add one more condition where we don’t want to add value = 2.

```
np.sum(x[ ((x < 5) | (x > 38)) & (x != 2)])
```

This covers our current article. In next article we will be covering more about NumPy and its operations.

Stay tuned! Keep Learning!

You can also watch all our tutorials on YouTube @ ML for Analytics