from IPython.display import HTML
import requests
HTML(requests.get("https://git.io/fh5WI").text)
import numpy as np
x = np.array([1, 2, 3])
x
x[0]
x[0] = 100
x
x
y = np.array([2, 3, 4])
y
x + y
10*x
x**2
np.sin(x)
Numpy computations are typically a lot faster than computations done using pure Python.
np.arange(10)
%%timeit -n 5
x = np.arange(10**6)
y = x**2
%%timeit -n 5
x = range(10**6)
y = [n**2 for n in x]
import bokeh.plotting as bk
bk.output_notebook() # show plots in the notebook
x = [1, 2, 3, 4]
y = [4, 3, 2, 3]
p = bk.figure(plot_width=300, plot_height=300)
p.line(x, y, color='blue')
p.circle(x, y, size = 10, color='red')
bk.show(p)
x = np.linspace(0, 1, 5)
x
x = np.linspace(0, 12, 200)
y = np.sin(x)
z = np.cos(x)
p = bk.figure(plot_width=600, plot_height=300)
p.line(x, y, color='blue', line_width=2)
p.line(x, z, color='red', line_width=2)
bk.show(p)
Array slicing works the same as for lists:
my_array[start : stop]
my_array[start : stop : step]
a = np.arange(15)
a
a[6:10]
a[1:10:2]
a = np.random.randint(0, 100, 15)
a
large = (a > 50)
large
a
for which large
is true:
a[large]
a[a>50]
a
a[a>50] = 0
a
a = np.arange(70).reshape(10, 7)
a
a[1, 2]
a[1,2] = -1
a
a[2:6, :3]
a[1, :]
a[:, 2]
a[a%2 == 0] = 0
a
b = a**2
b
import matplotlib.pyplot as plt
x = plt.imread("bird.jpg")
x
x.shape
x = x/255
x
def imshow(x):
plt.figure(figsize=(10,10))
plt.imshow(x)
plt.show()
imshow(x)
imshow(x/2)
imshow(np.minimum(1, x*2))
imshow(x[300:750, 600:1400, :])
y = x.copy()
y[:,:,1] = 0
y[:,:,2] = 0
imshow(y)
z = x.copy()
z[300:700, 600:1400, 0] = 1
z[700:1100, 600:1400, 1] = 1
z[1100:1500, 600:1400, 2] = 1
imshow(z)