Learning Numpy Array Chapter 2
Manipulating array shapes
The appropriately-named function, flatten()
, does the same as ravel()
, but flatten()
always allocates new memory, whereas ravel()
might return a view of an array.
a.shape
check the shape of the array a; a.shape = (m, n)
changes the array a to an (m, n) array.
The resize()
method works just like the reshape()
method but modifies the array it operates on.
Stacking arrays
hstack((a, b))
is as same as concatenate((a, b), axis=1)
, as same as column_stack((a, b))
.
vstack((a, b))
is as same as concatenate((a, b), axis=0)
, as same as ‘row_stack((a, b))’.
Splitting arrays
hsplit(a, 3)
is as same as split(a, 3, axis=1)
.
vsplit(a, 3)
is as same as split(a, 3, axis=0)
.
Fancy indexing
d = array([21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]) x = d>30 d[x] = array([31, 32, 33, 34])
Broadcasting
Because a = array([1.0,2.0,3.0]), b = 2.0, a * b
moves less memory, (b is a scalar, not an array) around during the multiplication, it is about 10% faster than a = array([1.0,2.0,3.0]), b = array([2.0,2.0,2.0]), a * b
using the standard numpy.