# What is the method for creating an equivalent bincounted array, and how can it be implemented?

Hey, I came across this function called “np.bincount()” which was creating an equivalent bincounted array, and I’m clueless as to how does it do, I did search for its documentation and it left me even more confused. I’m attaching a code snippet as well, which although is very simple but the functionality of it doesn’t make sense to me.

Can anyone just please explain to me either this code, or use any other code with any other functionality which is basically creating an equivalent bincounted array as well?

1 Like

Here’s an explanation of the code you’ve used:

The `np.bincount()` function takes a 1D integer array as input and returns a new array of non-negative integers representing the count of each element in the input array.

In your code, it creates a new NumPy array `C` using the following input array:

[2, 2, 2, 4, 4, 6, 8, 8, 8, 8, 8]

The resulting `C` array contains the counts of each integer from 0 to 8 since 8 is the maximum value in the input array. For example, the count of 0 is 0, the count of 1 is 0, the count of 2 is 3, and so on up to the count of 8 being 5. Here is how the output of it looks:

[0 0 3 0 2 0 1 0 5]

The `np.repeat()` function is then used which repeats each element of an input array a certain number of times, specified by a second array.

In your code, it creates a new array `A` that contains the repeated elements of `np.arange(len(C))`, which is an array of integers from 0 to the length of `C` minus 1 (in this case, 9). The output of `np.arange(len(C))` would look like this:

[0 1 2 3 4 5 6 7 8]

The second array is `C`, which specifies how many times each element of `np.arange(len(C))` should be repeated. So, for example, since the count of 2 in `C` is 3, the first three elements of `A` will be 2, and since the count of 8 in `C` is 5, the last five elements of `A` will be 8.

In summary, this code demonstrates how to use `np.bincount()` and `np.repeat()` to create a new array that repeats each element of an input array according to the count of that element in the input array. I hope this explanation helped you!