WARMUP
Set is a game between two players. There are cards featuring symbols with various properties (4 properties, each with the 3 possible values) in a deck which contains all possible combinations of features. Initially, 12 cards are drawn randomly from the deck and revealed on the board.
The players try to be the first to identify sets of three cards on the board where, for each feature on the cards, either all of the cards agree (e.g., all of the symbols are the same color) or all of the cards disagree (e.g., the symbols are diamonds on one card, pills on another, and squiggles on the last).
When a player identifies a set, that player removes those cards from the board and gets a point. Three cards are drawn from the deck to replace them. If no sets are present, then cards are added to the board three at a time until a set is present.
When the deck runs out of cards, and no more sets are on the board, the player with the most points wins.
HW Review
What did people have the most trouble figuring out? ask random
Programming
Thing big: what was this homework about? ask random
Key Research and Programming Idea
- use already programmed modules, especially when they have a large community supporting, finding bugs, etc.
- most research work involves complex integro-differential models, so know the appropriate libraries for this work
numpy
The numpy
library provides a variety of methods for creating and working with array-structured data, particularly numerical data. It also has some limited numerical methods.
__author__ = 'cap10'
import numpy as np
def simpsons(f, a, b, n):
deltaX = (b-a)/n
points = np.linspace(a, b, n+1)
fpoints = f(points)
coeffs = np.zeros(n-1)
coeffs[0::2] = 4
coeffs[1::2] = 2
# coeffs should be 4,2,4,...,2,4
res = fpoints[0] + np.sum(coeffs*fpoints[1:-1]) + fpoints[-1]
# res = fpoints[0] + 4*np.sum(fpoints[1:-1:2])
# + 2*np.sum(fpoints[2:-1:2]) + fpoints[-1]
return deltaX/3*res
def midpoint(f, a, b, n):
deltaX = (b-a)/n
start = a + deltaX/2
end = b - deltaX/2
mids = np.linspace(start, end, n)
fs = f(mids)
fsum = np.sum(fs)
return deltaX*fsum
def trapezoid(f, a, b, n):
deltaX = (b-a)/n
points = np.linspace(a, b, n+1)
fpoints = f(points)
res = fpoints[0]+fpoints[-1]+2*np.sum(fpoints[1:-1])
return res * deltaX/2
poly_coeffs = [1,2,3,5]
def f1(x):
return x**3 + 2*x**2 + 3*x + 5
pf1 = np.poly1d(poly_coeffs)
a, b = 0, 10
n = 100
print(simpsons(f1,a,b,n))
print(midpoint(f1,a,b,n))
print(trapezoid(f1,a,b,n))
pfi = pf1.integ()
print(pfi(b)-pfi(a))
scipy
The scipy
library provides several tools for numerical methods - differentiation, integration, special functions, etc. These are designed to work with numpy
structures.
import scipy, scipy.integrate
# modified based on http://people.oregonstate.edu/~medlockj/other/python/SIR.py
# Parameters
from collections import namedtuple
Params = namedtuple('Params','beta gamma mu N0')
params = Params(beta=5, gamma=1, mu=1/50, N0=1)
S0, R0 = 0.99, 0.
I0 = params.N0 - S0 - R0
Y0 = [ S0, I0 ]
tMax = 100
T = scipy.linspace(0, tMax, 1001)
def dSIR(Y, t, beta, gamma, mu, N0):
"""
SIR model, for const. pop.
@param Y: [S, I]
@param t: the time step
@param beta, gamma, nu, N0: model parameters
@return: [dS, dI]
"""
S, I = Y
dS = mu * N0 - beta * I * S - mu * S
dI = beta * I * S - (gamma + mu) * I
return [ dS, dI ]
# Integrate the ODE
# Warning! The ODE solver over-writes the initial value.
solution = scipy.integrate.odeint(dSIR,
Y0,
T,
args = params)
S = solution[:, 0]
I = solution[:, 1]
R = params.N0 - S - I
import pylab
pylab.figure()
pylab.plot(T, S,
T, I,
T, R)
pylab.xlabel('Time')
pylab.ylabel('Proportion')
pylab.legend([ 'Susceptible', 'Infective', 'Recovered' ])
pylab.show()
Advanced Work
Add 5 functions to a fun_plots2.py
file, then examine these with you make_fun_plots.py
script.
HOMEWORK
Using this csv of loan data, write a python script to load the data, and get the total amount of funds requested for education.
Using this json of donut menu data, write a python script to load the data, and write out each combination of donuts, batters, and (single) toppings.