## NumPy demo¶

This is a quick demo of some of the NumPy features that will be useful in CSC411.

Importing the modules we'll be using:

In :
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.

In :
a = array([5, 4, 10])

In :
a

Out:
array([ 5,  4, 10])

We can access individual elements of the array:

In :
a

Out:
4
In :
a[1:3]

Out:
array([ 4, 10])

Here is how to make a 2D array:

In :
a = array([[4,5,6], [1,2,3]])

In :
a

Out:
array([[4, 5, 6],
[1, 2, 3]])

We can access elements of the 2D array like this:

In :
a[1, 0]

Out:
1

We can slices matrices like this:

In :
a[:, 1]  #Get the second column

Out:
array([5, 2])
In :
a[:, 1:3]

Out:
array([[5, 6],
[2, 3]])

We can get the shape of the array using

In :
a.shape

Out:
(2, 3)