import numpy as np
np.logspace(-20, -10, 11)
np.repeat(2, 10)
np.empty(10) # sometimes returns values != 0 because uninitialized
np.zeros(5, dtype=np.float32)
x = np.random.random(10)
x[1:] - x[:-1]
np.diff(x)
filename = "data/munich_temperatures_average_with_bad_data.txt"
date, temperature = np.loadtxt(filename, unpack=True)
keep = np.abs(temperature) < 50
date_good = date[keep]
temperature_good = temperature[keep]
# 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]
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(date, temperature, '.')
plt.plot(date % 1, temperature, '.')
def gaussian(x, amplitude, offset, sigma):
return 1. / np.sqrt(2 * np.pi) / sigma * np.exp(-0.5 * (x - offset) ** 2 / sigma ** 2)
values = np.random.normal(0, 1, 100000)
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)
total = np.zeros(10000)
values = np.random.random(10000)
total = total + values
h = plt.hist(total, bins=30)
values = np.random.random(10000)
total = total + values
h = plt.hist(total / 2., bins=30)
for iter in range(8): # already did it twice
values = np.random.random(10000)
total = total + values
h = plt.hist(total / 10., bins=30)
import os
import glob
for filename in glob.glob('*'):
print(filename, os.path.getsize(filename) / 1024)
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)
to_string(2006,3,22,13,12,55)
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))
to_values('2006-22-03 13:12:55')