Arrays are important because they allow you to perform bulk operations on data without writing loops. This is usually called vectorization (vectorization).
Any arithmetic operation between an array of equal size applies the operation to the element level:
In [Bayi]: Arr=np.array ([[[1,2],[3,4.]])
In [to]: Arr*arr
out[82]:
Array ([[ 1., 4.],
[ 9.,] ])
in [[]: Arr-arr
out[83]:
Array ([[0., 0.],
[0., 0.]])
The operation between arrays of different sizes is called broadcast (broadcasting). For example, an array and scalar arithmetic operations propagate a scalar value to each element:
in [[]: 1/arr
out[84]:
Array ([[1. , 0.5 ],
[0.33333333, 0.25 ]])
in [[]: arr**0.5
out[85]:
Array ([[1. , 1.41421356],
[1.73205081, 2. ]])
The principle of broadcasting:
If the trailing edge dimensions of the two arrays (trailing dimension, that is, the dimension starting at the end) correspond to the axis lengths or the length of one of them is 1, they are considered to be broadcast-compatible. Broadcasts are performed on a dimension that is missing and/or length 1.
The popular point is that the "broadcast dimension" of a smaller array must be 1.
Let's take a look at the broadcast on the two-dimensional array. The shape of the arr (4,3), Arr.mean (0) (Ar.mean (index) can be easily understood as flattening the arr, such as shape (m,n,l,... ) Index 1 is processed after the shape is (m,l,... ) has a shape of (3,) that conforms to the broadcast principle and runs as follows:
>>> import NumPy as NP
>>> Arr=np.arange () reshape ((4,3))
>>> arr
Array ([[0 , 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, ten, one]])
>>> arr.mean (0)
ar Ray ([4.5, 5.5, 6.5])
>>> arr.mean (0). Shape
(3,)
>>> demeaned = Arr-arr.mean (0)
>>> demeaned
Array ([[ -4.5, -4.5, -4.5],
[ -1.5, -1.5, -1.5],
[1.5, 1.5, 1.5],
[4.5, 4.5, 4.5]])
Or two-dimensional, the above example, we have 1 axis dimensionality reduction, get shape (4,) of the array, the trailing edge dimension is not 3 and not 1, so do not comply with the broadcast rules, you will get the following error:
>>> Arr.mean (1)
Array ([1., 4., 7., ten.])
>>> Arr.mean (1). Shape
(4,)
>>> Arr-arr.mean (1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Valueerror:operands could is broadcast together with shapes (4,3) (4,)
The shape (4,) is converted (4,1) and can be broadcast, as follows:
>>> Arr-arr.mean (1). Reshape ((4,1))
Array ([[ -1., 0., 1.],
[ -1., 0., 1.],
[ -1., 0., 1.],
[ -1., 0., 1.]]
Multidimensional arrays are similar to this, with three-dimensional examples. For two arrays of shape (2,3,4) and shape (2,4), we need to convert the shape (2,4) to a shape (2,1,4) to conform to the broadcast rules, and here are two methods for conversion, one with the reshape method, The second is to use the Special Np.newaxis attribute:
>>> arr= Np.arange. Reshape ((2,3,4))
>>> arr Array ([[[
0, 1, 2, 3],
[ 4, 5, 6, 7], [
8, 9, one]], [[[A], [a], [20, 21,
22, 23]]] )
>>> Arr.mean (1)
Array ([[4., 5, 6., 7.],
[+,]])
>>> arr1= Arr.mean (1). Reshape ((2,1,4))
>>> arr2= Arr.mean (1) [:, Np.newaxis,:]
>> > arr-arr1
Array ([[[ -4., -4., -4., -4.],
[0., 0., 0., 0. ],
[4 ., 4., 4., 4.]],
[[ -4., -4., -4., -4.],
[0., 0., 0., 0.],
[4., 4., 4., 4 .]]
>>> arr-arr2
Array ([[[ -4., -4., -4., -4.],
[0., 0., 0., 0. ],
[4., 4., 4., 4.]],
[[ -4., -4., -4., -4.],
[0., 0., 0., 0 .],
[4., 4., 4., 4.]]]
For multidimensional arrays, is there a method that is both generic and not sacrificing performance? Actually, there are, but some techniques for indexing are needed, and the slice class is used here. The slice accepts three parameters, namely the start index, the end index, and the step size.
>>> def demeaned_axis (arr,axis=0):
... Means=arr.mean (axis) ... indexer = [Slice (None)]*arr.ndim
... Indexer[axis] =np.newaxis ... return Arr-means[indexer]
...
>>> Demeaned_axis (arr,1) Array ([[[[
-4., -4., -4, -4.],
[0., 0., 0., 0. ],
[ 4., 4., 4., 4.], [[ -4., -4.
, -4., -4.], [0., 0., 0., 0. ],
[4., 4., 4., 4.]]]
Broadcasting also applies to operations that set array values. Arr or the previous three-dimensional array:
>>> Arr[:1] = Np.array ([0,0,0]) [:, Np.newaxis]
>>> arr
Array ([[[0, 0, 0, 0], [
0, 0, 0, 0],
[0, 0, 0, 0]], [[A, M
],
[16, ,
[20, 21, 22, 23]]]