program story

인덱스 목록을 사용하여 행당 특정 열 인덱스를 선택하는 NumPy

inputbox 2020. 11. 5. 07:59
반응형

인덱스 목록을 사용하여 행당 특정 열 인덱스를 선택하는 NumPy


NumPy행렬의 행당 특정 열을 선택하는 데 어려움을 겪고 있습니다.

다음과 같은 행렬이 있다고 가정합니다 X.

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

또한 list호출 할 모든 행마다 열 인덱스가 있습니다 Y.

[1, 0, 2]

값을 가져와야합니다.

[2]
[4]
[9]

listwith indexes 대신에 모든 열이 0-1 값 범위에서 / 인 Y것과 동일한 모양의 행렬을 생성하여 이것이 필수 열인지 여부를 나타낼 수도 있습니다.Xboolint

[0, 1, 0]
[1, 0, 0]
[0, 0, 1]

배열을 반복하고 필요한 열 값을 선택하면이 작업을 수행 할 수 있습니다. 그러나 이것은 대규모 데이터 배열에서 자주 실행되므로 가능한 한 빨리 실행해야합니다.

따라서 더 나은 해결책이 있는지 궁금합니다.

감사합니다.


부울 배열이있는 경우이를 기반으로 직접 선택을 수행 할 수 있습니다.

>>> a = np.array([True, True, True, False, False])
>>> b = np.array([1,2,3,4,5])
>>> b[a]
array([1, 2, 3])

초기 예제와 함께 진행하려면 다음을 수행 할 수 있습니다.

>>> a = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> b = np.array([[False,True,False],[True,False,False],[False,False,True]])
>>> a[b]
array([2, 4, 9])

arange부울 배열을 생성하는 방법과 코드가 YMMV처럼 보이는 방식에 따라을 추가 하고 직접 선택할 수도 있습니다 .

>>> a = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> a[np.arange(len(a)), [1,0,2]]
array([2, 4, 9])

도움이 되었기를 바라며 더 궁금한 점이 있으면 알려주세요.


다음과 같이 할 수 있습니다.

In [7]: a = np.array([[1, 2, 3],
   ...: [4, 5, 6],
   ...: [7, 8, 9]])

In [8]: lst = [1, 0, 2]

In [9]: a[np.arange(len(a)), lst]
Out[9]: array([2, 4, 9])

다차원 배열 색인화에 대한 추가 정보 : http://docs.scipy.org/doc/numpy/user/basics.indexing.html#indexing-multi-dimensional-arrays


간단한 방법은 다음과 같습니다.

In [1]: a = np.array([[1, 2, 3],
   ...: [4, 5, 6],
   ...: [7, 8, 9]])

In [2]: y = [1, 0, 2]  #list of indices we want to select from matrix 'a'

range(a.shape[0]) 돌아올 것이다 array([0, 1, 2])

In [3]: a[range(a.shape[0]), y] #we're selecting y indices from every row
Out[3]: array([2, 4, 9])

반복자를 사용하여 수행 할 수 있습니다. 이렇게 :

np.fromiter((row[index] for row, index in zip(X, Y)), dtype=int)

시각:

N = 1000
X = np.zeros(shape=(N, N))
Y = np.arange(N)

#@Aशwini चhaudhary
%timeit X[np.arange(len(X)), Y]
10000 loops, best of 3: 30.7 us per loop

#mine
%timeit np.fromiter((row[index] for row, index in zip(X, Y)), dtype=int)
1000 loops, best of 3: 1.15 ms per loop

#mine
%timeit np.diag(X.T[Y])
10 loops, best of 3: 20.8 ms per loop

As a pure numpythonic approach you can get the indices from second axis using np.take(), then the diagonal of the result would be your expected output:

np.diagonal(np.take(arr, idx, axis=1))

Demo:

>>> arr = np.array([[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]])
>>> 
>>> idx = [1, 0, 2]
>>> 
>>> np.diagonal(np.take(arr, idx, axis=1))
array([2, 4, 9])

Another clever way is to first transpose the array and index it thereafter. Finally, take the diagonal, its always the right answer.

X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
Y = np.array([1, 0, 2, 2])

np.diag(X.T[Y])

Step by step:

Original arrays:

>>> X
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])

>>> Y
array([1, 0, 2, 2])

Transpose to make it possible to index it right.

>>> X.T
array([[ 1,  4,  7, 10],
       [ 2,  5,  8, 11],
       [ 3,  6,  9, 12]])

Get rows in the Y order.

>>> X.T[Y]
array([[ 2,  5,  8, 11],
       [ 1,  4,  7, 10],
       [ 3,  6,  9, 12],
       [ 3,  6,  9, 12]])

The diagonal should now become clear.

>>> np.diag(X.T[Y])
array([ 2,  4,  9, 12]

참고URL : https://stackoverflow.com/questions/23435782/numpy-selecting-specific-column-index-per-row-by-using-a-list-of-indexes

반응형