import numpy as np
# Create a 1-dimensional array
arr = np.array([1, 2, 3, 4, 5])
arrarray([1, 2, 3, 4, 5])
NumPy ArraysWrite a description of the lesson here.
Noor Sohail
Will Gammerdinger
March 15, 2026
keyword_1, keyword_2, keyword_3, keyword_4, keyword_5, keyword_6
Approximate time: XX minutes
In this lesson, we will:
When doing XYZ…
NumPy LibraryNumPy 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.
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:
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
resultarray([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.
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:
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_matrixarray([[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
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.
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
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]])
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]:
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]:
So if we wanted to access the element in the second row and third column of the matrix, we would use matrix[1, 2]:
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_arrarray([4, 5])
NumPy FunctionsThe 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
---
title: "`NumPy` Arrays"
description: |
Write a description of the lesson here.
author:
- Noor Sohail
- Will Gammerdinger
date: "2026-03-15"
categories:
- category_1
- category_2
- category_3
- category_4
keywords:
- keyword_1
- keyword_2
- keyword_3
- keyword_4
- keyword_5
- keyword_6
license: "CC-BY-4.0"
editor_options:
markdown:
wrap: 72
---
```{r}
#| label: load_libraries_data
#| echo: false
# Load libraries and data
# Interfacing with R quarto and python futzing
library(reticulate)
use_condaenv("/opt/anaconda3/envs/intro_python", required = TRUE)
```
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:
```{python}
#| label: create_1d_array
import numpy as np
# Create a 1-dimensional array
arr = np.array([1, 2, 3, 4, 5])
arr
```
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:
```{python}
#| label: elementwise_operations
# Create another 1-dimensional array
arr2 = np.array([10, 20, 30, 40, 50])
# Perform element-wise addition
result = arr + arr2
result
```
If we were to try to do this with Python lists, we would get a different result:
```{python}
#| label: list_addition
# 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
```
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.
```{python}
#| label: check_dtype
# Check the data type of the NumPy array
arr.dtype
```
### 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:
```{python}
#| label: create_2d_array
# Create a 2-dimensional array (matrix)
matrix = np.array([[1, 2, 3], [4, 5, 6]])
matrix
```
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).
```{python}
#| label: matrix_multiplication
# 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
```
We can even initialize a matrix of zeros or ones using `numpy.zeros()` and `numpy.ones()` functions in as many dimensions as we want
::: callout-note
# 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.
:::
```{python}
#| label: initialize_matrices
# Initialize a 3x3 matrix of zeros
zeros_matrix = np.zeros((3, 3))
zeros_matrix
# Initialize a 2x4 matrix of ones
ones_matrix = np.ones((2, 4))
ones_matrix
```
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.
```{python}
#| label: arange_function
#| echo: false
# Create an array of integers from 0 to 9
matrix_arange = np.arange(10).reshape((2, 5))
matrix_arange
```
### 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]`:
```{python}
#| label: indexing_1d_array
# Access the first element
first_element = arr[0]
first_element
```
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]`:
```{python}
#| label: indexing_2d_array
# Access the first row of a 2-dimensional array
first_row = matrix[0]
first_row
```
So if we wanted to access the element in the second row and third column of the matrix, we would use `matrix[1, 2]`:
```{python}
#| label: indexing_2d_array_element
# Access the element in the second row and third column
element = matrix[1, 2]
element
```
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
```{python}
#| label: boolean_indexing
# 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
```
:::{.callout-tip}
# [**Exercise 1**](08_lesson-Answer_key.qmd#exercise-1)
1. A question to evaluate Learning Objective 1
2. A followup question to question #1
3. ...
:::
## Useful `NumPy` Functions
The list of functions available in base Python is somewhat limited when it comes to numerical operations. The [`NumPy` library](https://numpy.org/doc/stable/reference/) 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:
```{python}
#| label: numpy_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}")
```
### Subsection 2A
### Subsection 2B
:::{.callout-tip}
# [**Exercise 2**](08_lesson-Answer_key.qmd#exercise-2)
1. A question to evaluate Learning Objective 2
2. A followup question to question #1
3. ...
:::
## Topic 3
### Subsection 3A
### Subsection 3B
:::{.callout-tip}
# [**Exercise 3**](08_lesson-Answer_key.qmd#exercise-3)
1. A question to evaluate Learning Objective 3
2. A followup question to question #1
3. ...
:::
***
[Next Lesson >>](09_pandas_dataframes.qmd)
[Back to Schedule](../schedule/schedule.qmd)