MTH 337: Week 10

Week 9 Notebook

In [1]:
%pylab inline
Populating the interactive namespace from numpy and matplotlib

numpy.random.rand

  • Generates samples drawn from [0, 1)
  • Syntax is random.rand(d0, d1, ... , dn)
  • Returns an array of shape (d0, d1, ... , dn)
In [2]:
print random.rand(10)
[ 0.43734441  0.05376485  0.65019626  0.64317677  0.758315    0.51261784
  0.61417608  0.86960852  0.19470002  0.85346397]
In [3]:
print random.rand(3, 4)
[[ 0.04455306  0.43567292  0.30428789  0.75031996]
 [ 0.50669334  0.20921099  0.68207593  0.20228728]
 [ 0.50773418  0.02983001  0.87583893  0.27668114]]

Importing the "mysteryf" function for Report 8: Exercise 2

In [4]:
from mystery import mysteryf
In [5]:
# Testing it works
mysteryf(10)
Out[5]:
2090

The mysteryf function can be called directly on an array.

In [6]:
print mysteryf(arange(5))
[  0   2  18  60 140]

"polar" plots equations in polar coordinates

In [7]:
theta = linspace(0, 2*pi, 400)
r = 2 + cos(7*theta)
polar(theta, r)
Out[7]:
[<matplotlib.lines.Line2D at 0x7f1722ed1310>]

"Axis-dependent" operations

The functions sum, mean, min and max can all be called on one-dimensional arrays.

In [8]:
a = arange(10)
print a
print "Sum  =", sum(a)
print "Mean =", mean(a)
print "Min  =", min(a)
print "Max  =", max(a)
[0 1 2 3 4 5 6 7 8 9]
Sum  = 45
Mean = 4.5
Min  = 0
Max  = 9

The same can be done for higher-dimensional arrays, although we now have to use amin and amax rather than min and max.

When these functions are called, they get applied to every element of a higher-dimensional array as if it were a one-dimensional array.

In [9]:
a = arange(12).reshape((3, 4))
print a
print "Sum  =", sum(a)
print "Mean =", mean(a)
print "Min  =", amin(a)
print "Max  =", amax(a)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
Sum  = 66
Mean = 5.5
Min  = 0
Max  = 11

We can also apply these operations to a single axis (such as down a column or across a row), by using the "axis" keyword.

Using "axis=0" means that the operations are applied to the elements in a column. Using sum in this way (for example) sums the elements of each column, and stores each sum separately in the resulting 1D array.

In [10]:
a = arange(12).reshape((3, 4))
print a
print "Sum  =", sum(a, axis=0)
print "Mean =", mean(a, axis=0)
print "Min  = ", amin(a, axis=0)
print "Max  =", amax(a, axis=0)
print "Array shape  =", a.shape
print "Result shape =", sum(a, axis=0).shape
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
Sum  = [12 15 18 21]
Mean = [ 4.  5.  6.  7.]
Min  =  [0 1 2 3]
Max  = [ 8  9 10 11]
Array shape  = (3, 4)
Result shape = (4,)

Note that the size of axis 0 has been lost in the result (as seen in the loss of a '3' from the shape). The original array has been "compressed" vertically along axis 0 (the columns) to create the result.

If instead we wanted to apply an operation to the elements of a row, we would use "axis=1" instead. The original array is then "compressed" horizontally along axis 1 (the rows) to create the result.

In [11]:
a = arange(12).reshape((3, 4))
print a
print "Sum =", sum(a, axis=1)
print "Mean =", mean(a, axis=1)
print "Min = ", amin(a, axis=1)
print "Max =", amax(a, axis=1)
print "Array shape  =", a.shape
print "Result shape =", sum(a, axis=1).shape
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
Sum = [ 6 22 38]
Mean = [ 1.5  5.5  9.5]
Min =  [0 4 8]
Max = [ 3  7 11]
Array shape  = (3, 4)
Result shape = (3,)

Sums of Uniform Random Numbers

We've seen that a histogram of the output of random.rand shows an approximately uniform distribution.

What does the distribution of the sum of M random variables look like, for M = 2, 3, 4, ...?

In [12]:
npts = 100000
M = 2
a = random.rand(npts, M)
b = sum(a, axis=1)
hist(b, bins=50, normed=True)
xlabel('Sum')
Out[12]:
<matplotlib.text.Text at 0x7f1722f7bfd0>