In the last article, we learned many different ways in which we can create ndarrays. Now that we know how to create NumPy arrays it's time to start playing around with them.
We will learn to perform basic operations, a task you can divide into 3 categories:
- Operations between arrays of the same size.
- Operations between an array and a scalar.
- Operations between arrays of different sizes.
The third scenario is known as broadcasting, and we won't deal with it in this article, instead, we will focus on the first two.
Great, let's get started.
Operations using ndarrays of the same size
Operations between arrays of the same size are applied in an element-wise fashion. We will create two arrays and see how subtraction, addition, multiplication, and division work.
import numpy as np arr_1 = np.array([1, 2, 3, 4, 5]) arr_2 = np.array([20, 30, 40, 50, 60])
# Element-wise sum of two arrays sum_result = arr_1 + arr_2 print(sum_result)
[21 32 43 54 65]
# Element-wise substraction of two arrays sub_result = arr_2 - arr_1 print(sub_result)
[19 28 37 46 55]
# Element-wise multiplication of two arrays mult_result = arr_1 * arr_2 print(mult_result)
[ 20 60 120 200 300]
# Element-wise division of two arrays div_result = arr_1 / arr_2 print(div_result)
[0.05 0.06666667 0.075 0.08 0.08333333]
# Element-wise power pow_result = arr_2 ** arr_1 print(pow_result)
[ 20 900 64000 6250000 777600000]
You can also perform comparisons between two arrays. The result is a boolean array where every entry is the result of performing an element-wise comparison:
arr_1 = np.array([1, 10, 2, 20]) arr_2 = np.array([50, 5, 30, 3]) # Return True if the elements in the first array are larger than the ones in the second array comparison_result = arr_1 > arr_2 print(comparison_result)
[False True False True]
Now, there are two additional things to note.
The first one is that you can chain operations with as many arrays as you want:
arr_1 = np.array([1,4,7]) arr_2 = np.array([2,5,8]) arr_3 = np.array([3,6,9]) sum_result = arr_1 + arr_2 + arr_3 print(sum_result)
[ 6 15 24]
The second one is that you can use the same array several times when defining the operation:
arr = np.array([1, 5, 10]) result = arr * arr - arr # This applies the operation x^2 - x to every entry print(result)
[ 0 20 90]
Good, now let's see what happens when we perform these operations with an array and a scalar.
Operations using ndarrays of the same size
Operations between an array and a scalar are very intuitive: they are performed over every entry in the array and the scalar, and the result is a boolean array. This is much easier to understand with examples:
arr = np.arange(1, 6) # Generate entries from 1 to 5 print(arr)
[1 2 3 4 5]
# Sum of an array and a scalar sum_result = arr + 10 print(sum_result)
[11 12 13 14 15]
# Substraction between an array and a scalar sub_result = arr - 1 print(sub_result)
[0 1 2 3 4]
# Like the rest of the operations, substractions works as you would expect if you invert the order sub_result = 1 - arr print(sub_result)
[ 0 -1 -2 -3 -4]
# Multiplication between an array and a scalar mult_result = arr * 5 print(mult_result)
[ 5 10 15 20 25]
# Division between an array and a scalar div_result = arr / 2 print(div_result)
[0.5 1. 1.5 2. 2.5]
# Again, the order of the elements is important, just as you would expect div_result = 2 / arr print(div_result)
[2. 1. 0.66666667 0.5 0.4 ]
Comparisons between a scalar work as you would expect. The comparison returns a boolean array with the result of evaluating the comparison over every element in the original array.
result = arr > 3 print(result)
[False False False True True]
Pretty straightforward, isn't it?
One of the best things about NumPy is how consistent and usually obvious it is, and these basic operations are proof of that.
Knowing how to perform basic operations is the foundation you need to build more elaborate solutions, and now you got that covered. In the next article, we will learn about an extremely important topic: Indexing and slicing. See you then.
Thanks for reading!
What to do next
- Share this article with friends and colleagues. Thank you for helping me reach people who might find this information useful.
- You can find the source code for this series in this repo.
- This article is based on Python for Data Analysis. These and other very helpful books can be found in the recommended reading list.
- Send me an email with questions, comments or suggestions (it's in the About Me page)