A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://nbviewer.ipython.org/github/pydata/pydata-book/blob/3rd-edition/ch04.ipynb below:

Jupyter Notebook Viewer

  1. pydata-book
  2. ch04.ipynb
Notebook

In [1]:

import numpy as np
np.random.seed(12345)
import matplotlib.pyplot as plt
plt.rc("figure", figsize=(10, 6))
np.set_printoptions(precision=4, suppress=True)

In [2]:

import numpy as np

my_arr = np.arange(1_000_000)
my_list = list(range(1_000_000))

In [3]:

%timeit my_arr2 = my_arr * 2
%timeit my_list2 = [x * 2 for x in my_list]

In [4]:

import numpy as np
data = np.array([[1.5, -0.1, 3], [0, -3, 6.5]])
data

In [7]:

data1 = [6, 7.5, 8, 0, 1]
arr1 = np.array(data1)
arr1

In [8]:

data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
arr2 = np.array(data2)
arr2

In [11]:

np.zeros(10)
np.zeros((3, 6))
np.empty((2, 3, 2))

In [13]:

arr1 = np.array([1, 2, 3], dtype=np.float64)
arr2 = np.array([1, 2, 3], dtype=np.int32)
arr1.dtype
arr2.dtype

In [14]:

arr = np.array([1, 2, 3, 4, 5])
arr.dtype
float_arr = arr.astype(np.float64)
float_arr
float_arr.dtype

In [15]:

arr = np.array([3.7, -1.2, -2.6, 0.5, 12.9, 10.1])
arr
arr.astype(np.int32)

In [16]:

numeric_strings = np.array(["1.25", "-9.6", "42"], dtype=np.string_)
numeric_strings.astype(float)

In [17]:

int_array = np.arange(10)
calibers = np.array([.22, .270, .357, .380, .44, .50], dtype=np.float64)
int_array.astype(calibers.dtype)

In [18]:

zeros_uint32 = np.zeros(8, dtype="u4")
zeros_uint32

In [19]:

arr = np.array([[1., 2., 3.], [4., 5., 6.]])
arr
arr * arr
arr - arr

In [21]:

arr2 = np.array([[0., 4., 1.], [7., 2., 12.]])
arr2
arr2 > arr

In [22]:

arr = np.arange(10)
arr
arr[5]
arr[5:8]
arr[5:8] = 12
arr

In [23]:

arr_slice = arr[5:8]
arr_slice

In [26]:

arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
arr2d[2]

In [28]:

arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
arr3d

In [30]:

old_values = arr3d[0].copy()
arr3d[0] = 42
arr3d
arr3d[0] = old_values
arr3d

In [36]:

lower_dim_slice = arr2d[1, :2]

In [41]:

names = np.array(["Bob", "Joe", "Will", "Bob", "Will", "Joe", "Joe"])
data = np.array([[4, 7], [0, 2], [-5, 6], [0, 0], [1, 2],
                 [-12, -4], [3, 4]])
names
data

In [44]:

data[names == "Bob", 1:]
data[names == "Bob", 1]

In [45]:

names != "Bob"
~(names == "Bob")
data[~(names == "Bob")]

In [46]:

cond = names == "Bob"
data[~cond]

In [47]:

mask = (names == "Bob") | (names == "Will")
mask
data[mask]

In [49]:

data[names != "Joe"] = 7
data

In [50]:

arr = np.zeros((8, 4))
for i in range(8):
    arr[i] = i
arr

In [53]:

arr = np.arange(32).reshape((8, 4))
arr
arr[[1, 5, 7, 2], [0, 3, 1, 2]]

In [54]:

arr[[1, 5, 7, 2]][:, [0, 3, 1, 2]]

In [55]:

arr[[1, 5, 7, 2], [0, 3, 1, 2]]
arr[[1, 5, 7, 2], [0, 3, 1, 2]] = 0
arr

In [56]:

arr = np.arange(15).reshape((3, 5))
arr
arr.T

In [57]:

arr = np.array([[0, 1, 0], [1, 2, -2], [6, 3, 2], [-1, 0, -1], [1, 0, 1]])
arr
np.dot(arr.T, arr)

In [60]:

samples = np.random.standard_normal(size=(4, 4))
samples

In [61]:

from random import normalvariate
N = 1_000_000
%timeit samples = [normalvariate(0, 1) for _ in range(N)]
%timeit np.random.standard_normal(N)

In [62]:

rng = np.random.default_rng(seed=12345)
data = rng.standard_normal((2, 3))

In [64]:

arr = np.arange(10)
arr
np.sqrt(arr)
np.exp(arr)

In [65]:

