This article explores an efficient method for calculating the frequency counts of unique values within a NumPy array.
Using numpy.unique with return_counts=True (for NumPy versions 1.9 and above) allows for efficient computation of both unique values and their corresponding counts. For illustration:
<code class="python">import numpy as np x = np.array([1,1,1,2,2,2,5,25,1,1]) unique, counts = np.unique(x, return_counts=True) print(np.asarray((unique, counts)).T)</code>
This approach significantly outperforms the scipy.stats.itemfreq function in terms of execution speed, as demonstrated in performance benchmarks:
<code class="python">In [4]: x = np.random.random_integers(0,100,1e6) In [5]: %timeit unique, counts = np.unique(x, return_counts=True) 10 loops, best of 3: 31.5 ms per loop In [6]: %timeit scipy.stats.itemfreq(x) 10 loops, best of 3: 170 ms per loop</code>
The above is the detailed content of How to Efficiently Count Unique Values in a NumPy Array?. For more information, please follow other related articles on the PHP Chinese website!