This is a quick demo of some of the NumPy features that will be useful in CSC411.
Importing the modules we'll be using:
from numpy import *
(Note that the usual convention is to use something like import numpy as np
and import matplotlib.pyplot as plt
, but I personally prefer from numpy import *
.)
NumPy uses the array
data structure. The array
data structure is an N-dimensional matrix of elements why are all of the same type.
a = array([5, 4, 10])
a
We can access individual elements of the array:
a[1]
a[1:3]
Here is how to make a 2D array:
a = array([[4,5,6], [1,2,3]])
a
We can access elements of the 2D array like this:
a[1, 0]
We can slices matrices like this:
a[:, 1] #Get the second column
a[:, 1:3]
We can get the shape of the array using
a.shape