In [2]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [3]:
np.tile([1, 2, 3], reps=2)
Out[3]:
In [4]:
np.repeat([1, 2, 3], 2)
Out[4]:
In [5]:
np.tile(np.repeat([1, 2, 3, 4], 2), 3)
Out[5]:
In [6]:
d = {'b': 12}
dict({'a': 2}, **d)
Out[6]:
In [7]:
a = np.arange(4).reshape(2, -1)
np.tile(a, (2, 3))
Out[7]:
In [8]:
a = np.arange(4).reshape(2, -1)
np.repeat(a, (2, 5), axis=0)
Out[8]:
In [9]:
a = np.arange(4).reshape(2, -1)
np.repeat(a, (2, 5), axis=1)
Out[9]:
In [8]:
a = np.array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])
b = np.array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])
np.intersect1d(a, b), np.setdiff1d(a, b)
Out[8]:
In [10]:
a = np.array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])
b = np.array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])
np.where(a == b), a[a==b]
Out[10]:
In [11]:
a[a > 4]
Out[11]:
In [12]:
a = np.arange(10).reshape(2, -1)
a[:, [1, 2, 3, 0, 4]]
Out[12]:
In [14]:
a = np.random.uniform(size=(5, 4), low=-5, high=10)
a
Out[14]:
In [15]:
(a - a.mean()) / a.std()
Out[15]:
Normalizing: squash into range [0, 1)
In [16]:
(a - a.min()) / a.ptp()
Out[16]:
In [17]:
a = np.arange(1, 11).reshape(2, -1)
a = np.array([20, -2, 3, 5, 8, 7])
np.digitize(a, bins=[1, 4, 8])
Out[17]:
In [18]:
a = np.array([1, 3, 7, 1, 2, 6, 0, 1])
diff1 = a - np.hstack((a[1:], 0))
diff2 = a - np.hstack((0, a[:-1]))
np.where((diff1>0) & (diff2>0))
Out[18]:
In [19]:
a = np.array([[3,3,3],[4,4,4],[5,5,5]])
b = np.array([1,2,3])
a - b[:, None]
Out[19]:
In [20]:
x = np.array([1, 2, 1, 1, 3, 4, 3, 1, 1, 2, 1, 1, 2])
np.where(x == 1)[0][4]
Out[20]:
In [21]:
np.arange(np.datetime64("2018-01-02"), np.datetime64("2018-01-15"), 3)
Out[21]:
In [27]:
a = np.arange(15)
stride = 2
window = 4
np.array([a[i:i+window] for i in range(0, a.shape[0]-window+1, stride)])
Out[27]:
In [24]:
import itertools
x = [0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1]
plt.step(np.arange(len(x)), x)
cut = 2
x = np.array([0] + x + [0])
up = np.where(np.diff(x) == 1)[0] + 1
down = np.where(np.diff(x) == -1)[0] + 1
delta = down - up
delta[delta > cut] = cut
x[:] = 0
x[list(itertools.chain(*(list(range(up[i], up[i]+delta[i])) for i in range(delta.shape[0]))))] = 1
x = x[1:-1]
x
plt.step(np.arange(len(x)), x)
Out[24]:
In [25]:
a = np.array([4, 3, 0, 10, 1])
order = np.argsort(-a)
a[order]
order, a[order][np.argsort(order)]
Out[25]:
In [26]:
a = np.array([[1, -1, 2], [5, 0, 0]])
np.argmax(a, -1)
a.argmax(-1)
Out[26]:
In [33]:
a = np.array([3, -1, 2, 0, 5, 2])
order = np.argsort(-a)
a[order]
Out[33]:
In [35]:
a[order][np.argsort(order)]
Out[35]:
In [36]:
[1, 2] * -1
Out[36]: