#### You've reached the end of your free preview.

Want to read all 19 pages?

**Unformatted text preview: **Recitation 13
NumPy Announcements
●
● Homework 09 is due Monday 11/18 - start SOON!
Homework 09 Prompt and Skeleton File have been updated!
○
○ Changed values for test case for relation function
Added parameter img_source to get_imgs function
■ You do not have to download the new solution ﬁle What is NumPy?
NumPy is the fundamental package for scientiﬁc computing in Python
● A Python library that’s based on n-dimensional array objects (think linear
algebra) that hold objects of the same data type You should already have NumPy installed through anaconda. To use it, import
using the following convention:
import numpy as np
*Note: use np for consistency in hws/exams NumPy Arrays
Can be thought of as the lists of NumPy
●
●
● Can be indexed
Contain elements of the same data type
Comparable to matrices Create an array using np.array() which will take
in a list OR a list of lists where each inner list
represents a row
Ex: np.array([[1,2,3], [4,5,6]]) Array Creation
np.array([alist], dtype = “your data type”)
Turns a python list into an array of a
homogeneous data type
dtype is not a required parameter
np.zeros( n)
Creates an array of n zeros, defaults to ﬂoats
np.ones(( m,n) , dtype = x)
An m x n matrix of ones with data type x
( m = # of rows, n = # of columns) np.full(( m,n), aValue)
Creates an m x n matrix of aValue Array Creation
np.arange(start, end, step)
Works like the range function, returns
array with the values from start to end
(end is EXCLUSIVE)
np.linspace(start, end, n)
Returns an array from start to end that
is n elements long, INCLUSIVE for both
start and end More Array Creation
np.random.random(( m,n))
Creates an m x n matrix of random Uniform
distribution [0,1)
np.random.normal( μ, σ², ( m,n))
Creates an m x n matrix of Normal(μ, σ²) numbers
np.random.randint(start, end, ( m,n))
Creates an m x n array of random integers in
range [start, end)
np.identity( n)
Creates an n x n identity matrix, defaults to ﬂoats Array Attributes
arrayName.ndim
Returns the number of dimensions
m x n arrays will always have ndim = 2
arrayName.shape
Returns tuple with number of elements
in each dimension
m x n arrays will return ( m rows, n columns)
arrayName.dtype
Returns type of elements in array Reshaping Arrays
Use reshaping to change the dimensions of
an array
arrayName.reshape(rowNum, colNum)
rowNum is the number of rows in the reshaped array,
and colNum is the number of columns in the reshaped
array
You must maintain the same number of elements to
reshape an array!
Reshaping will not change the original array Vectorized Operations
Replace explicit loops with array
expressions
Occur between arrays of compatible
shapes or between an array and scalar
One array is “broadcast” across the
other
Much faster than pure Python
equivalents Universal Functions
Universal Functions apply to an entire array as a loop and
support vectorized operations; vectorized wrappers for a
function
arrayName**2 OR np.power(2,arrayName): squares array
arrayName < 5: returns T/F matrix
arr1 + arr2 OR np.add(arr1, arr2)
arr1 * arr2 OR np.multiply(arr1, arr2) Aggregate Functions
Aggregate Functions apply to the entire array and
provide key data points from the array
np.sum(arrayName) OR arrayName.sum()
np.min(arrayName) OR arrayName.min()
np.max(arrayName) OR arrayName.max()
np.mean(arrayName) OR arrayName.mean() np.where
np.where returns an array based on a condition
*similar to ternary operators in Java
**Example on next slide
np.where(condition, if true do this, if false do this)
●
● ● Parameter 1: The condition
A boolean statement with your array as the variable
Parameter 2: If true…
What should happen if the condition is true for the particular element in
your array
*Can even be another np.where statement
Parameter 3: If false…
What should happen if the condition is FALSE for the particular element np.where example
arr = a numpy 10 x 10 numpy array of all
numbers from 0-99
If we think of our numpy array as a list of 100
numbers, the following python logic is
equivalent to the logic of the np.where
statement in the example:
for element in arr:
if element % 5 == 0:
#Replace element with 0
elif element % 7 == 0:
#Replace element with 0
else:
#Leave element alone Masking and Boolean Ufuncs
You can use an array of booleans to
index into an array - this is called
masking
arrayName[[list of T/F booleans]]
You can combine masking with
vectorized operations to manipulate
certain elements of an array Indexing Arrays
Use indexing to ﬁnd an element at a
speciﬁc position in a speciﬁc row
Syntax:
arrayName[a,b]
where a is the row, and b is the column/
position of the element in the row *Can also use negative indices Slicing Arrays
Use slicing to retrieve pieces of your
array - can return things like:
●
●
● Single value (indexing)
Subarray
Single column as array Similar syntax to indexing
arrayName[row:Slice , column:Slice] NumPy with File IO and CSV
.loadtxt makes it possible to turn ﬁles of information into arrays that we can manipulate.
np.loadtxt(open(“ﬁleName.csv”), delimiter = “,”, skiprows = 1, usecols = [1,2,4], dtype = str)
The above line of code will…
●
Load ﬁleName.csv as a numpy array ( matrix)
●
Take “,” as the delimiter between data
●
Skip the ﬁrst row (useful if the ﬁrst row is a header that you won’t need, if skiprows = 2, we’d skip the ﬁrst 2 rows)
●
The resulting array will only include the 2nd, 3rd, and 5th columns
●
Interpret all data as strings *Note: The only required attributes are the ﬁle object and the delimiter. Default dtype will be a ﬂoat, so if
your data are not ﬂoats, you need to specify that
**np.genfromtxt works similarly, but it infers the dtype from the ﬁle if you have mixed data types in your ﬁle Questions? ...

View
Full Document

- Spring '12
- summet