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

Зміст курсу

Ultimate NumPy

Ultimate NumPy

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

bookBasic 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.

Let’s first look at an example with scalars:

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 an example where the second 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].

Завдання
test

Swipe to show code editor

Calculate the quarterly revenue growth for each product in percent (each row of a 2D array contains quarterly sales for a certain product).

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2
toggle bottom row

bookBasic 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.

Let’s first look at an example with scalars:

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 an example where the second 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].

Завдання
test

Swipe to show code editor

Calculate the quarterly revenue growth for each product in percent (each row of a 2D array contains quarterly sales for a certain product).

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2
toggle bottom row

bookBasic 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.

Let’s first look at an example with scalars:

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 an example where the second 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].

Завдання
test

Swipe to show code editor

Calculate the quarterly revenue growth for each product in percent (each row of a 2D array contains quarterly sales for a certain product).

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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.

Let’s first look at an example with scalars:

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 an example where the second 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].

Завдання
test

Swipe to show code editor

Calculate the quarterly revenue growth for each product in percent (each row of a 2D array contains quarterly sales for a certain product).

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 4. Розділ 2
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt