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:
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)
This approach significantly outperforms the scipy.stats.itemfreq function in terms of execution speed, as demonstrated in performance benchmarks:
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
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3