10. Introduction to Numpy

Exercise 1

In [1]:
import numpy as np
np.logspace(-20, -10, 11)
Out[1]:
array([  1.00000000e-20,   1.00000000e-19,   1.00000000e-18,
         1.00000000e-17,   1.00000000e-16,   1.00000000e-15,
         1.00000000e-14,   1.00000000e-13,   1.00000000e-12,
         1.00000000e-11,   1.00000000e-10])
In [2]:
np.repeat(2, 10)
Out[2]:
array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
In [3]:
np.empty(10)  # sometimes returns values != 0 because uninitialized
Out[3]:
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
In [4]:
np.zeros(5, dtype=np.float32)
Out[4]:
array([ 0.,  0.,  0.,  0.,  0.], dtype=float32)

Exercise 2

In [5]:
x = np.random.random(10)
In [6]:
x[1:] - x[:-1]
Out[6]:
array([-0.39425923, -0.10499752,  0.1186491 ,  0.22141307,  0.01082329,
       -0.35096401, -0.26005718,  0.07962309,  0.48803232])
In [7]:
np.diff(x)
Out[7]:
array([-0.39425923, -0.10499752,  0.1186491 ,  0.22141307,  0.01082329,
       -0.35096401, -0.26005718,  0.07962309,  0.48803232])

Exercise 3

In [8]:
filename = "data/munich_temperatures_average_with_bad_data.txt"
date, temperature = np.loadtxt(filename, unpack=True)
In [9]:
keep = np.abs(temperature) < 50
In [10]:
date_good = date[keep]
temperature_good = temperature[keep]

11. Introduction to Matplotlib

Exercise 1

In [11]:
# The following code reads in the file and removes bad values
date, temperature = np.loadtxt('data/munich_temperatures_average_with_bad_data.txt', unpack=True)
keep = np.abs(temperature) < 90
date = date[keep]
temperature = temperature[keep]
In [12]:
%matplotlib inline
import matplotlib.pyplot as plt
In [13]:
plt.plot(date, temperature, '.')
Out[13]:
[<matplotlib.lines.Line2D at 0x10c1b6208>]
In [14]:
plt.plot(date % 1, temperature, '.')
Out[14]:
[<matplotlib.lines.Line2D at 0x10f36a898>]

Exercise 2

In [15]:
def gaussian(x, amplitude, offset, sigma):
    return 1. / np.sqrt(2 * np.pi) / sigma * np.exp(-0.5 * (x - offset) ** 2 / sigma ** 2)
In [16]:
values = np.random.normal(0, 1, 100000)
In [17]:
h = plt.hist(values, bins=100, normed=True)
x = np.linspace(-5, 5, 100)
plt.plot(x, gaussian(x, 1, 0, 1), color='red', lw=2)
Out[17]:
[<matplotlib.lines.Line2D at 0x10f41aa20>]

Exercise 3

In [18]:
total = np.zeros(10000)
In [19]:
values = np.random.random(10000)
total = total + values
In [20]:
h = plt.hist(total, bins=30)
In [21]:
values = np.random.random(10000)
total = total + values
In [22]:
h = plt.hist(total / 2., bins=30)
In [23]:
for iter in range(8):  # already did it twice
    values = np.random.random(10000)
    total = total + values
In [24]:
h = plt.hist(total / 10., bins=30)

12. Files and paths

In [25]:
import os
import glob
for filename in glob.glob('*'):
    print(filename, os.path.getsize(filename) / 1024)
10. Introduction to Numpy.ipynb 39.630859375
Practice Problem - Monte-Carlo Error Propagation - Sample Solution.ipynb 38.880859375
Problem Set 2.ipynb 7.595703125
11. Introduction to Matplotlib.ipynb 184.7734375
Wednesday Exercise Solutions.ipynb 7.6953125
13. String Formatting.ipynb 5.5634765625
12. Files and paths.ipynb 4.4384765625
my_plot.png 1.244140625
data 0.1875
Practice Problem - Monte-Carlo Error Propagation.ipynb 3.818359375

13. String Formatting

Exercise 1

In [26]:
def to_string(year, day, month, hour, minute, second):
    date = "{0:04d}-{1:02d}-{2:02d}".format(year, month, day)
    time = "{0:02d}:{1:02d}:{2:02d}".format(hour, minute, second)
    return "{date} {time}".format(date=date, time=time)
In [27]:
to_string(2006,3,22,13,12,55)
Out[27]:
'2006-22-03 13:12:55'
In [28]:
def to_values(string):
    date, time = string.split()
    year, month, day = date.split('-')
    hour, minute, second = time.split(':')
    return (int(year), int(month), int(day),
            int(hour), int(minute), int(second))
In [29]:
to_values('2006-22-03 13:12:55')
Out[29]:
(2006, 22, 3, 13, 12, 55)