Transcript for Friday 16 November

| categories: Transcripts

Transcript for today.

# IPython log file

cd ~/Dropbox/116

# load our baby data
data = np.loadtxt('babies.txt')

# look at the first few rows to remind ourselves
data[:5]
#[Out]# array([[ 5.34,  0.  ],
#[Out]#        [ 3.1 ,  1.  ],
#[Out]#        [ 7.5 ,  1.  ],
#[Out]#        [ 8.56,  1.  ],
#[Out]#        [ 6.03,  0.  ]])

# then we wrote our BigBabies function.
import numpy as np

def BigBabies(d):
    'select big babies'
    m = np.mean(d[:,0])  # get the mean
    s = np.std(d[:,0])   # get the standard deviation
    return d[d[:,0] > m + s,:]  # get the big ones

b = BigBabies(data)
b[:5]
#[Out]# array([[ 7.5 ,  1.  ],
#[Out]#        [ 8.56,  1.  ],
#[Out]#        [ 8.25,  1.  ],
#[Out]#        [ 7.63,  1.  ],
#[Out]#        [ 7.78,  1.  ]])

# then we expanded our script to get the percentages of big boys and girls
bigones = BigBabies(data)
bigboys = bigones[bigones[:,1] == 0]
biggirls = bigones[bigones[:,1] == 1]

print 'percentage of big babies that are boys', 100.0 * len(bigboys) / len(bigones)
print 'percentage of big babies that are girls', 100.0 * len(biggirls) / len(bigones)

# then we worked on generating the first N primes.
# here is the code we put in nprimes.py
def isPrime(p):
    for i in range(2, p):
        if p % i == 0:
            return False
    return True

def nprimes(N):
    'return a list of the first N primes'
    R = []
    p = 1
    for i in range(N):
        # add a prime to R
        while not isPrime(p):
            p = p + 1
        R.append(p)
        p = p + 1
    return R

# we broke it into two functions to make coding the nprimes function easier

nprimes(5)
#[Out]# [2, 3, 5, 7, 11]

# someone asked why we needed that second p = p + 1 at the end of the loop?
# so we removed it
run nprimes.py
nprimes(5)

# now we can't get beyond 2 because it is prime so we never run the body of the while loop
#[Out]# [2, 2, 2, 2, 2]