NumPy is the fundamental package for scientific computing with Python. It contains among other things:
The NumPy array object is the common interface for working with typed arrays of data across a wide-variety of scientific Python packages. NumPy also features a C-API, which enables interfacing existing Fortran/C/C++ libraries with Python and NumPy.
In [ ]:
# Convention for import to get shortened namespace
import numpy as np
In [ ]:
# Create a simple array from a list of integers
a = np.array([1, 2, 3])
a
In [ ]:
# See how many dimensions the array has
a.ndim
In [ ]:
# Print out the shape attribute
a.shape
In [ ]:
# Print out the data type attribute
a.dtype
In [ ]:
# This time use a nested list of floats
a = np.array([[1., 2., 3., 4., 5.]])
a
In [ ]:
# See how many dimensions the array has
a.ndim
In [ ]:
# Print out the shape attribute
a.shape
In [ ]:
# Print out the data type attribute
a.dtype
NumPy also provides helper functions for generating arrays of data to save you typing for regularly spaced data.
arange(start, stop, interval)
creates a range of values in the interval [start,stop)
with step
spacing.linspace(start, stop, num)
creates a range of num
evenly spaced values over the range [start,stop]
.
In [ ]:
a = np.arange(5)
print(a)
In [ ]:
a = np.arange(3, 11)
print(a)
In [ ]:
a = np.arange(1, 10, 2)
print(a)
In [ ]:
b = np.linspace(5, 15, 5)
print(b)
In [ ]:
b = np.linspace(2.5, 10.25, 11)
print(b)
In [ ]:
a = range(5, 10)
b = [3 + i * 1.5/4 for i in range(5)]
In [ ]:
result = []
for x, y in zip(a, b):
result.append(x + y)
print(result)
That is very verbose and not very intuitive. Using NumPy this becomes:
In [ ]:
a = np.arange(5, 10)
b = np.linspace(3, 4.5, 5)
In [ ]:
a + b
The four major mathematical operations operate in the same way. They perform an element-by-element calculation of the two arrays. The two must be the same shape though!
In [ ]:
a * b
In [ ]:
np.pi
In [ ]:
np.e
In [ ]:
# This makes working with radians effortless!
t = np.arange(0, 2 * np.pi + np.pi / 4, np.pi / 4)
t
NumPy also has math functions that can operate on arrays. Similar to the math operations, these greatly simplify and speed up these operations. Be sure to checkout the listing of mathematical functions in the NumPy documentation.
In [ ]:
# Calculate the sine function
sin_t = np.sin(t)
print(sin_t)
In [ ]:
# Round to three decimal places
print(np.round(sin_t, 3))
In [ ]:
# Calculate the cosine function
cos_t = np.cos(t)
print(cos_t)
In [ ]:
# Convert radians to degrees
degrees = np.rad2deg(t)
print(degrees)
In [ ]:
# Integrate the sine function with the trapezoidal rule
sine_integral = np.trapz(sin_t, t)
print(np.round(sine_integral, 3))
In [ ]:
# Sum the values of the cosine
cos_sum = np.sum(cos_t)
print(cos_sum)
In [ ]:
# Calculate the cumulative sum of the cosine
cos_csum = np.cumsum(cos_t)
print(cos_csum)
In [ ]:
# Create an array for testing
a = np.arange(12).reshape(3, 4)
In [ ]:
a
Indexing in Python is 0-based, so the command below looks for the 2nd item along the first dimension (row) and the 3rd along the second dimension (column).
In [ ]:
a[1, 2]
Can also just index on one dimension
In [ ]:
a[2]
Negative indices are also allowed, which permit indexing relative to the end of the array.
In [ ]:
a[0, -1]
Slicing syntax is written as start:stop[:step]
, where all numbers are optional.
It should be noted that end represents one past the last item; one can also think of it as a half open interval: [start, end)
In [ ]:
# Get the 2nd and 3rd rows
a[1:3]
In [ ]:
# All rows and 3rd column
a[:, 2]
In [ ]:
# ... can be used to replace one or more full slices
a[..., 2]
In [ ]:
# Slice every other row
a[::2]
In [ ]:
data = [1, 3, 5, 7, 9, 11]
out = []
# Look carefully at the loop. Think carefully about the sequence of values
# that data[i] takes--is there some way to get those values as a numpy slice?
# What about for data[i + 1]?
for i in range(len(data) - 1):
out.append((data[i] + data[i + 1]) / 2)
print(out)
In [ ]:
# Your code goes here
In [ ]:
# %load solutions/slice.py
In [ ]:
# Your code goes here
In [ ]:
# %load solutions/slice_bonus.py
In [ ]:
data = np.arange(12).reshape(3, 4)
# Your code goes here
# total = ?
In [ ]:
# %load solutions/sum_row.py