📓 About This Tutorial

This webpage is a static view of an interactive Jupyter Notebook tutorial. To get the full interactive experience where you can run Python code, modify examples and complete exercises, click the "Open in Colab" button. This will access the Google Colab notebook from a GitHub repository github.com/dchappell2/Computational-Physics and open it in Google Colab. You will need a Google account if you want to open it in Colab.

Note: You can download a pdf of the lecture slides for this chapter here: Chapter 8 slides

Chapter 8 - Multidimensional NumPy Arrays (2D & 3D)

Goals

8.0 Multidimensional Arrays

This chapter describes how to create, apply and extract data from multi-dimensional arrays in 2D, 3D and higher dimensions.

8.1 Creating multidimensional arrays

8.1.1 Creating 2D arrays

🔆 Example: Creating 2D array filled with zeros

import numpy as np

A = np.zeros((2,3))
print(A)

🔆 Example: Creating an array of random integers

In the following example, we fill a 2x5 array with random integers between 1 and 9 inclusive.

rng = np.random.default_rng()        # create random number Generator

A = rng.integers(1, 9, size=(3,5),endpoint=True)
print(A)

✅ Skill Check 1

Create a 2D array with 6 columns and 3 rows. Each element should be a random integer whose value satisfies $1 \leq x \leq 4$. Print the array.

# Your code here

8.1.2 Creating 3D arrays

A = np.zeros((4,2,3))
print(A)

8.2 .shape, .size and .ndim attributes

NumPy provides three attributes that characterize the shape and size of NumPy arrays. Remember that attributes do no use parentheses, while methods do.


A = rng.integers(1, 9, size=(3,5),endpoint=True)
print("A = ")
print(A)

print()
print("shape of A is ",A.shape)

n_rows = A.shape[0]              # extract the number of rows
n_cols = A.shape[1]              # extract the number of columns

print("number of rows in A    =",n_rows)
print("number of columns in A =",n_cols)
print("size of A (total # elements) =",A.size)
print("number of dimensions of A    =",A.ndim)

🔆 Example: 4D Array

Here's an example of creating a 4D array:

z = np.zeros((2,3,4,5))

print("shape of z (length along each axis) =",z.shape)
print("size of z (total # elements) =",z.size)
print("number of dimensions of A    =",z.ndim)
print()
print("z = ")
print(z)

8.4 Accessing elements in multi-dimensional arrays

The following examples show how to access and slice a 2D array.

rng = np.random.default_rng()    # create random number Generator
A = rng.integers(1, 9, size=(3,5),endpoint=True)

print("A = ")
print(A)                         # prints array A
print()                          # prints a blank line

print("A[0,2]  =  ",A[0,2])      # top row, 3rd column
print("A[0,:2] =  ",A[0,1:3])    # top row, columns 2 and 3
print("A[0,:]  =  ",A[0,:])      # top row
print("A[0,:]  =  ",A[:,1])      # second column
print()
print("A[0:2,0:2] = ")           # 2x2 matrix from top-left corner of A
print(A[0:2,0:2])

The following over-writes the top row of our 2D array with zeros.

A[0,:] = np.zeros(A.shape[1])
print(A)

✅ Skill Check 2

# your code here

8.5 Working with multi-dimensional arrays

8.5.1 Create a new array to match size of an existing array

A = rng.integers(1, 9, size=(3,5),endpoint=True)

B = np.zeros(A.shape)     # Array B will have same size as array A
print(B)

8.5.2 Take the transpose of a 2D array

print("original Array, A:")
print(A)
print()

print("transpose of A:")
print(A.T)

8.5.3 Reshaping arrays

Flatten

print("flattened version of A")
A_flat = A.flatten()
print(A_flat)

Reshape

#####  Create a 1x12 array filled with integers
print("original array. A ")
A = np.linspace(1,12,12,dtype=int)
print(A)

#####  Reshape -> 2x6  (2 rows, 6 columns)
print()
print("A2 = A.reshape((2,6)) ")
A2 = A.reshape((2,6))
print(A2)
print()

#####  Reshape -> 3x4  (3 rows, 4 columns)
print("A3 = A2.reshape((3,4)) ")
A3 = A2.reshape((3,4))
print(A3)
print()

#####  Reshape -> 2x2x3  (2 layers, 2 rows, 3 columns) = 3D array
print("A4 = A3.reshape((2,2,3)) ")
A4 = A3.reshape((2,2,3))
print(A4)

8.5.4 Analyzing Arrays along Rows or Columns

8.5.4 Computing statistics along rows or columns

R = np.linspace(1,15,15)
R = R.reshape((5,3))
R = R.T
print("R = ")
print(R)

ave = R.mean(axis = 0)
print()
print("Column mean = ")
print(ave)

ave = R.mean(axis = 1)
print()
print("Row mean = ")
print(ave)

8.6 Reading data files in Google Colab

Google Colab poses some challenges when reading and writing files due to the way Google handles directories. When we run Python on your laptop, this process will be much simpler.

Here's what the following Code Cell does to read in the data file:

Use of the np.loadtxt() command

```

1.0 2.4 3.6

3.5 0.1 0.4

4.3 2.1 3.5

```

```

1.0, 2.4, 3.6

3.5, 0.1, 0.4

4.3, 2.1, 3.5

```

```

x, y, z

1.0, 2.4, 3.6

3.5, 0.1, 0.4

4.3, 2.1, 3.5

```

# Import Libraries
from google.colab import files   # used to load files in Google
import numpy as np

#####  Load Data File
uploaded = files.upload()  # This opens a file picker

# uploaded is a dictionary: filename -> bytes
# Get the first filename
file_name = list(uploaded.keys())[0]

# Load the data into a NumPy array
# Assuming a text file with numeric data, separated by commas
#    and with one header line that must be skipped
data = np.loadtxt(file_name,skiprows=1,delimiter=',')

The bh_merger.csv file contains the following columns of data:

The following Code prints the number of rows, columns and first 5 rows of data.

# Inspect the array
nrows = data.shape[0]
ncols = data.shape[1]
print()
print(f"{file_name} contains {nrows} rows and {ncols} columns")
print()
print("First 5 rows:")
print(data[:5])

✅ Skill Check 3

The following code shows how to extract the first column of values from the data array and name it mass1 (this will contain the masses of one of black holes in the binary system.)

mass1 = data[:,0]  # mass one is stored in the first column (index = 0)

# your code here

✅ Skill Check 4

# your code here

✅ Skill Check 5

# your code here

Key Points

This tutorial is an adaptation of "Python for Physicists"

© Software Carpentry