Intro to Django for Beginners
■Episode 13: Arithmetic Operations with NumPy
(Last updated: 2023.06.07)
Image of Django framework
You can read this article in 4 minutes!
(If the image looks small, try turning your phone sideways)
“Let’s do calculations with NumPy!”
In the previous article, we learned about basic operations with NumPy arrays, such as initialization, creating equally spaced values, indexing and slicing, and reshaping arrays. In this article, we’ll learn how to perform arithmetic operations, broadcasting, dot products, and matrix multiplication.
[Table of Contents]
Arithmetic Operations with NumPy Arrays
Broadcasting in NumPy
Dot Product and Matrix Multiplication with NumPy Arrays
Summary
1. Arithmetic Operations with NumPy Arrays
NumPy makes it easy to perform element-wise operations using standard arithmetic operators like +, -, *, and /. Below is a basic example of such operations. As you can see, the calculation is done between elements in the same position. In addition to these, you can also use exponentiation (**), floor division (//), and modulo (%).
Arithmetic operations with NumPy
Arithmetic Operations with NumPy Arrays
Furthermore, NumPy offers various mathematical functions such as sin, cos, exp, log, etc., all of which operate element-wise on arrays. For example, `np.sin(np.radians([0, 90]))` will compute [0, 1], corresponding to sin(0°) and sin(90°).
NumPy also supports comparison operators (<, >, <=, >=, ==, !=), which allow you to compare array elements. This enables a powerful feature called masking, where elements matching a condition can be extracted. In the example below, elements greater than or equal to 4 are extracted into a new array.
Masking in NumPy
Masking with NumPy Arrays
With these arithmetic and mathematical operations, NumPy provides flexible and efficient numerical computation.
2. Broadcasting in NumPy
NumPy has a feature called broadcasting, which allows arithmetic operations between arrays of different shapes. Thanks to NumPy’s broadcasting rules, operations can be performed between smaller and larger arrays under certain conditions. In the example below, `a` is a 1×3 NumPy array and `b` is the scalar value 2. When multiplied, each element in `a` is multiplied by 2. This means you can apply operations to all elements of an array without explicitly creating another array of the same size—an efficient and powerful capability.
Broadcasting in NumPy
Broadcasting with NumPy Arrays
3. Dot Product and Matrix Multiplication with NumPy Arrays
NumPy also supports dot products and matrix multiplications, which are often used in linear algebra. The dot product represents the sum of products along a specific direction, while matrix multiplication can represent linear transformations such as rotation. We won’t go into the math in detail here, but NumPy makes these operations easy to perform.
Dot Product in NumPy
Dot Product with NumPy Arrays
Matrix Multiplication in NumPy
Matrix Multiplication with NumPy Arrays
4. Summary
In this article, we covered advanced NumPy features including arithmetic operations, mathematical functions like trigonometry, broadcasting, dot product, and matrix multiplication. Broadcasting in particular is highly useful, even outside of specialized fields, for optimizing computation. Be sure to remember this powerful tool.
▼References
Numpy’s dot() function performs “dot product” or “matrix multiplication” depending on the input - kakakakakku blog
Behavior of Broadcasting in NumPy - Qiita
How to Multiply Matrices: Examples and Intuition - Atarimae!
Masking in NumPy - Skill-up Tips