Зміст курсу
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.
Let’s first look at an example with scalars:
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 an example where the second 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]
.
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).
Дякуємо за ваш відгук!
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:
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 an example where the second 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]
.
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).
Дякуємо за ваш відгук!
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:
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 an example where the second 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]
.
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).
Дякуємо за ваш відгук!
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:
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 an example where the second 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]
.
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).