NumPy Arrays

category_1
category_2
category_3
category_4

Write a description of the lesson here.

Authors

Noor Sohail

Will Gammerdinger

Published

March 15, 2026

Keywords

keyword_1, keyword_2, keyword_3, keyword_4, keyword_5, keyword_6

Approximate time: XX minutes

Learning Objectives

In this lesson, we will:

  • Learning Objective 1
  • Learning Objective 2
  • Learning Objective 3

Overview of lesson

When doing XYZ…

NumPy Library

NumPy is a powerful library for numerical computing in Python. It is widely used for working with arrays and matrices. The implementation of these data structures has been optimized for performance, making it a popular choice for scientific computing and data analysis.

1-dimensional Arrays

The most basic data structure in NumPy is the 1-dimensional array, which is similar to a list. You can create a 1-dimensional array using the numpy.array() function:

import numpy as np

# Create a 1-dimensional array
arr = np.array([1, 2, 3, 4, 5])
arr
array([1, 2, 3, 4, 5])

One of the key advantages of NumPy arrays over Python lists is that they are more efficient for numerical operations. For example, you can perform element-wise operations on NumPy arrays. So if we were to add two arrays together, it would add each element of the first array to the corresponding element of the second array:

# Create another 1-dimensional array
arr2 = np.array([10, 20, 30, 40, 50])
# Perform element-wise addition
result = arr + arr2
result
array([11, 22, 33, 44, 55])

If we were to try to do this with Python lists, we would get a different result:

# Create two Python lists
list1 = [1, 2, 3, 4, 5]
list2 = [10, 20, 30, 40, 50]

# Attempt to perform element-wise addition (this will not work as expected)
result_list = list1 + list2
result_list
[1, 2, 3, 4, 5, 10, 20, 30, 40, 50]

Instead of adding two lists together element-wise, Python concatenates them.

Additionally, if we were to investigate the dtype() of the NumPy array, we would find that it is a specific data type (e.g., int64 or float64). So, unlike lists which can contain different data types, the elements in NumPy arrays are all the same data type.

# Check the data type of the NumPy array
arr.dtype
dtype('int64')

N-dimensional Arrays

These arrays are not just limited to 1 dimension; NumPy can handle multi-dimensional arrays as well. For example, you can create a 2-dimensional array (also known as a matrix) using the same numpy.array() function:

# Create a 2-dimensional array (matrix)
matrix = np.array([[1, 2, 3], [4, 5, 6]])
matrix
array([[1, 2, 3],
       [4, 5, 6]])

This again may seem familiar to a list of lists in Python, but it is important to note that this is a NumPy array, which has different properties and capabilities than a list of lists. For example, you can perform matrix operations on this 2-dimensional array, such as matrix multiplication (dot product).

# Create another 2-dimensional array (matrix)
matrix2 = np.array([[10, 20], [30, 40], [50, 60]])

# Perform matrix multiplication
result_matrix = np.dot(matrix, matrix2)
result_matrix
array([[220, 280],
       [490, 640]])

We can even initialize a matrix of zeros or ones using numpy.zeros() and numpy.ones() functions in as many dimensions as we want

Rows then columns

When accessing elements in a 2-dimensional array, the first index refers to the row and the second index refers to the column. So matrix[0, 1] would access the element in the first row and second column of the matrix.

Similarly, when initializing a matrix, the first number refers to the number of rows and the second number refers to the number of columns.

# Initialize a 3x3 matrix of zeros
zeros_matrix = np.zeros((3, 3))
zeros_matrix 
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
# Initialize a 2x4 matrix of ones
ones_matrix = np.ones((2, 4))
ones_matrix
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.]])

The numpy.reshape() function is another flexible way to create arrays of varying shapes and sizes. We can use this in conjunction with numpy.arange() to create an array of a specific range of numbers and then reshape it into the desired dimensions.

array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

Indexing and Slicing Arrays

Arrays can be indexed and sliced in a similar way to lists with indices (starting at 0) and slicing syntax. So if we wanted to access the first element of a 1-dimensional array, we would use arr1[0]:

# Access the first element
first_element = arr[0]
first_element
np.int64(1)

In a 2-dimensional array, the first index refers to the row and the second index refers to the column. So if we wanted to access the first row of a 2-dimensional array, we would use matrix[0]:

# Access the first row of a 2-dimensional array
first_row = matrix[0]
first_row
array([1, 2, 3])

So if we wanted to access the element in the second row and third column of the matrix, we would use matrix[1, 2]:

# Access the element in the second row and third column
element = matrix[1, 2]
element
np.int64(6)

So far, this is quite similar to lists. Arrays differ in that they have more advanced indexing and slicing capabilities, allowing you to select specific elements or subsets of the array based on certain conditions. This is particularly useful when working with large datasets, as it allows you to efficiently filter and manipulate the data

# Create a 1-dimensional array
arr = np.array([1, 2, 3, 4, 5])
# Select elements greater than 3
filtered_arr = arr[arr > 3]
filtered_arr
array([4, 5])
  1. A question to evaluate Learning Objective 1
  2. A followup question to question #1

Useful NumPy Functions

The list of functions available in base Python is somewhat limited when it comes to numerical operations. The NumPy library provides a broader range of tools to perform mathematical operations. For example, you can calculate the mean, median, and standard deviation of an array using numpy.mean(), numpy.median(), and numpy.std() functions:

# Create a 1-dimensional array
arr = np.array([2, 7, 9.5, 5, 7, 3.2])

# Calculate mean, median, and standard deviation
mean = np.mean(arr)
median = np.median(arr)
std_dev = np.std(arr)

print(f"Mean: {mean}, Median: {median}, Standard Deviation: {std_dev}")
Mean: 5.616666666666667, Median: 6.0, Standard Deviation: 2.523500654954453

Subsection 2A

Subsection 2B

  1. A question to evaluate Learning Objective 2
  2. A followup question to question #1

Topic 3

Subsection 3A

Subsection 3B

  1. A question to evaluate Learning Objective 3
  2. A followup question to question #1

Next Lesson >>

Back to Schedule

Reuse

CC-BY-4.0