%autosave 2
import numpy as np
%%timeit a = np.arange(10000)
b = a.cumsum()
np.std(b, ddof=1)
a = np.arange(16).reshape(4, 4)
a
a[1, 2]
a[1:, :-1]
a[a % 2 == 0]
b = a[0]
a[b % 3 == 0][[1, 0], [3, 2]]
a = np.arange(16)
np.sum(a[a % 2 == 0])
a = np.arange(16).reshape(4, -1)
a[np.arange(a.shape[0]), np.arange(a.shape[1])]
np.diag(a)
np.diag(a, k=1)
np.diag_indices_from(a)
np.diag_indices(4)
a[np.tril_indices_from(a)] = 0
a
a = np.arange(10, dtype=float)
b = np.ones(10, dtype=float)
np.hstack([a, b])
A = np.arange(10, dtype=float).reshape(5, 2)
B = np.ones((5, 2), dtype=float)
print(A)
print(B)
np.hstack([A, B])
np.vstack([a, b])
np.vstack([A, B])
np.concatenate([a, b])
np.concatenate([A, B])
np.stack([a, b], axis=0)
np.stack([a, b], axis=1)
np.stack([a, b], axis=-1)
%%timeit lines = [np.full(16, i) for i in range(128)]
np.vstack(lines)
%%timeit lines = [np.full(16, i) for i in range(128)]
x = lines[0]
for y in lines[1:]:
x = np.vstack([x, y])
?np.block
np.r_[np.ones(10), 2]
np.r_[0:10]
np.r_[0:10:2]
np.r_[0.0:100.0:5.0j]
a, step = np.linspace(0.0, 10.0, 11, retstep=True)
print(a)
print(step)
np.arange(0, 9./7., 1./7.)
np.linspace(0.0, 8./7., 9)
np.logspace(0.0, 2.0, 3)
np.r_[1:10:3, 0.5:1.5:11j, 10, np.arange(10)**2]
np.mgrid[0:10, 0:5]
def f(x, y):
return x**2 + y**3
f(*np.mgrid[0:10, 0:5])
np.meshgrid(np.arange(10), np.arange(5))
x = np.ones((3, 4), dtype=[('a', np.uint8), ('b', np.float)])
x[0, 0]['a'], x[0, 0]['b']
a = x['a']
assert not a.flags.owndata
a
x = np.rec.fromarrays([np.ones(10), np.arange(10)], names='a,b')
x['a']
x.a
a = np.ma.array([1, 2, 3,], mask=[True, False, True], fill_value=-1)
np.ma.sum(a)
np.sqrt([1, 2, 3, -1 + 0j])
np.emath.sqrt([1, 2, 3, -1])
import cmath
import math
cmath.sqrt(-1)
math.sqrt(-1+0j)
f = np.fft.fft(np.sin(2 * np.pi * np.linspace(0, 1, 101)))
plt.plot(f.real)
np.linalg.norm([1, 2], ord=np.inf)
np.linalg.svd
np.linalg.eig
?np.einsum
np.random.rand()
rng = np.random.default_rng()
rng.random()
rng.normal(loc=1.0, scale=5.0, size=(4, 5))
rng.bytes(100)
a = np.arange(16) ** 2
rng.choice(a, 17, replace=True)
rng.shuffle(a)
a
import random
from numpy.testing import assert_allclose, assert_array_equal
a = np.linspace(0, 1, 10, dtype=np.float32)
b = (a ** 7.5) ** (1./7.5)
assert_allclose(a, b, rtol=1e-6)
assert_array_equal(np.arange(10), np.r_[0:10])
assert np.all(np.arange(10) == np.r_[0:10])