Using numpy
¶
In [1]:
import itertools as it
import numpy as np
Exercise: Create the following 12 by 12 multiplicaiton table using
numpy
.
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],
[ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],
[ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48],
[ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],
[ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72],
[ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84],
[ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96],
[ 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108],
[ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
[ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132],
[ 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]])
In [16]:
np.array([[x*y for x in range(1, 13)] for y in range(1,13)])
Out[16]:
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],
[ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],
[ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48],
[ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],
[ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72],
[ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84],
[ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96],
[ 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108],
[ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
[ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132],
[ 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]])
In [97]:
np.fromfunction(lambda i, j: (i+1)*(j+1), shape=(12,12), dtype='int')
Out[97]:
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],
[ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],
[ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48],
[ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],
[ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72],
[ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84],
[ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96],
[ 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108],
[ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
[ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132],
[ 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]])
In [98]:
np.outer(np.arange(1,13), np.arange(1,13))
Out[98]:
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],
[ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],
[ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48],
[ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],
[ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72],
[ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84],
[ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96],
[ 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108],
[ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
[ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132],
[ 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]])
In [99]:
np.arange(1,13)[:, None] * np.arange(1, 13)[None,:]
Out[99]:
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],
[ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],
[ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48],
[ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],
[ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72],
[ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84],
[ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96],
[ 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108],
[ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
[ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132],
[ 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]])
Reductions and the axis argument¶
In [100]:
x = np.arange(12).reshape((3,4))
In [101]:
x
Out[101]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [102]:
x.sum()
Out[102]:
66
In [103]:
x.sum(axis=0)
Out[103]:
array([12, 15, 18, 21])
In [104]:
x.sum(axis=1)
Out[104]:
array([ 6, 22, 38])
In [105]:
y = np.arange(24).reshape((2,3,4))
In [106]:
y
Out[106]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
In [107]:
y.shape
Out[107]:
(2, 3, 4)
In [108]:
y.sum(axis=0)
Out[108]:
array([[12, 14, 16, 18],
[20, 22, 24, 26],
[28, 30, 32, 34]])
In [109]:
y.sum(axis=1)
Out[109]:
array([[12, 15, 18, 21],
[48, 51, 54, 57]])
In [110]:
y.sum(axis=2)
Out[110]:
array([[ 6, 22, 38],
[54, 70, 86]])
In [111]:
y.sum(axis=(1,2))
Out[111]:
array([ 66, 210])
Splitting arrays¶
In [112]:
x
Out[112]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [113]:
x = np.arange(10)
In [114]:
x
Out[114]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [115]:
np.split(x, 5)
Out[115]:
[array([0, 1]), array([2, 3]), array([4, 5]), array([6, 7]), array([8, 9])]
In [116]:
np.split(np.lib.pad(x, (1,1), 'edge'), 3)
Out[116]:
[array([0, 0, 1, 2]), array([3, 4, 5, 6]), array([7, 8, 9, 9])]
In [117]:
y = np.arange(12).reshape((3,4))
In [118]:
y
Out[118]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [119]:
y1, y2 = np.split(y, 2, 1)
In [120]:
y1
Out[120]:
array([[0, 1],
[4, 5],
[8, 9]])
In [121]:
y1, y2, y3 = np.split(y, [1,3], 1)
In [122]:
y2
Out[122]:
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
In [123]:
y1, y2, y3 = np.split(y, 3, 0)
In [124]:
y3
Out[124]:
array([[ 8, 9, 10, 11]])
In [125]:
z = np.arange(24).reshape((2,3,4))
In [126]:
z
Out[126]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
In [127]:
z.shape
Out[127]:
(2, 3, 4)
In [128]:
for item in np.split(z, 2, axis=0):
print(item.squeeze())
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
In [129]:
for item in np.split(z, 3, axis=1):
print(item.squeeze())
[[ 0 1 2 3]
[12 13 14 15]]
[[ 4 5 6 7]
[16 17 18 19]]
[[ 8 9 10 11]
[20 21 22 23]]
In [130]:
for item in np.split(z, 4, axis=2):
print(item.squeeze())
[[ 0 4 8]
[12 16 20]]
[[ 1 5 9]
[13 17 21]]
[[ 2 6 10]
[14 18 22]]
[[ 3 7 11]
[15 19 23]]
Combining arrays¶
In [131]:
x = np.ones((2,3), 'int')
In [132]:
x
Out[132]:
array([[1, 1, 1],
[1, 1, 1]])
In [133]:
np.vstack([x, x])
Out[133]:
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
In [134]:
np.hstack([x, x])
Out[134]:
array([[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1]])
In [135]:
np.dstack([x,x])
Out[135]:
array([[[1, 1],
[1, 1],
[1, 1]],
[[1, 1],
[1, 1],
[1, 1]]])
In [136]:
np.concatenate([x, x], axis=0)
Out[136]:
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
In [137]:
np.concatenate([x, x], axis=1)
Out[137]:
array([[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1]])
In [138]:
np.r_[x, np.zeros((1,3))]
Out[138]:
array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 0., 0., 0.]])
In [139]:
np.r_['-1', x, np.zeros((2,1))]
Out[139]:
array([[ 1., 1., 1., 0.],
[ 1., 1., 1., 0.]])
In [140]:
np.r_['0', x, x]
Out[140]:
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
In [141]:
np.r_['-1', x, x]
Out[141]:
array([[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1]])
In [142]:
np.r_[1:3:5j, 4:7]
Out[142]:
array([ 1. , 1.5, 2. , 2.5, 3. , 4. , 5. , 6. ])
In [ ]: