Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basic Mathematical Operations | Math with NumPy
Ultimate NumPy
course content

Course Content

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

book
Basic Mathematical Operations

Now that you're familiar with the concept of broadcasting, let’s discuss some basic mathematical operations in NumPy.

Scalar Operations

Remember, broadcasting allows you to perform mathematical operations between two arrays of compatible shapes or between an array and a scalar.

1234567891011
import numpy as np array = np.array([1, 2, 3, 4]) # Scalar addition result_add_scalar = array + 2 print(f'Scalar addition: {result_add_scalar}') # Scalar multiplication result_mul_scalar = array * 3 print(f'Scalar multiplication: {result_mul_scalar}') # Raising an array to a scalar power result_power_scalar = array ** 3 print(f'Scalar exponentiation: {result_power_scalar}')
copy

As you can see, each operation is performed element-wise on the array. Essentially, a scalar is broadcast to an array of the same shape as our original array, where all the elements are the same number. Therefore, the operation is performed on every pair of corresponding elements of the two arrays.

Operations Between Two Arrays

If the shapes of two arrays are compatible, broadcasting is performed if needed, and once again, an operation is performed element-wise:

123456789101112
import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([5, 6, 7, 8]) # Element-wise addition result_add = arr1 + arr2 print(f'Element-wise addition: {result_add}') # Element-wise multiplication result_mul = arr1 * arr2 print(f'Element-wise multiplication: {result_mul}') # Element-wise exponentiation (raising to power) result_power = arr1 ** arr2 print(f'Element-wise exponentiation: {result_power}')
copy

Division, subtraction, and other arithmetic operations work in a similar fashion. Here is another example where the second (right) array is broadcast:

123456789101112
import numpy as np arr1 = np.array([[1, 2, 3], [4, 5, 6]]) arr2 = np.array([5, 6, 7]) # Element-wise addition result_add = arr1 + arr2 print(f'Element-wise addition: {result_add}') # Element-wise multiplication result_mul = arr1 * arr2 print(f'Element-wise multiplication: {result_mul}') # Element-wise exponentiation (raising to power) result_power = arr1 ** arr2 print(f'Element-wise exponentiation:\n{result_power}')
copy

arr_2 is broadcast to a 2D array with two identical rows, each containing the array [5, 6, 7].

Applications

Such mathematical operations are essential for tasks like scaling, normalizing, and transforming data in machine learning and statistical analysis. They enable efficient element-wise operations for combining datasets, performing numerical simulations, and applying filters in image and signal processing. Besides, these operations are widely used in scientific computing and data-driven applications.

Task
test

Swipe to show code editor

You are analyzing the quarterly sales data for two products in 2021 and 2022, stored in two 2D arrays:

  • sales_data_2021: quarterly sales for each product in 2021, with each row representing a specific product;
  • sales_data_2022: quarterly sales for each product in 2022, with each row representing a specific product.

Calculate the quarterly revenue growth for each product in percent.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
toggle bottom row

book
Basic Mathematical Operations

Now that you're familiar with the concept of broadcasting, let’s discuss some basic mathematical operations in NumPy.

Scalar Operations

Remember, broadcasting allows you to perform mathematical operations between two arrays of compatible shapes or between an array and a scalar.

1234567891011
import numpy as np array = np.array([1, 2, 3, 4]) # Scalar addition result_add_scalar = array + 2 print(f'Scalar addition: {result_add_scalar}') # Scalar multiplication result_mul_scalar = array * 3 print(f'Scalar multiplication: {result_mul_scalar}') # Raising an array to a scalar power result_power_scalar = array ** 3 print(f'Scalar exponentiation: {result_power_scalar}')
copy

As you can see, each operation is performed element-wise on the array. Essentially, a scalar is broadcast to an array of the same shape as our original array, where all the elements are the same number. Therefore, the operation is performed on every pair of corresponding elements of the two arrays.

Operations Between Two Arrays

If the shapes of two arrays are compatible, broadcasting is performed if needed, and once again, an operation is performed element-wise:

123456789101112
import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([5, 6, 7, 8]) # Element-wise addition result_add = arr1 + arr2 print(f'Element-wise addition: {result_add}') # Element-wise multiplication result_mul = arr1 * arr2 print(f'Element-wise multiplication: {result_mul}') # Element-wise exponentiation (raising to power) result_power = arr1 ** arr2 print(f'Element-wise exponentiation: {result_power}')
copy

Division, subtraction, and other arithmetic operations work in a similar fashion. Here is another example where the second (right) array is broadcast:

123456789101112
import numpy as np arr1 = np.array([[1, 2, 3], [4, 5, 6]]) arr2 = np.array([5, 6, 7]) # Element-wise addition result_add = arr1 + arr2 print(f'Element-wise addition: {result_add}') # Element-wise multiplication result_mul = arr1 * arr2 print(f'Element-wise multiplication: {result_mul}') # Element-wise exponentiation (raising to power) result_power = arr1 ** arr2 print(f'Element-wise exponentiation:\n{result_power}')
copy

arr_2 is broadcast to a 2D array with two identical rows, each containing the array [5, 6, 7].

Applications

Such mathematical operations are essential for tasks like scaling, normalizing, and transforming data in machine learning and statistical analysis. They enable efficient element-wise operations for combining datasets, performing numerical simulations, and applying filters in image and signal processing. Besides, these operations are widely used in scientific computing and data-driven applications.

Task
test

Swipe to show code editor

You are analyzing the quarterly sales data for two products in 2021 and 2022, stored in two 2D arrays:

  • sales_data_2021: quarterly sales for each product in 2021, with each row representing a specific product;
  • sales_data_2022: quarterly sales for each product in 2022, with each row representing a specific product.

Calculate the quarterly revenue growth for each product in percent.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt