numpy에서 두 개의 2 차원 배열의 요소 별 바깥 쪽 생성을 수행하려고합니다.numpy 요소 단위 바깥 쪽
A.shape = (100, 3) # A numpy ndarray
B.shape = (100, 5) # A numpy ndarray
C = element_wise_outer_product(A, B) # A function that does the trick
C.shape = (100, 3, 5) # This should be the result
C[i] = np.outer(A[i], B[i]) # This should be the result
순진 구현은 다음과 같습니다.
tmp = []
for i in range(len(A):
outer_product = np.outer(A[i], B[i])
tmp.append(outer_product)
C = np.array(tmp)
더 나은 솔루션은 스택 오버플로에서 영감을 얻었습니다.
big_outer = np.multiply.outer(A, B)
tmp = np.swapaxes(tmp, 1, 2)
C_tmp = [tmp[i][i] for i in range(len(A)]
C = np.array(C_tmp)
for 루프를 제거하는 벡터화 된 구현을 찾고 있습니다. 누구나 아이디어가 있습니까? 감사합니다.
을 ', 형태로'100, 3, 100, 5' – Eric