NumPy

Array Operations

Element-wise operations

This is an array operations tutorial.

Element-wise operations in NumPy refer to operations that are performed independently on each element of an array. NumPy provides a convenient way to perform these operations efficiently. Let’s explore some of the ways of dealing with them.

We cover addition, subtraction, multiplication, division, exponentiation, square root, comparison, and logical operations.

Below is an example of adding two arrays.

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

# addition
result = arr1 + arr2
print(result)
# Output: [5 7 9]

The following are the rest of the functions.

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

# subtraction
result = arr1 - arr2
# multiplication
result = arr1 * arr2
# division
result = arr2 / arr1
# exponentiation
result = arr1 ** 2
# square root
result = np.sqrt(arr1)
# comparison
result = arr1 == arr2
# logical operation AND
result_and = np.logical_and(arr1, arr2)
# logical operation OR
result_or = np.logical_or(arr1, arr2)

Broadcasting

NumPy broadcasting is a powerful mechanism that allows you to perform arithmetic operations on arrays of different shapes, making certain assumptions about how to align the arrays’ dimensions. This makes it easier to write vectorised code without explicitly handling array shapes.

In broadcasting, NumPy compares the shapes of the input arrays element-wise, starting from the trailing dimensions and working its way forward. Two dimensions are considered compatible when they are equal or one of them is 1. When either of the dimensions is 1, NumPy will stretch this dimension to match the other.

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

to_add = np.array([10, 20, 30])

# broadcasting
result = arr + to_add
print(result)

Below is the outcome.

[[11 22 33]
 [14 25 36]]

Mathematical functions


NumPy provides a wide range of mathematical functions that operate element-wise on arrays. There are many mathematical functions within NumPy, which belong into a variety of categories. We cover a portion of all functions.

We explore the following:

  1. Summation: the sum of array elements over a given axis.
  2. Mean: the arithmetic mean along the specified axis.
  3. Maximum: the maximum of array elements along the specified axis.
  4. Minimum: the minimum of array elements along the specified axis.
  5. Maximum index: the indices of the maximum values along an axis.
  6. Minimum index: the indices of the minimum values along an axis.
  7. Standard Deviation: the standard deviation along the specified axis.
  8. Product: the product of array elements over a given axis.

Let’s look at the coding.

import numpy as np

arr = np.array([[1, 2], [3, 4]])

total_sum = np.sum(arr)
print(total_sum)
# Output: 10

mean_value = np.mean(arr)
print(mean_value)
# Output: 2.5

max_value = np.max(arr)
print(max_value)
# Output: 4

min_value = np.min(arr)
print(min_value)
# Output: 1

max_index = np.argmax(arr)
print(max_index)
# Output: 3

min_index = np.argmin(arr)
print(min_index)
# Output: 0

std_dev = np.std(arr)
print(std_dev)
# Output: 1.118033988749895

product = np.prod(arr)
print(product)
# Output: 24

This is an original array operations educational material created by aicorr.com.

Next: Random Data

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.