Contenido del Curso
Ultimate NumPy
Ultimate 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.
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}')
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:
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}')
Division, subtraction, and other arithmetic operations work in a similar fashion. Here is another example where the second (right) array is broadcast:
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}')
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.
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.
¡Gracias por tus comentarios!
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.
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}')
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:
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}')
Division, subtraction, and other arithmetic operations work in a similar fashion. Here is another example where the second (right) array is broadcast:
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}')
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.
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.
¡Gracias por tus comentarios!