How can you find what percentage of a Numpy Array has some value X?

Say I have a numpy array:

array = np.array(['Fe', 'Pt', 'Ce', 'Nd', 'Pt', 'Fe', ..., 'Pt', 'Ce', 'Fe', 'Fe'])

Where each entry in the array is either ‘Fe’, ‘Pt’, ‘Ce’, or ‘Nd’. I understand how to get the percentage for a single entry, something like:

percentage = np.sum(array = 'Fe')/array.shape[0]*100

But what if I want to get the percentage for every unique string value? Is there a way to vectorize that operation and generalise it to an arbitrary number of unique string values?

Ideally I would like something like:

percentages = np.some_operation(array)

Which produces output like:

percentages = {'Fe': 25, 'Pt': 15, 'Nd': 45, 'Ce': 15}

It doesn’t have to be in the form of a dictionary, so long as it’s clear which percentage belongs to which element. The arrays I intend to work on could be anywhere from 1,000 to 1,000,000 entries long.

Answer

Simply use the return_counts argument from np.unique :

uniques, counts = np.unique(array, return_counts=True)

And then create a dictionnary from your 2 arrays :

percentages = dict(zip(uniques, counts * 100 / len(array)))