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=''.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 if n < 5000: print('Smaller half') else: print('Bigger half') n = random.randint(1e3, 1e4-1) # that's 10^3 print(n) %%bash #echo $PATH py=$(which python) echo "Current python executable: $py" !ls ../data/ fls = !ls ../data/ fls !head ../data/{fls[-3]} import pandas as pd df = pd.read_csv('../data/'+fls[-3]) df.head(4) from sympy import init_printing, Symbol, cos, sin init_printing(use_latex='mathjax') x = Symbol('x') print(cos(x).series(x, 0, 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}); 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)); 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); # 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()