Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of bincount() method from numpy?

What is its purpose? I tried reading the official site but wasn't able to understand.

like image 597
loksoni Avatar asked Jul 04 '18 16:07

loksoni


2 Answers

bincount returns the count of values in each bin from 0 to the largest value in the array i.e.

np.bincount(my_list) == [count(i) for i in range(0, max(my_list))]
                     == [count(0), count(1), ..., count(max(my_list))]

e.g.

np.bincount([0, 1, 2, 3, 4, 4, 6])
>>>   array([1, 1, 1, 1, 2, 0, 1])

Note:

  • absent numbers (e.g. 5 above) return a count of 0
  • a ValueError is raised if the list contains negative numbers or NaN
like image 102
iacob Avatar answered Nov 15 '22 22:11

iacob


Here's a graphic explanation of bincount() with and without weights:

enter image description here

like image 34
JColares Avatar answered Nov 15 '22 23:11

JColares