import numpy as np
Numpy
Excellent summary.
Broadcasting
= np.zeros([3,4])
a a
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
= np.array([0,2])
rows = np.array([1,2,3]).reshape(1,-1).T vec
+= vec
a[:,rows] a
array([[1., 0., 1., 0.],
[2., 0., 2., 0.],
[3., 0., 3., 0.]])
Indexing
= np.arange(1, 10).reshape(3, 3)
a a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# An example of integer array indexing.
# The returned array will have shape (3,)
0, 1, 2], [0, 1, 0]] a[[
array([1, 5, 7])
= a[range(3), [0,2,1]]
b b
array([1, 6, 8])
Row to Column
b[:, np.newaxis]
array([[1],
[6],
[8]])
None] b[...,
array([[1],
[6],
[8]])