python intro

Contents

1. 1 python
link to my programming story in the course files

I need to provide a link to the best web page that I know of that contains forecast verification methodology information:

I have used some of the equations from that page (since the Google docs equation editor doesn't translate to Google sites)

# basically, the stuff in this courier font is code
# you should be able to copy and paste it into the python shell
# things that begin with # are comment lines and are not executed

python

what is python?
http://pyaos.johnny-lin.com/

connect to jupyterhub on scholar.rcac.purdue.edu

open a new notebook using "New" and select Baldwin Python 2

print 'hello world'

it is always good to know how to get out of a program...under File select Close and Halt:

try these to see how python handles mathematical expressions and integer data types

print 5*4
print 6/2
print 13+5+7
print 6**2

a=5/2
print a
dir(a)

variables, math, logicals
a=3.5
b=-2.1
c=3
d=4
a*b
b+c
a/c
c/d
d**c
a>b
a<=b
a==b
a!=b

strings
s1='hello'
s2='world'
s1 + s2
s1 + ' ' + s2 + "!"

lists
a=[2, 3.2, 'hello', 'eeee']
len(a)
a
a[1:3]
a='goodbye'

tuples (lists that can’t be changed)
b=(2, 3.2, 'goodbye', 'aaaa')
b

modules and packages
package is a collection of modules, I’ll probably use these interchangeably

import module_name

import numpy

p=numpy.pi
print p
numpy.sin(p/2)

import numpy as np
print np.pi

we’re going to generate some fake observations and forecasts and do some quick calculations

from scipy.stats import norm

mu=50
sigma=20
obs=norm.rvs(loc=mu,scale=sigma,size=1000)

np.max(obs)
np.min(obs)
np.mean(obs)
np.median(obs)

randerr=norm.rvs(loc=-1,scale=10,size=1000)
syserr=10.0-0.2*obs
fcst=obs+randerr+syserr
np.max(fcst)
np.min(fcst)
np.mean(fcst)
np.median(fcst)

let’s use joint distribution approach and make a scatter plot
forecast values vs. observed values (f,x)
to be consistent with contingency table, obs=x coord, fcst=y coord

need to import another package that has nice plotting modules

import matplotlib.pyplot as plt
%matplotlib inline

matplotlib is a Matlab-style plotting package, since I’m familiar with Matlab, it’s easy for me to learn

plt.plot(obs,fcst,'.')
plt.xlabel('obs')
plt.ylabel('fcst')

dd