Arithmetic and Logical Operators#


Arithmetic Operators#

Just like pandas, numpy supports convenient usage of mathematical and logical operators on numpy.arrays. The following code cell shows and explains some of the most common operations.

import numpy as np

x = np.arange(4)
print('x')
print(x)
print()

# Add a number to each element in the array
y = x + 3
print('y = x + 3')
print(y)
print()

# Multiply two arrays element-by-element
# Works with any mathematical operation (+, -, *, /, //, **)
z = x * y
print('z = x * y')
print(z)
print()

# This also works for arrays with multiple dimensions
m = x.reshape((2, 2))

print('m / 2')
print(m / 2)
print()

# Order of operations still applies. Same as (m * 3) + (m / 2)
print('m * 3 + m / 2')
print(m * 3 + m / 2)
print()

Logical Operators#

You can also use logical operators ( == , < , >= ) to compare elements of numpy.arrays. You can use & (and), | (or), and ~ (not) just like pandas.

import numpy as np

x = np.arange(4)
print('x')
print(x)
print()

# Comparison
print('x < 3')
print(x < 3)
print()

# Using & (still requires parentheses)
print('(x < 3) & (x % 2 == 0)')
print((x < 3) & (x % 2 == 0))
print()

Not surprisingly, just like pandas, you can use these numpy.arrays of bool values to filter down to certain values in the numpy.array!

import numpy as np

x = np.arange(10)
print('x')
print(x)
print()

mask = (x < 3) & (x % 2 == 0)
print('mask')
print(mask)
print()

y = x[mask]
print('y = x[mask]')
print(y)

Note

We commonly compare numpy and pandas since they were designed to be similar. Since we learned pandas first, we commonly refer to numpy as being similar to pandas. However, historically numpy came first so it’s actually pandas that borrowed a lot of the terminology/syntax since it came out later!