NumPy Array Operations

NumPyNumPyBeginner
Practice Now

Introduction

NumPy is a Python library used for numerical computing. It is designed to work with arrays and matrices, making it a powerful tool for scientific computing. In this lab, you will learn the following three topics related to NumPy Array Operations:

  1. Mathematical Operations
  2. Broadcasting
  3. Universal Functions

Mathematical Operations

NumPy provides a variety of mathematical operations for arrays. These operations can be performed on one or more arrays.

Open the Python Shell

Open the Python shell by typing the following command in the terminal.

python3

Import NumPy

NumPy is already installed, you can import it in your Python code:

import numpy as np

Element-wise Operations

Element-wise operations are operations performed on each element in the array.

Let's create two arrays and perform some element-wise operations:

## Creating two arrays
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])

## Adding two arrays
print("Adding two arrays: ", arr1 + arr2)

## Subtracting two arrays
print("Subtracting two arrays: ", arr1 - arr2)

## Multiplying two arrays
print("Multiplying two arrays: ", arr1 * arr2)

## Dividing two arrays
print("Dividing two arrays: ", arr1 / arr2)

## Finding the remainder after division of two arrays
print("Modulo of two arrays: ", arr1 % arr2)

## Raising elements of an array to a power
print("Raising an array to a power: ", arr1 ** 2)

Output:

Adding two arrays:  [ 6  8 10 12]
Subtracting two arrays:  [-4 -4 -4 -4]
Multiplying two arrays:  [ 5 12 21 32]
Dividing two arrays:  [0.2        0.33333333 0.42857143 0.5       ]
Modulo of two arrays:  [1 2 3 4]
Raising an array to a power:  [ 1  4  9 16]

Array-wise Operations

Array-wise operations are operations performed on the entire array.

Let's create an array and perform some array-wise operations:

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

## Finding the sum of all elements in the array
print("Sum of array: ", np.sum(arr))

## Finding the product of all elements in the array
print("Product of array: ", np.prod(arr))

## Finding the minimum element in the array
print("Minimum element in array: ", np.min(arr))

## Finding the maximum element in the array
print("Maximum element in array: ", np.max(arr))

## Finding the average of all elements in the array
print("Average of array: ", np.mean(arr))

## Finding the standard deviation of all elements in the array
print("Standard deviation of array: ", np.std(arr))

Output:

Sum of array:  10
Product of array:  24
Minimum element in array:  1
Maximum element in array:  4
Average of array:  2.5
Standard deviation of array:  1.118033988749895

Broadcasting

Broadcasting is a feature of NumPy that allows for element-wise operations between arrays with different shapes. Broadcasting is especially useful when working with arrays of different dimensions.

Let's create an array and perform some broadcasting operations:

## Creating two arrays of different shapes
array1 = np.array([1, 2, 3])
array2 = np.array([[4, 5, 6], [7, 8, 9]])

## Broadcasting the smaller array to the larger array
print("Adding two arrays using broadcasting: ", array1 + array2)

print("Subtracting two arrays using broadcasting: ", array1 - array2)

print("Multiplying two arrays using broadcasting: ", array1 * array2)

print("Dividing two arrays using broadcasting: ", array1 / array2)

Output:

Adding two arrays using broadcasting:  [[ 5  7  9]
                                         [ 8 10 12]]

Subtracting two arrays using broadcasting:  [[-3 -3 -3]
                                              [-6 -6 -6]]

Multiplying two arrays using broadcasting:  [[ 4 10 18]
                                              [7 16 27]]

Dividing two arrays using broadcasting:  [[0.25       0.4        0.5       ]
                                           [0.14285714 0.25       0.33333333]]

In the above code, we create two arrays, array1 with the shape (3,) and array2 with the shape (2,3). We perform element-wise operations between array1 and array2, thanks to the broadcasting feature in NumPy. The smaller array, array1, is broadcasted to the larger array, array2, to perform element-wise operations. Broadcasting makes it possible to perform operations on arrays with different shapes.

Universal Functions

Universal functions, or ufuncs, are functions that operate on arrays in an element-wise fashion. They provide fast and efficient operations on arrays.

Let's create an array and perform some ufuncs:

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

## Finding the square root of each element in the array
print("Square root of array: ", np.sqrt(arr))

## Finding the exponential of each element in the array
print("Exponential of array: ", np.exp(arr))

## Finding the sine of each element in the array
print("Sine of array: ", np.sin(arr))

## Finding the cosine of each element in the array
print("Cosine of array: ", np.cos(arr))

## Finding the natural logarithm of each element in the array
print("Natural logarithm of array: ", np.log(arr))

Output:

Square root of array:  [1.         1.41421356 1.73205081 2.        ]
Exponential of array:  [ 2.71828183  7.3890561  20.08553692 54.59815003]
Sine of array:  [ 0.84147098  0.90929743  0.14112001 -0.7568025 ]
Cosine of array:  [ 0.54030231 -0.41614684 -0.9899925  -0.65364362]
Natural logarithm of array:  [0.         0.69314718 1.09861229 1.38629436]

Summary

Congratulations! You have now learned about NumPy Array Operations including Mathematical Operations, Broadcasting, and Universal Functions. With this knowledge, you can now perform a wide range of numerical computing tasks with Python.

Other NumPy Tutorials you may like