Arithmetic and linear algebra with arrays
Now that we have seen how to create and access information with NumPy arrays, let's cover some of the numerical operations you can do with arrays. In this section, we will be discussing arithmetic using NumPy arrays; we also discuss using NumPy arrays for linear algebra.
Arithmetic with two equal-shaped arrays
Arithmetic with NumPy arrays is always done component-wise. This means that, if we have two matrices that have equal shapes, an operation such as addition is done by matching corresponding components in the two matrices and adding them. This is true for any arithmetic operation, be it addition, subtraction, multiplication, division, powers, or even logical operators.
Let's see an example. First, we create two arrays of random data:


While I explain these ideas in terms of arithmetic involving two arrays, it can involve arrays and scalars as we see here, where we add 100
to every element in arr1
:

Next, we divide every element in arr1
by 2
:

Next, we...