In [1]:
import numpy as np
a = np.array([0, 1, 2, 3])
a
Out[1]:
In [2]:
L = range(1000)
%timeit [i**2 for i in L]
In [3]:
a = np.arange(1000)
%timeit a**2
In [4]:
L = range(3)
L
Out[4]:
In [5]:
L * 2
Out[5]:
In [6]:
[i * 2 for i in L]
Out[6]:
In [7]:
a = np.arange(3)
a
Out[7]:
In [8]:
a * 2
Out[8]:
In [9]:
a = np.array([0, 1, 2, 3])
a
Out[9]:
In [10]:
a.ndim
Out[10]:
In [11]:
a.shape
Out[11]:
In [12]:
len(a)
Out[12]:
In [13]:
b = np.array([[0, 1, 2],
[3, 4, 5]]) # 2 x 3 array
b
Out[13]:
In [14]:
b.ndim
Out[14]:
In [15]:
b.shape
Out[15]:
In [16]:
len(b)
Out[16]:
In [17]:
a2 = np.array([[0, 1, 2, 3]]).T
a2
Out[17]:
In [18]:
a3 = np.array([[0], [1], [2], [3]])
a3
Out[18]:
In [19]:
a2.shape
Out[19]:
In [20]:
c = np.array([[[1,2],
[3,4]],
[[5,6],
[7,8]]])
c
Out[20]:
In [21]:
c.ndim
Out[21]:
In [22]:
c.shape
Out[22]:
In [23]:
len(c)
Out[23]:
In [24]:
a = np.arange(4)
a
Out[24]:
In [25]:
a.shape
Out[25]:
In [26]:
b = np.array([[0, 1, 2, 3]])
b
Out[26]:
In [27]:
b.shape
Out[27]:
In [28]:
c = np.array([[0], [1], [2], [3]])
c
Out[28]:
In [29]:
c.shape
Out[29]:
In [30]:
a = np.array([[0, 1, 2, 3]])
a
Out[30]:
In [31]:
a.shape
Out[31]:
In [32]:
b = a.T
b
Out[32]:
In [33]:
b.shape
Out[33]:
In [34]:
a = np.arange(10) # 0 .. n-1 (!)
a
Out[34]:
In [35]:
b = np.arange(1, 9, 2) # start, end (exclusive), step
b
Out[35]:
In [36]:
c = np.linspace(0, 1, 6) # start, end, num-points
c
Out[36]:
In [37]:
d = np.linspace(0, 1, 5, endpoint=False)
d
Out[37]:
In [38]:
a = np.ones((3, 3)) # reminder: (3, 3) is a tuple
a
Out[38]:
In [39]:
b = np.zeros((2, 2))
b
Out[39]:
In [40]:
c = np.diag([1,2,3])
c
Out[40]:
In [41]:
d = np.eye(4)
d
Out[41]:
In [42]:
a = np.array([0, 1, 2])
a
Out[42]:
In [43]:
np.tile(a, 2)
Out[43]:
In [44]:
np.tile(a, (3, 2))
Out[44]:
In [45]:
np.tile(a, (2, 1, 2))
Out[45]:
In [46]:
b = np.array([[1, 2], [3, 4]])
b
Out[46]:
In [47]:
np.tile(b, 2)
Out[47]:
In [48]:
np.tile(b, (2, 1))
Out[48]:
In [49]:
a = np.arange(20)
a
Out[49]:
In [50]:
b = np.reshape(a, (4, 5))
b
Out[50]:
In [51]:
c = a.reshape(4,5)
c
Out[51]:
In [52]:
a = np.arange(24)
a.reshape(2, 12)
Out[52]:
In [53]:
a.reshape(2, -1)
Out[53]:
In [54]:
a.reshape(-1, 12)
Out[54]:
In [55]:
c
Out[55]:
In [56]:
d = c.flatten() # return a copy
d
Out[56]:
In [57]:
d.base is None
Out[57]:
In [58]:
e = c.ravel()
e
Out[58]:
In [59]:
e.base
Out[59]:
In [60]:
a = np.arange(5)
a
Out[60]:
In [61]:
np.vstack([a * 10, a * 20])
Out[61]:
In [62]:
b = np.arange(5)[:, np.newaxis]
b
Out[62]:
In [63]:
np.hstack([b * 10, b * 20])
Out[63]:
In [64]:
a = np.array((1,2,3))
b = np.array((2,3,4))
In [65]:
a
Out[65]:
In [66]:
a.shape
Out[66]:
In [67]:
np.dstack((a,b))
Out[67]:
In [68]:
a = np.array([[1],[2],[3]])
b = np.array([[2],[3],[4]])
np.dstack((a,b))
Out[68]:
bool
Boolean (True or False) stored as a byteint8
Byte (-128 to 127)int16
Integer (-32768 to 32767)int32
Integer (-2147483648 to 2147483647)int64
Integer (-9223372036854775808 to 9223372036854775807)uint8
Unsigned integer (0 to 255)uint16
Unsigned integer (0 to 65535)uint32
Unsigned integer (0 to 4294967295)uint64
Unsigned integer (0 to 18446744073709551615)float16
Half precision float: sign bit, 5 bits exponent, 10 bits mantissafloat32
Single precision float: sign bit, 8 bits exponent, 23 bits mantissafloat64
Double precision float: sign bit, 11 bits exponent, 52 bits mantissaS
String
In [69]:
a = np.array([1, 2, 3])
a.dtype
Out[69]:
In [70]:
b = np.array([1., 2., 3.])
b.dtype
Out[70]:
In [71]:
c = np.array([1, 2, 3], dtype=np.float64)
c.dtype
Out[71]:
In [72]:
d = np.array([1+2j, 3+4j, 5+6*1j])
d.dtype
Out[72]:
In [73]:
e = np.array([True, False, False, True])
e.dtype
Out[73]:
In [74]:
f = np.array(['Bonjour', 'Hello', 'Hallo',])
f.dtype
Out[74]:
NaN
Not a NumberInf
Infinity
In [75]:
x = np.array([1, -1, 0]) / np.array([0, 0, 0])
x
Out[75]:
In [76]:
x[0]
Out[76]:
In [77]:
np.inf, np.nan
Out[77]:
In [78]:
a = np.arange(10)
a
Out[78]:
In [79]:
a[0], a[2], a[-1]
Out[79]:
In [80]:
a[::-1]
Out[80]:
In [81]:
l = [[0,0,0],[0,1,0],[0,0,2]]
In [82]:
l[1]
Out[82]:
In [83]:
l[1][1]
Out[83]:
In [84]:
a = np.diag(np.arange(3))
a
Out[84]:
In [85]:
a[1, 1]
Out[85]:
In [86]:
a[2, 1] = 10 # third line, second column
a
Out[86]:
In [87]:
a[2] = [10, 20, 30]
a
Out[87]:
In [88]:
a = np.arange(10)
a
Out[88]:
In [89]:
a[2:9:3] # [start:end:step]
Out[89]:
In [90]:
a[:4]
Out[90]:
In [91]:
a[1:3]
Out[91]:
In [92]:
a[::2]
Out[92]:
In [93]:
a[3:]
Out[93]:
In [94]:
a = np.arange(6) + (np.arange(6) * 10)[:, np.newaxis]
a
Out[94]:
In [95]:
a[0,:]
Out[95]:
In [96]:
a[:,0]
Out[96]:
In [97]:
a = np.arange(4)
a
Out[97]:
In [98]:
a.shape
Out[98]:
In [99]:
b = np.arange(4).reshape(4,1)
b
Out[99]:
In [100]:
b.shape
Out[100]:
In [101]:
c = np.arange(4)[:, np.newaxis]
c
Out[101]:
In [102]:
c.shape
Out[102]:
In [103]:
a = np.arange(10)
a
Out[103]:
In [104]:
b = a[::2]
b
Out[104]:
In [105]:
a[0] = 99
a
Out[105]:
In [106]:
b
Out[106]:
In [107]:
a = np.arange(5)
a
Out[107]:
In [108]:
b = a.copy()
b
Out[108]:
In [109]:
a[0] = 99
a
Out[109]:
In [110]:
b
Out[110]:
In [111]:
a = np.arange(20)
a
Out[111]:
In [112]:
a % 2
Out[112]:
In [113]:
idx = (a % 2) == 0
idx
Out[113]:
In [114]:
a[idx]
Out[114]:
In [115]:
a[(a % 2) == 0]
Out[115]:
In [116]:
a = np.arange(50) * 10
a
Out[116]:
In [117]:
idx = [1, 3, 4, -1, 30]
a[idx]
Out[117]:
In [118]:
a[[1,3,4,-1,30]]
Out[118]:
In [119]:
a = np.arange(6) + (np.arange(6) * 10)[:, np.newaxis]
a
Out[119]:
In [120]:
a[[0,1,2,3,4],(1,2,3,4,5)]
Out[120]:
In [121]:
a[3:, [0,2,5]]
Out[121]:
In [122]:
a = np.array([1, 2, 3, 4])
a
Out[122]:
In [123]:
a + 1
Out[123]:
In [124]:
2**a
Out[124]:
In [125]:
b = np.ones(4) + 1
b
Out[125]:
In [126]:
a - b
Out[126]:
In [127]:
a + b
Out[127]:
In [128]:
c = np.ones((3, 3))
c
Out[128]:
In [129]:
c * c # element-wise, NOT Matrix product
Out[129]:
In [130]:
c.dot(c) # matrix product
Out[130]:
In [131]:
a = np.array([1, 2, 3, 4])
b = np.array([4, 2, 2, 4])
In [132]:
a == b
Out[132]:
In [133]:
a > b
Out[133]:
In [134]:
a = np.array([1, 2, 3, 4])
b = np.array([4, 2, 2, 4])
c = np.array([1, 2, 3, 4])
In [135]:
np.array_equal(a, b)
Out[135]:
In [136]:
np.array_equal(a, c)
Out[136]:
In [137]:
a = np.arange(5)
In [138]:
np.sin(a)
Out[138]:
In [139]:
np.log(a)
Out[139]:
In [140]:
np.exp(a)
Out[140]:
In [141]:
np.log10(a)
Out[141]:
In [142]:
a = np.arange(4)
b = np.array([1, 2])
In [143]:
a
Out[143]:
In [144]:
b
Out[144]:
In [145]:
a + b
In [147]:
x = np.array([1, 2, 3, 4])
x
Out[147]:
In [148]:
np.sum(x)
Out[148]:
In [149]:
x.sum()
Out[149]:
In [150]:
x = np.array([[1, 1], [2, 2]])
x
Out[150]:
<img src="http://www.scipy-lectures.org/_images/reductions.png", style="width: 20%; margin: 0 auto 0 auto;">
In [151]:
x.sum()
Out[151]:
In [152]:
x.sum(axis=0) # columns (first dimension)
Out[152]:
In [153]:
x.sum(axis=1) # rows (second dimension)
Out[153]:
In [154]:
x = np.array([1, 3, 2])
In [155]:
x.min()
Out[155]:
In [156]:
x.max()
Out[156]:
In [157]:
x.argmin() # index of minimum
Out[157]:
In [158]:
x.argmax() # index of maximum
Out[158]:
In [159]:
np.all([True, True, False])
Out[159]:
In [160]:
np.any([True, True, False])
Out[160]:
In [161]:
a = np.zeros((100, 100), dtype=np.int)
a
Out[161]:
In [162]:
np.any(a != 0)
Out[162]:
In [163]:
np.all(a == a)
Out[163]:
In [164]:
a = np.array([1, 2, 3, 2])
b = np.array([2, 2, 3, 2])
c = np.array([6, 4, 4, 5])
In [165]:
((a <= b) & (b <= c)).all()
Out[165]:
In [166]:
x = np.array([1, 2, 3, 1])
y = np.array([[1, 2, 3], [5, 6, 1]])
In [167]:
x.mean()
Out[167]:
In [168]:
np.median(x)
Out[168]:
In [169]:
np.median(y, axis=-1) # last axis
Out[169]:
In [170]:
x.std() # full population standard dev.
Out[170]:
In [171]:
a = np.tile(np.arange(0, 40, 10), (3, 1)).T
a
Out[171]:
In [172]:
b = np.array([0, 1, 2])
b
Out[172]:
In [173]:
a + b
Out[173]:
In [174]:
a[:,0][:, np.newaxis]
Out[174]:
In [175]:
a[:,0][:, np.newaxis] + b
Out[175]:
In [176]:
a = np.ones((4, 5))
a
Out[176]:
In [177]:
a[0]
Out[177]:
In [178]:
a[0] = 2
a
Out[178]:
In [179]:
x, y = np.arange(5), np.arange(5)[:, np.newaxis]
In [180]:
x
Out[180]:
In [181]:
y
Out[181]:
In [182]:
distance = np.sqrt(x ** 2 + y ** 2)
distance
Out[182]:
In [183]:
x, y = np.ogrid[0:3, 0:5]
In [184]:
x
Out[184]:
In [185]:
y
Out[185]:
In [186]:
np.ogrid[-1:1:3j, -1:1:5j]
Out[186]:
In [187]:
x, y = np.mgrid[0:3, 0:5]
In [188]:
x
Out[188]:
In [189]:
y
Out[189]:
In [190]:
np.mgrid[-1:1:3j, -1:1:5j]
Out[190]:
In [191]:
X, Y = np.meshgrid(np.arange(3), np.arange(5))
In [192]:
X
Out[192]:
In [193]:
Y
Out[193]:
In [194]:
zip(X.ravel(), Y.ravel())
Out[194]:
In [195]:
plt.scatter(*np.vstack(np.meshgrid(np.linspace(-1,1,10), np.linspace(-2,2,10))).reshape(2,-1).tolist())
Out[195]:
In [196]:
a = np.array([[4, 3, 5], [1, 2, 1]])
a
Out[196]:
In [197]:
a[:,0]
Out[197]:
In [198]:
b = np.sort(a, axis=0)
b
Out[198]:
In [199]:
b = np.sort(a, axis=1)
b
Out[199]:
In [200]:
a = np.array([4, 3, 1, 2])
j = np.argsort(a)
j
Out[200]:
In [201]:
a[j]
Out[201]:
In [202]:
x = range(10)
x
Out[202]:
In [203]:
import math
math.exp(x)
In [205]:
math.exp(x[0])
Out[205]:
In [206]:
[math.exp(x_i) for x_i in x]
Out[206]:
In [207]:
np.exp(x)
Out[207]:
In [208]:
np.random.seed(0)
numpy.random.shuffle(x)
In [209]:
x = np.arange(10)
np.random.shuffle(x)
x
Out[209]:
numpy.random.choice(a, size=None, replace=True, p=None)
In [210]:
# same as shuffle
np.random.choice(5, 5, replace=False)
Out[210]:
In [211]:
np.random.choice(5, 3, replace=False)
Out[211]:
In [212]:
np.random.choice(5, 10)
Out[212]:
In [213]:
np.random.choice(5, 10, p=[0.1, 0, 0.3, 0.6, 0])
Out[213]:
In [214]:
x = np.random.rand(10000)
print(x[:10])
sns.distplot(x)
Out[214]:
In [215]:
np.random.rand(3,2)
Out[215]:
In [216]:
x = np.random.random_integers(-100, 100, 50)
sns.distplot(x, rug=True)
Out[216]:
In [217]:
x = np.random.randn(1000)
sns.distplot(x, rug=True)
Out[217]:
In [218]:
np.random.randn(3,4)
Out[218]:
In [219]:
np.unique([11, 11, 2, 2, 34, 34])
Out[219]:
In [220]:
a = np.array([[1, 1], [2, 3]])
np.unique(a)
Out[220]:
In [221]:
a = np.array(['a', 'b', 'b', 'c', 'a'])
index, count = np.unique(a, return_counts=True)
In [222]:
count
Out[222]:
In [223]:
index
Out[223]:
In [224]:
np.bincount([1, 1, 2, 2, 3, 3], minlength=6)
Out[224]:
In [225]:
np.histogram([1.1, 2.5, 1.8, 2.4, 0.7], bins=[0, 1, 2, 3])
Out[225]:
In [226]:
np.histogram([1, 2, 1], bins=[0, 1, 2, 3])
Out[226]:
In [227]:
np.histogram([[1, 2, 1], [1, 0, 1]], bins=[0,1,2,3])
Out[227]:
In [228]:
np.histogram(np.arange(4), bins=np.arange(5), density=True)
Out[228]: