LoginSignup
10
8

More than 5 years have passed since last update.

numpyでarrayの要素内の最頻値を求める

Posted at

整数値でできた1次元arrayの最頻値を求める際に少してこずってわかりにくかったのでメモ

array = [0,1,1,5]
count = np.bincount(array)  -> [1,2,0,0,0,1]
ans = np.argmax(count)  -> 1

np.bincountが上の例だと0が1個、1が2個、2~4が0個、5が1個なので上記のような結果を返してくれる。
要素が整数の場合にしか使えないのが難点か

10
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8