x = rng.standard_normal(8)
y = rng.standard_normal(8)
x
y
np.maximum(x, y)

In [66]:

arr = rng.standard_normal(7) * 5
arr
remainder, whole_part = np.modf(arr)
remainder
whole_part

In [67]:

arr
out = np.zeros_like(arr)
np.add(arr, 1)
np.add(arr, 1, out=out)
out

In [68]:

points = np.arange(-5, 5, 0.01) # 100 equally spaced points
xs, ys = np.meshgrid(points, points)
ys

In [69]:

z = np.sqrt(xs ** 2 + ys ** 2)
z

In [70]:

import matplotlib.pyplot as plt
plt.imshow(z, cmap=plt.cm.gray, extent=[-5, 5, -5, 5])
plt.colorbar()
plt.title("Image plot of $\sqrt{x^2 + y^2}$ for a grid of values")

In [73]:

xarr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])
yarr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])
cond = np.array([True, False, True, True, False])

In [74]:

result = [(x if c else y)
          for x, y, c in zip(xarr, yarr, cond)]
result

In [75]:

result = np.where(cond, xarr, yarr)
result

In [76]:

arr = rng.standard_normal((4, 4))
arr
arr > 0
np.where(arr > 0, 2, -2)

In [77]:

np.where(arr > 0, 2, arr) # set only positive values to 2

In [78]:

arr = rng.standard_normal((5, 4))
arr
arr.mean()
np.mean(arr)
arr.sum()

In [79]:

arr.mean(axis=1)
arr.sum(axis=0)

In [80]:

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7])
arr.cumsum()

In [81]:

arr = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
arr

In [82]:

arr.cumsum(axis=0)
arr.cumsum(axis=1)

In [83]:

arr = rng.standard_normal(100)
(arr > 0).sum() # Number of positive values
(arr <= 0).sum() # Number of non-positive values

In [84]:

bools = np.array([False, False, True, False])
bools.any()
bools.all()

In [85]:

arr = rng.standard_normal(6)
arr
arr.sort()
arr

In [86]:

arr = rng.standard_normal((5, 3))
arr

In [87]:

arr.sort(axis=0)
arr
arr.sort(axis=1)
arr

In [88]:

arr2 = np.array([5, -10, 7, 1, 0, -3])
sorted_arr2 = np.sort(arr2)
sorted_arr2

In [89]:

names = np.array(["Bob", "Will", "Joe", "Bob", "Will", "Joe", "Joe"])
np.unique(names)
ints = np.array([3, 3, 3, 2, 2, 1, 1, 4, 4])
np.unique(ints)

In [91]:

values = np.array([6, 0, 0, 3, 2, 5, 6])
np.in1d(values, [2, 3, 6])

In [92]:

arr = np.arange(10)
np.save("some_array", arr)

In [93]:

np.load("some_array.npy")

In [94]:

np.savez("array_archive.npz", a=arr, b=arr)

In [95]:

arch = np.load("array_archive.npz")
arch["b"]

In [96]:

np.savez_compressed("arrays_compressed.npz", a=arr, b=arr)

In [97]:

!rm some_array.npy
!rm array_archive.npz
!rm arrays_compressed.npz

In [98]:

x = np.array([[1., 2., 3.], [4., 5., 6.]])
y = np.array([[6., 23.], [-1, 7], [8, 9]])
x
y
x.dot(y)

In [101]:

from numpy.linalg import inv, qr
X = rng.standard_normal((5, 5))
mat = X.T @ X
inv(mat)
mat @ inv(mat)

In [102]:

import random
position = 0
walk = [position]
nsteps = 1000
for _ in range(nsteps):
    step = 1 if random.randint(0, 1) else -1
    position += step
    walk.append(position)

In [105]:

nsteps = 1000
rng = np.random.default_rng(seed=12345)  # fresh random generator
draws = rng.integers(0, 2, size=nsteps)
steps = np.where(draws == 0, 1, -1)
walk = steps.cumsum()

In [107]:

(np.abs(walk) >= 10).argmax()

In [108]:

nwalks = 5000
nsteps = 1000
draws = rng.integers(0, 2, size=(nwalks, nsteps)) # 0 or 1
steps = np.where(draws > 0, 1, -1)
walks = steps.cumsum(axis=1)
walks

In [110]:

hits30 = (np.abs(walks) >= 30).any(axis=1)
hits30
hits30.sum() # Number that hit 30 or -30

In [111]:

crossing_times = (np.abs(walks[hits30]) >= 30).argmax(axis=1)
crossing_times

In [113]:

draws = 0.25 * rng.standard_normal((nwalks, nsteps))

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4