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

Contenido del Curso

Ultimate NumPy

Ultimate NumPy

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

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.

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

Tarea

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

Tarea

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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 4. Capítulo 2
toggle bottom row

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.

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

Tarea

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

Tarea

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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 4. Capítulo 2
toggle bottom row

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.

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

Tarea

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

Tarea

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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

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

Tarea

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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 4. Capítulo 2
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt