2016-07-02 2 views
0

에 ValueError : 피연산자는 모양 (200,49000) (10,49000) (200,49000)

a=np.zeros((20,4900)) 

b=np.zeros((1,4900)) 

a+=b 

작품을 완벽하게 정상적으로과 함께 방송 할 수 없습니다. 그러나,이 :

ValueError: operands could not be broadcast together with shapes (200,49000) (10,49000) (200,49000) 

무엇 이것에 대한 이유가 될 수 있습니다

a=np.zeros((200,49000)) 

b=np.zeros((10,49000)) 

a+=b 

이 오류를 보여줍니다?

답변

3

편집 :

NumPy와는 당신이 불평등 한 차원 두 행렬을 추가 할 경우와 두 행렬 A 또는 B는 높이가 1 개 행이있는 경우에만. 이것은 방송이라고합니다. Basic linear algebra는 두 행렬이 같은 차원 (덧셈/뺄셈)을 가져야하기 때문에 유효하지 않은 행렬 연산을 시도한다고 말합니다. 그래서 Numpy는이를 방송으로 보완하려고 시도합니다.

는 두 번째 예에서 경우 B 행렬은 대신과 같이 정의 된 경우 :

b=np.zeros((1,49000)) 

오류가있을 수 없을 것입니다. 그러나 이것을 시도한 경우 :

b=np.zeros((2,49000)) 

같은 오류가 발생합니다. Numpy docs의 사례 2가 해당 상황에 적용됩니다.

General Broadcasting Rules

When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when

1.they are equal, or 
2.one of them is 1 

If these conditions are not met, a ValueError: frames are not aligned exception is thrown, indicating that the arrays have incompatible shapes. The size of the resulting array is the maximum size along each dimension of the input arrays.

+0

오타로 인해 미안하지만 방금 제 질문에 수정했습니다. 두 번째 예제에서 'a와 b'모두에 실제로 49000 개의 열이 있습니다. – muglikar

+0

Ito A가 쓰는 것처럼, 예 1은 행렬에 열을 추가하는 것입니다. Numpy는 a와 b의 형태로부터 당신의 가능한 의도를 자동으로 탐지합니다. 그것은 a의 각 열에 b를 더합니다. –

+1

@muglikar 나는 나의 대답을 편집했다 나는 그것이 당신을 돕기를 바란다 – ifma