카테고리 없음

[파이썬] Numpy 다른 열에 동일한 섹션이있는 열의 특정 섹션 전환

행복을전해요 2021. 2. 22. 06:09
>>> rows, cols = A.shape
>>> row_idx = np.arange(rows*cols)
>>> col_idx = np.repeat(np.arange(rows), cols)
>>> B = np.zeros((rows*cols, rows), dtype=A.dtype)
>>> B[row_idx, col_idx] = A.ravel()
>>> B
array([[1, 0, 0],
       [2, 0, 0],
              [3, 0, 0],
                     [0, 1, 0],
                            [0, 3, 0],
                                   [0, 0, 0],
                                          [0, 0, 2],
                                                 [0, 0, 0],
                                                        [0, 0, 0]])
                                                        
-------------------
rows = A.shape[0]
C = np.zeros((A.size, rows))
ind = np.arange(0, C.size, rows).reshape(A.shape) + np.arange(rows)[:,None]
C.flat[ind] = A


출처
https://stackoverflow.com/questions/22079995