python array, list of numpy.array + and + - * /

Previously thought that adding two array corresponding to the sum Example: [2,3] + [1,2,3] = [2,4,6]

I now find it and numpy.array confused.

For normal python array or list of structures: and extend + is a meaning.

a = [1,2,3] b = [1,2,3] ab, a * b, a / b can not be operational. n * a can:

In contrast, np.array closer math, np.array multiply like dot

The above is equal to the dimension of the case, unequal dimension, np, array using the broadcast mechanism may further calculates

Broadcast principle: if the two dimensions of the trailing edge of the array (trailing dimension, i.e. from the end of the counting dimension) matching the length of the shaft, wherein the one or length is 1, they are considered compatible broadcast. Deletions will be performed on the broadcast and (or) dimension of length 1. Broadcasting mainly in two cases, one is the dimension of the two arrays are not equal, but the trailing edge thereof matches the axial length dimension, the other one is a length of 1.

Different array dimensions, the axial length dimension of the trailing edge coincides

-1 3-dimensional case are to be broadcast, is extended along the 0 arr2 In this example the shaft.

image

image 

Can be seen from the above chart, (3,4,2) and dimensions (4,2) is not the same, the former is three-dimensional, which is two-dimensional. However, after they have the same edge of the axial length dimension, are (4,2), it may be broadcast along the 0 axis.

Same array dimensions, which has a shaft 1

arr1 shape of the (4,3), shape arr2 is (4,1), which are two-dimensional, but the length of a second array axis is 1, so the shaft 1 may be broadcast in the above ,As shown below:

image 

In this case, to ensure that the dimensions of the two arrays are equal, which is the length of a shaft 1, so that it will extend along the length of the shaft 1. Such examples are: (4,6) and (1,6). (3,5,6) and (1,5,6), (3,1,6), (3,5,1), respectively, will be broadcast back along three axes 0, 1 axis, two axes.

Broadcast mechanism part Reference: https://www.cnblogs.com/jiaxin359/p/9021726.html 

Text Source: "Data analysis was performed using the python" Chapter XI of broadcast 

 

Published 44 original articles · won praise 0 · Views 1895

Guess you like

Origin blog.csdn.net/weixin_39331401/article/details/104711122