In [ ]:
# imports
import numpy as np # It will be used a lot, so the shorthand is helpful.
import matplotlib.pyplot as plt # Same here.
%matplotlib inline
# these can be useful if you plan on using the respective functions a lot:
np.random.seed(42) # Seeding is important to replicate results when using random numbers.
rnd = np.random.random
sin = np.sin # Be careful to no write "sin = np.sin()"! Why?
cos = np.cos
RAD2DEG = 180.0/np.pi # Constants for quick conversion between radians (used by sin/cos) and degree
DEG2RAD = np.pi/180.0
Every numpy array has some basic values that denote its format. Note that numpy array cannot change their size once they are created, but they can change their shape, i.e., an array will always hold the same number of elements, but their organization into rows and columns may change as desired.
(see: Numpy basics)
In [ ]:
m = np.array([[1,2,3],
[4,5,6],
[7,8,9]], dtype=np.int32) # np.float32, np.float64, np.complex64, np.complex128
print m
print 'ndim: ', m.ndim, '\nshape:', m.shape, '\nsize: ', m.size, '\ndtype:', m.dtype
In [ ]:
s = m[1]
print 'BEFORE'
print s, 'slice', '\n'
print m, '\n'
s[0] = 0
print 'AFTER'
print s, 'slice' '\n'
print m, '\n'
In [ ]:
print m.flags, '\n'
print s.flags
In [ ]:
# helper function for examples below; plots the graphical depiction of a given numpy array
def showMatrix(X):
Y = np.array(np.array(X, ndmin=2)) # 1D -> 2D
vmin = min(np.min(Y), 0)
vmax = max(np.max(Y), 1)
plt.imshow(Y, interpolation='none', vmin=vmin, vmax=vmax, cmap=plt.cm.get_cmap('Blues'))
In [ ]:
Z = np.zeros(9)
showMatrix(Z)
In [ ]:
Z = np.zeros((5,9))
showMatrix(Z)
In [ ]:
Z = np.ones(9)
showMatrix(Z)
In [ ]:
Z = np.ones((5,9))
showMatrix(Z)
In [ ]:
Z = np.array( [0,0,0,0,0,0,0,0,0] )
showMatrix(Z)
In [ ]:
Z = np.array( [[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0]] )
showMatrix(Z)
In [ ]:
Z = np.arange(9) # the numpy arange function also allows floating point arguments
showMatrix(Z)
(see also: linspace)
In [ ]:
Z = np.arange(5*9).reshape(5,9)
showMatrix(Z)
In [ ]:
Z = np.random.uniform(0,1,9) # args: min, max, no. of elements
showMatrix(Z)
In [ ]:
Z = np.random.uniform(0, 1, (5, 9))
showMatrix(Z)
In [ ]:
# single element
Z = np.zeros((5, 9))
Z[1,1] = 1
showMatrix(Z)
In [ ]:
# single row
Z = np.zeros((5, 9))
Z[1,:] = 1
showMatrix(Z)
In [ ]:
# single column
Z = np.zeros((5, 9))
Z[:,1] = 1
showMatrix(Z)
In [ ]:
# specific area
Z = np.zeros((5, 9))
Z[2:4,2:6] = 1 # for each dimension format is always: <from:to:step> (with step being optional)
showMatrix(Z)
In [ ]:
# every second column
Z = np.zeros((5, 9))
Z[:,::2] = 1 # for each dimension format is always: <from:to:step> (with step being optional)
showMatrix(Z)
In [ ]:
# indices can be negative
Z = np.arange(10)
print ">>> Z[-1]: ", Z[-1] # start indexing at the back
print ">>> Z[3:-3]:", Z[3:-3] # slice of array center
print ">>> Z[::-1]:", Z[::-1] # quickly reverse an array
(see: Numpy array slicing)
Arithmetic operations applied to two Numpy arrays of different dimensions leads to 'broadcasting', i.e., filling up the missing values to allow the operation if possible. This includes:
NOTE: Multiplying with * WILL ALSO BE APPLIED elementwise! Use np.dot() for actual matrix multiplication!
FUN FACT: Truth value checks will also applied elementwise.
(see: Numpy broadcasting)
In [ ]:
#-#-# EXC_NUMPY: YOUR CODE HERE #-#-#