现在有向量x,元素值非负,如何用Numpy得到x中出现次数最多的元素值?
>>>x = np.array([2,3,1,1,4])
>>>count = np.bincount(x)
array([0, 2, 1, 1, 1])
>>>common_value = np.argmax(count)
1
函数np.bincount(x)返回长度为x中最大元素值+1的np.ndarray,每个元素值对应相应索引值在x中出现的次数,比如位置1上的值为2,表示1在x中出现了两次。
现在有向量x,元素值非负,如何用Numpy得到x中出现次数最多的元素值?
>>>x = np.array([2,3,1,1,4])
>>>count = np.bincount(x)
array([0, 2, 1, 1, 1])
>>>common_value = np.argmax(count)
1
函数np.bincount(x)返回长度为x中最大元素值+1的np.ndarray,每个元素值对应相应索引值在x中出现的次数,比如位置1上的值为2,表示1在x中出现了两次。