Frequency counts for unique values in a NumPy array
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The most common way to get frequency counts for values in a NumPy array is np.unique(..., return_counts=True). It is simple, vectorized, and works for many data types. The only real complication is choosing the right tool for the array you have: np.unique is general-purpose, while np.bincount can be faster for non-negative integers.
The standard solution: np.unique
np.unique can return both the sorted unique values and the number of times each value appears.
The two returned arrays line up by position. If values[0] is 1, then counts[0] is the frequency of 1.
This is the best default because it works for integers, floats, strings, and many other array types without special handling.
Build a mapping when that is easier to read
Sometimes you want a dictionary-like representation rather than parallel arrays.
This is convenient for display, serialization, or code that expects keyed lookups. Just remember that the real counting work is still happening in NumPy.
Use np.bincount for non-negative integers
If your data is already a one-dimensional array of non-negative integers, np.bincount is often faster and simpler.
The output index is the value itself. So counts[3] tells you how many times 3 appears.
The tradeoff is that np.bincount is specialized. It is not for negative numbers, strings, or general mixed arrays.
Frequency counts along an axis
If you pass a multidimensional array to np.unique without an axis, NumPy flattens it first. That is often correct, but not always. When you want unique rows or unique columns, use the axis argument.
This counts repeated rows, not individual scalar values. That distinction matters in preprocessing pipelines and feature engineering.
Compare with pure Python tools
You can also use collections.Counter, especially if the data is already a Python list. But if the data is already in a NumPy array and you care about vectorized numeric workflows, np.unique usually fits better because it keeps the operation inside NumPy.
In short:
- use
np.uniquefor general arrays - use
np.bincountfor dense non-negative integer values - use
Countermainly when the data is not really in NumPy yet
Common Pitfalls
The most common mistake is forgetting that np.unique sorts the unique values. If you expected first-seen order, the output may look surprising.
Another issue is using np.bincount on data that contains negatives or non-integer values. That function is intentionally narrower than np.unique.
People also forget that multidimensional arrays are flattened by default. If the goal is to count unique rows, you need axis=0.
Finally, when building a dictionary from NumPy scalars, convert to Python values if you need clean JSON serialization or ordinary Python container behavior.
Summary
- Use
np.unique(arr, return_counts=True)as the default solution for frequency counts. - The returned
valuesandcountsarrays align by index. - Use
np.bincountwhen the array contains non-negative integers and you want speed. - Be explicit about
axiswhen counting rows or columns in multidimensional arrays. - Remember that
np.uniquereturns sorted unique values, not first-seen order.
Related reading
- Fuel chart smoothing algorithm
- Future prediction using time series data set with Tensorflow
- FutureWarning arrays to stack must be passed as a sequence type such as list or tuple. Support for non-sequence iterables is deprecated
- FutureWarning Conversion of the second argument of issubdtype from float to np.floating is deprecated
- from keras.backend.tensorflow_backend import set_session
- From list of integers, get number closest to a given value
- from ... import vs import .
- From conda create requirements.txt for pip3

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.