IT

한 축을 따라 numpy 배열에서 최대 요소의 보장을 얻는 방법

lottoking 2020. 8. 7. 07:49
반응형

한 축을 따라 numpy 배열에서 최대 요소의 보장을 얻는 방법


2 차원 NumPy 배열이 있습니다. 축에 대해 최대 값을 얻는 방법을 알고 있습니다.

>>> a = array([[1,2,3],[4,3,1]])
>>> amax(a,axis=0)
array([4, 3, 3])

최대 요소의 성능은 어떻게 사용할 수 있습니까? 그래서 출력으로하고 싶습니다array([1,1,0])


>>> a.argmax(axis=0)

array([1, 1, 0])

>>> import numpy as np
>>> a = np.array([[1,2,3],[4,3,1]])
>>> i,j = np.unravel_index(a.argmax(), a.shape)
>>> a[i,j]
4

argmax()각 행에 대한 첫 번째 항목 만 반환합니다. http://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html

모양이 지정된 배열에 대해 작업을 수행해야하는 경우 다음보다 더 잘 작동합니다 unravel.

import numpy as np
a = np.array([[1,2,3], [4,3,1]])  # Can be of any shape
indices = np.where(a == a.max())

조건을 사용할 수도 있습니다.

indices = np.where(a >= 1.5)

위의 내용은 요청한 양식으로 결과를 제공합니다. 또는 다음과 같이 x, y 좌표 목록으로 변환 할 수 있습니다.

x_y_coords =  zip(indices[0], indices[1])

v = alli.max()
index = alli.argmax()
x, y = index/8, index%8

참고 URL : https://stackoverflow.com/questions/5469286/how-to-get-the-index-of-a-maximum-element-in-a-numpy-array-along-one-axis

반응형