NumPy bincount uses each nonnegative integer as an output position for counting

An array of small integer codes can describe repeated categories. NumPy bincount turns those codes into counts by using each code as a position in the output array. Academia Master Trader explains the function in English for readers in Italy, with constructed numerical examples. The important distinction is between the input position, the category code stored there and the output position that receives the count.

Read output positions as category labels

After import numpy as np, define x = np.array([0,2,2,4,2,0]). The call np.bincount(x) returns [2,0,3,0,1]. Output position zero contains two because zero appears twice in x. Output position two contains three because two appears three times. Output position four contains one because four appears once.

The zeros at output positions one and three are meaningful. They show that those codes do not occur in the input. NumPy has not removed those positions to produce a compact list of observed categories. The category labels are implicit in the array indices, so retaining the empty positions keeps every count attached to the correct code.

NumPy bincount

Check the length and the count total

For this nonempty input, the largest code is four and the default output has five positions, indexed zero through four. The counts sum to six, matching the input length. These are two different checks: the output length reflects the largest code, while the count total reflects the number of entries.

The figure pairs every output position with its count, including the empty categories. Reading the two rows together gives code zero with count two, code one with count zero, code two with count three, code three with count zero and code four with count one. The position of a number is part of its meaning.

Minimum length extends the available positions

The call np.bincount(x,minlength=7) returns [2,0,3,0,1,0,0]. The last two positions make codes five and six explicit, even though neither code appears in x. This can keep category arrays the same length when different groups contain different subsets of a known code range.

Minimum length is a lower bound, not an upper bound. If the input includes code eight, an output with positions through eight is needed even when minlength is seven. Decide the intended code range before allocating or comparing outputs. Widely separated integer labels can produce a long output even when the input itself is short.

Weights replace counts with sums

Define w = np.array([1,5,2,7,3,4]). The call np.bincount(x,weights=w) returns [5.,0.,10.,0.,7.]. The two entries with code zero contribute weights one and four, giving five. The three entries with code two contribute five, two and three, giving ten. Code four receives weight seven.

The weights align with input positions, not with the final output categories. There are six weights for six input entries. Each code directs its corresponding weight into the appropriate output position. Once weights are supplied, describe the result as a sum of weights for each code rather than a frequency count.

Keep the input contract explicit

The input must be one dimensional and contain nonnegative integers. Negative codes, a matrix supplied directly or floating point input do not meet that contract. Even floating point entries written as whole numbers still have a floating point type. Establish the intended categories and their integer representation before using the function.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Write down the code range, whether weights are present and the required minimum output length. Then explain the output by its positions. This approach keeps category labels, counts and weighted sums distinct when preparing a numerical summary.