Home  >  Article  >  Backend Development  >  How to Efficiently Count Unique Values in a NumPy Array?

How to Efficiently Count Unique Values in a NumPy Array?

Linda Hamilton
Linda HamiltonOriginal
2024-10-26 12:31:02543browse

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>

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!

Statement:
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