Video Demo

This notebook shows off some fun features. It was the notebook I played around with in the demo video online. video


In [1]:
2+2


Out[1]:
4

In [3]:
2**128


Out[3]:
340282366920938463463374607431768211456L

In [5]:
def factorial(n):
    r = 1
    for i in range(n):
        r *= i + 1
    return r

factorial(3.5)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-64f484c5b11f> in <module>()
      5     return r
      6 
----> 7 factorial(3.5)

<ipython-input-5-64f484c5b11f> in factorial(n)
      1 def factorial(n):
      2     r = 1
----> 3     for i in range(n):
      4         r *= i + 1
      5     return r

TypeError: range() integer end argument expected, got float.

The Factorial Function

The factorial function is defined by:

$$f(n) = \prod_{i=1}^n i$$

In [7]:
print("Hello, I am a pineapple")


Hello, I am a pineapple

In [9]:
from time import sleep
for i in range(5):
    print("Hello, I am a pineapple")
    sleep(0.5)


Hello, I am a pineapple
Hello, I am a pineapple
Hello, I am a pineapple
Hello, I am a pineapple
Hello, I am a pineapple

In [2]:
import numpy as np

M = np.array(range(13))
N = M.reshape(13,1)
M * N


Out[2]:
array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12],
       [  0,   2,   4,   6,   8,  10,  12,  14,  16,  18,  20,  22,  24],
       [  0,   3,   6,   9,  12,  15,  18,  21,  24,  27,  30,  33,  36],
       [  0,   4,   8,  12,  16,  20,  24,  28,  32,  36,  40,  44,  48],
       [  0,   5,  10,  15,  20,  25,  30,  35,  40,  45,  50,  55,  60],
       [  0,   6,  12,  18,  24,  30,  36,  42,  48,  54,  60,  66,  72],
       [  0,   7,  14,  21,  28,  35,  42,  49,  56,  63,  70,  77,  84],
       [  0,   8,  16,  24,  32,  40,  48,  56,  64,  72,  80,  88,  96],
       [  0,   9,  18,  27,  36,  45,  54,  63,  72,  81,  90,  99, 108],
       [  0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120],
       [  0,  11,  22,  33,  44,  55,  66,  77,  88,  99, 110, 121, 132],
       [  0,  12,  24,  36,  48,  60,  72,  84,  96, 108, 120, 132, 144]])

In [11]:
%timeit np.dot(M * N, M * N)


The slowest run took 1668.50 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 12.3 µs per loop

In [3]:
%matplotlib inline
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y = np.cos(x + np.cos(x))

plt.plot(x, y);



In [6]:
import urllib.request
import io
png = io.BytesIO(
    urllib.request.urlopen('http://i.imgur.com/IyUsYQ8.png').read())
img = plt.imread(png)
img.shape


Out[6]:
(400, 400, 3)

In [7]:
plt.imshow(img[:, :, 0]);



In [18]:
import matplotlib
from scipy.cluster.vq import *
M = img[:,:,0].ravel()
centroids, _ = kmeans(M, 4)
q, _ = vq(M, centroids)
result = centroids[np.reshape(q, (400, 400))]

cmap = matplotlib.colors.ListedColormap([
        (0,.2,.3), (.85,.1,.13), (.44,.6,.6), (1.,.9,.65)])

plt.imshow(result, cmap=cmap);



In [22]:
from ggplot import *
ggplot(meat, aes('date', 'beef')) + geom_line() + stat_smooth(span=0.3, color='red')


Out[22]:
<ggplot: (284009353)>

In [23]:
from ivisual import *

scene = canvas()
sphere()
visual.display(scene)



In [26]:
from ivisual import *
scene = canvas('ball')
N = 60
balls = [sphere(color=color.red) for i in range(N)]
visual.display(scene)
t = 0.0
R = 3.0
while t < 20:
    rate(60)
    for i in range(N):
        a = float(i) / N * 2 * np.pi
        balls[i].pos = vector(R * np.sin(3*a + t), R * np.cos(5.0*a + t),
                              R * np.sin(2.0 * a + t))
    t += 0.01



In [ ]:
# That's all folks