numpy의 브로드캐스트

2540 단어 Broadcast
broadcast에 대한 공식 설명은 다음과 같습니다.
Each universal function takes array inputs and produces array outputs by performing the core function element-wise

on the inputs. Standard broadcasting rules are applied so that inputs not sharing exactly the same shapes can still be

usefully operated on. Broadcasting can be understood by four rules:

1. All input arrays with ndim smaller than the input array of largest ndim, have 1’s prepended to their shapes.

2. The size in each dimension of the output shape is the maximum of all the input sizes in that dimension.

3. An input can be used in the calculation if its size in a particular dimension either matches the output size in that

dimension, or has value exactly 1.

4. If an input has a dimension size of 1 in its shape, the first data entry in that dimension will be used for all

calculations along that dimension. In other words, the stepping machinery of the ufunc will simply not step

along that dimension (the stride will be 0 for that dimension).

Broadcasting is used throughout NumPy to decide how to handle disparately shaped arrays; for example, all arith-

metic operations (+, -, * , ...) between ndarrays broadcast the arrays before operation. A set of arrays is called

“broadcastable” to the same shape if the above rules produce a valid result, i.e., one of the following is true:

1. The arrays all have exactly the same shape.

2. The arrays all have the same number of dimensions and the length of each dimensions is either a common length

or 1.

3. The arrays that have too few dimensions can have their shapes prepended with a dimension of length 1 to satisfy

property 2.
Example

If a.shape is (5,1), b.shape is (1,6), c.shape is (6,) and d.shape is () so that d is a scalar, then a, b, c, and d

are all broadcastable to dimension (5,6); and

• a acts like a (5,6) array where a[:,0] is broadcast to the other columns,

• b acts like a (5,6) array where b[0,:] is broadcast to the other rows,

• c acts like a (1,6) array and therefore like a (5,6) array where c[:] is broadcast to every row, and finally,


 
 
이 안에 형상에 대한 묘사는 모두 완전하지만, 때때로 우리도 이러한 정의를 볼 수 있다. a=np.zeros((2,))
print(a)
array([0.,0.0])
중괄호가 하나밖에 없음을 주의하지만, 우리는 정의한다
a = np.zeros (2,1) 때
print(a)
array([[0,],[0.]])
기본적으로 a = np.zeros ((2,)) 정의는 벡터입니다. 모양은 (2,1)와 다르기 때문에 바꾸려면 기본적으로 (1,2)로 바꿉니다!!!
numpy의 그룹 저장소는 기본적으로 C 언어와 같이 줄을 우선합니다. 따라서 벡터는 기본적으로 줄 벡터입니다. FORTRAN과 같은 열을 우선하는 방식으로 수정할 수 있습니다!

좋은 웹페이지 즐겨찾기