In [1]:
import numpy as np
import numpy.random as npr
In [4]:
x = np.array([1,2,3])
In [8]:
x2 = np.array([[1,2,3],[4,5,6]])
In [5]:
x.max()
Out[5]:
In [6]:
np.max(x)
Out[6]:
In [9]:
x2.shape
Out[9]:
In [10]:
x2.size
Out[10]:
In [11]:
x2.dtype
Out[11]:
In [12]:
x2.strides
Out[12]:
In [18]:
x3 = np.fromstring('1-2-3', sep='-', dtype='int')
In [19]:
x3.dtype
Out[19]:
In [20]:
x3
Out[20]:
In [22]:
x3.astype('complex')
Out[22]:
In [15]:
%%file foo.txt
123-456-789abc
abc234-23-99x
In [23]:
np.fromregex('foo.txt', r'[a-c]*(\d+)-(\d+)-(\d+)[abcx]*', dtype='int')
Out[23]:
In [24]:
np.fromfunction(lambda i, j: i**2 + j**2, (4,5))
Out[24]:
In [26]:
np.arange(8,10, 0.2)
Out[26]:
In [27]:
np.zeros((3,4))
Out[27]:
In [28]:
np.ones((2,3))
Out[28]:
In [29]:
np.eye(3)
Out[29]:
In [30]:
np.eye(3, k=1)
Out[30]:
In [32]:
np.diag([1,2,3,4])
Out[32]:
In [38]:
x = np.ones((3,4), dtype='float')
In [39]:
x
Out[39]:
In [41]:
np.empty_like(x)
Out[41]:
In [42]:
np.linspace(0, 10, 11)
Out[42]:
In [44]:
np.logspace(0, 10, num=11, base=2).astype('int')
Out[44]:
In [45]:
np.meshgrid(range(3), range(4))
Out[45]:
In [46]:
npr.choice(['H', 'T'], 10, p=[0.2, 0.8])
Out[46]:
In [50]:
npr.choice(10, 20, replace=True)
Out[50]:
In [51]:
npr.binomial(10, 0.5, 3)
Out[51]:
In [52]:
npr.beta(5,1, 10)
Out[52]:
In [53]:
npr.seed(123)
npr.beta(5,1, 10)
Out[53]:
In [54]:
npr.seed(123)
npr.beta(5,1, 10)
Out[54]:
In [55]:
x = np.arange(10)
x
Out[55]:
In [56]:
npr.shuffle(x)
In [57]:
x
Out[57]:
In [58]:
npr.permutation(x)
Out[58]:
In [59]:
x = np.arange(1,17).reshape(8,2)
x
Out[59]:
In [60]:
npr.shuffle(x)
x
Out[60]:
In [63]:
x
Out[63]:
In [65]:
x[2:5, :]
Out[65]:
In [68]:
x[x % 2 == 0]
Out[68]:
In [69]:
x[[0,3,5],:]
Out[69]:
In [74]:
x = np.arange(1, 17).reshape((4,4))
In [75]:
x
Out[75]:
In [76]:
x[np.ix_([0,2], [1,3])]
Out[76]:
In [77]:
x
Out[77]:
In [78]:
np.transpose(x)
Out[78]:
In [79]:
x.T
Out[79]:
In [80]:
x = np.array([1,2,3])
In [81]:
x
Out[81]:
In [82]:
x.shape
Out[82]:
In [83]:
x @ x
Out[83]:
In [86]:
xc = x.reshape((3,1))
xc
Out[86]:
In [87]:
xr = x.reshape((1,3))
xr
Out[87]:
In [88]:
xc.T @ xc
Out[88]:
In [89]:
xc @ xc.T
Out[89]:
In [90]:
np.dot(xc, xc.T)
Out[90]:
In [91]:
x = np.arange(12).reshape(3,4)
In [92]:
x
Out[92]:
In [97]:
np.concatenate([[1,1,1], x])
In [98]:
x0 = np.ones(3)
In [99]:
x0
Out[99]:
In [100]:
np.c_[x0, x]
Out[100]:
In [107]:
np.vstack((x, x))
Out[107]:
In [108]:
y = np.r_[x, x]
In [109]:
y
Out[109]:
In [116]:
np.split(y, 2, axis=1)
Out[116]:
In [126]:
a = np.arange(10)
In [119]:
a**2
Out[119]:
In [123]:
%timeit a**2
In [124]:
%timeit [i**2 for i in a]
In [127]:
np.exp(a)
Out[127]:
In [128]:
a + a
Out[128]:
In [129]:
def f(a, b):
return a + 2*b
In [130]:
f(a, a)
Out[130]:
In [131]:
a = list(range(10))
In [132]:
f(a, a)
Out[132]:
In [133]:
fv = np.vectorize(f)
In [134]:
fv(a,a)
Out[134]:
In [136]:
np.kron([1,2,3], [2,3,4])
Out[136]:
In [137]:
np.kron(np.diag([1,2]), np.ones((2,2)))
Out[137]:
In [138]:
np.diag([1,2])
Out[138]:
In [139]:
np.ones((2,2))
Out[139]:
In [143]:
a = np.arange(1, 17).reshape((-1,2))
In [144]:
a
Out[144]:
In [147]:
np.mean(a, axis=0)
Out[147]:
In [148]:
a.mean(axis=1)
Out[148]:
In [149]:
a.var(axis=0)
Out[149]:
In [151]:
x = np.zeros((3,4), dtype='int')
x
Out[151]:
In [152]:
x.shape
Out[152]:
In [153]:
a = np.array(1)
b = np.array([1,2,3,4])
c = np.array([10,20,30])
In [154]:
a.shape, b.shape, c.shape
Out[154]:
In [155]:
x + a
Out[155]:
In [156]:
x + b
Out[156]:
In [158]:
x + c[:, np.newaxis]
Out[158]:
In [159]:
x = np.arange(1, 13)
In [160]:
x
Out[160]:
In [161]:
x[:, np.newaxis] * x[np.newaxis, :]
Out[161]:
In [162]:
x = npr.random((5, 2))
In [163]:
x
Out[163]:
In [164]:
x[:,np.newaxis,:].shape
Out[164]:
In [165]:
x[np.newaxis, :,:].shape
Out[165]:
In [167]:
np.sum((x[:,np.newaxis,:] - x[np.newaxis, :,:])**2, axis=2)
Out[167]:
In [168]:
np.set_printoptions(precision=2, suppress=True)
In [170]:
M = np.sum((x[:,np.newaxis,:] - x[np.newaxis, :,:])**2, axis=2)
In [171]:
np.save('M.npy', M)
In [172]:
np.load('M.npy')
Out[172]: