LoginSignup
1
2

More than 3 years have passed since last update.

numpy.histogramの使い方

Posted at

NumpyのHistgramの使い方メモ

任意のbinを指定

bins引数で任意のbinを指定することができる.

bins引数で与える数値はbinのエッジを表している。bins=[0,1,2,3]の例では、

  • 0以上1未満 [0,1)
  • 1以上2未満 [1,2)
  • 2以上3未満 [2,3)

の3のつのbinが用意される。

下記の例では[0,1)に1つ, [1,2)に2つ, [2,3)に1つ入る.

>>> np.histogram([0,1,1,2],bins=[0,1,2,3])
(array([1, 2, 1]), array([0, 1, 2, 3]))

binsの範囲外の値はカウントされない.

>>> np.histogram([0,1,1,2,4],bins=[0,1,2,3])
(array([1, 2, 1]), array([0, 1, 2, 3]))

binの範囲外に落ちた値もカウントするなら、下記のようにする

>>> np.histogram([-1,0,1,1,2,4],bins=[-sys.float_info.max,0,1,2,3,sys.float_info.max])
(array([1, 1, 2, 1, 1]), array([-1.79769313e+308,  0.0,  1.0,
        2.0,  3.0,  1.79769313e+308]))

binは単調増加であれば、等間隔である必要もない。

1
2
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
1
2