How to Efficiently Count Unique Values in a NumPy Array?

Linda Hamilton
Release: 2024-10-26 12:31:02
Original
552 people have browsed it

How to Efficiently Count Unique Values in a NumPy Array?

Efficiently Determining Frequency Counts of Unique Values in NumPy Arrays

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!