import numpy as np
import matplotlib.pyplot as plt
import mpld3
from IPython.display import HTML, display, YouTubeVideo
# Load CSS
display(HTML(open('style.css').read()))
YouTubeVideo('wjSsISiacEU', width=600, height=400)
def embed_url(url, width=800, height=450):
src='<iframe src="{0}" width="{1}" height="{2}"></iframe>'.format(url, 800, 450)
display(HTML(src))
embed_url('http://worrydream.com/ExplorableExplanations/')
embed_url('https://en.wikipedia.org/wiki/Literate_programming')
embed_url('http://ipython.org/notebook.html')
import random
# generate a random number between 1000 and 9999
n = random.randint(1e3, 1e4-1)
n*2
15566
Code cells are executable mini-editors. It's like having a bunch of interactive shells sharing the same namespace.
if n < 5000:
print('Smaller half')
else:
print('Bigger half')
Bigger half
n = random.randint(1e3, 1e4-1) # that's 10^3
print(n)
5405
$10^3$ is much better than '10^3' to explan '1e3
'.
%%bash
#echo $PATH
py=$(which python)
echo "Current python executable: $py"
Current python executable: /Users/ivan/dev/pve/dev3/bin/python
!ls ../data/
2013-Nov_chicago-salaries.csv.gz UN_population.csv 20131117_bash_history.txt countries.json UN_infant_mortality.csv population.txt
fls = !ls ../data/
fls
['2013-Nov_chicago-salaries.csv.gz', '20131117_bash_history.txt', 'UN_infant_mortality.csv', 'UN_population.csv', 'countries.json', 'population.txt']
!head ../data/{fls[-3]}
import pandas as pd
df = pd.read_csv('../data/'+fls[-3])
df.head(4)
Country | Subgroup | Year | Unit | Value | Value Footnotes | |
---|---|---|---|---|---|---|
0 | Afghanistan | Female | 2005 | Number | 12082881 | NaN |
1 | Afghanistan | Female | 2002 | Number | 9844300 | 1 |
2 | Afghanistan | Female | 2000 | Number | 9993697 | NaN |
3 | Afghanistan | Female | 1995 | Number | 8792425 | NaN |
from sympy import init_printing, Symbol, cos, sin
init_printing(use_latex='mathjax')
x = Symbol('x')
print(cos(x).series(x, 0, 7))
1 - x**2/2 + x**4/24 - x**6/720 + O(x**7)
display(cos(x).series(x, 0, 5))
from IPython.html.widgets import interact, ToggleButtonsWidget
def show_series(n):
display(cos(x).series(x, 0, n))
interact(show_series, n=(3,25));
import string
def gen_passwd(length=6, punctuation=False):
chars = string.ascii_letters + string.digits
if punctuation:
chars += string.punctuation
passwd = [random.choice(chars) for _ in range(length)]
return ''.join(passwd)
def print_passwd(length=6, punctuation=False):
print("New password: " + gen_passwd(length, punctuation))
interact(print_passwd,
length=(6,20),
punctuation={"no": False, "yes":True});
New password: qwR6Fd
from IPython.display import Audio, clear_output
duration = 3
sampling = 8000
ts = np.linspace(0, duration, sampling*duration)
def sig_gen(time, f0=220.0, f1=224.0):
two_pi = 2*np.pi
sig = np.sin(two_pi*f0*time) + np.sin(two_pi*f1*time)
return sig
display(Audio(data=sig_gen(ts), rate=sampling))
def play_sig(f0=220.0, f1=224.0):
sig = sig_gen(ts, f0, f1)
display(Audio(data=sig, rate=sampling, autoplay=True))
interact(play_sig, f0=(200.0, 300.0), f1=(200.0, 300.0));
%matplotlib inline
def play_sig(f0=220.0, f1=224.0):
sig = sig_gen(ts, f0, f1)
display(Audio(data=sig, rate=sampling, autoplay=True))
plt.plot(ts[:sampling/2], sig[:sampling/2])
interact(play_sig, f0=(200.0, 300.0), f1=(200.0, 300.0));
-c:4: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future -c:4: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
import mpld3
mpld3.enable_notebook()
t = ts[:4000]
s = sig_gen(t, f0=220.0, f1=224.0)
plt.plot(t, s);
npts = 50
rnd_err = np.random.randn(npts)*15
# simulate quadratic process
a, b, c = 0.5, -2.4, -8.0
x = np.linspace(-10, 20, npts)
y = a * x**2 + b * x + c
dat = y + rnd_err
# fit
p = np.polyfit(x, dat, 2)
y_fit = np.polyval(p, x)
#plt.plot(x, dat, 'ko', mfc='none');
plt.plot(x, dat, 'ko');
plt.plot(x, y, 'g');
plt.plot(x, y_fit, 'r');
mpld3.disable_notebook()
npts = 500
x1 = np.random.normal(-1, 1, size=npts)
x2 = np.random.normal(1, 1, size=npts)
x3 = np.random.normal(2, 1, size=npts)
plt.hist(x1, normed=True);
plt.hist(x2, normed=True);
plt.hist(x3, normed=True);
matplotlib
is a good plotting library. It can be used to create beautiful visualizations, but not with the default plotting style.
# better default plotting style
import seaborn as sb
plt.hist(x1, normed=True);
plt.hist(x2, normed=True);
plt.hist(x3, normed=True);
# works with d3
mpld3.enable_notebook()
plt.hist(x1, normed=True);
plt.hist(x2, normed=True);
plt.hist(x3, normed=True);
# lot's of styles and helper plots
sb.set_style('whitegrid')
sb.set_palette("deep")
#sb.set_palette(sb.color_palette("Set2"))
nbins = 10
sb.distplot(x1, bins=nbins);
sb.distplot(x2, bins=nbins);
sb.distplot(x3, bins=nbins);
sb.palplot(sb.color_palette())
mpld3.disable_notebook()