Tim Luka
Tim Luka

Reputation: 481

How to calculate occurrence number of each item in numpy arrays

I have the following <type 'numpy.ndarray'>

array_element = [('T10', 'R1T0') ('T20', 'R2T0') ('T31', 'R3T1') ('T21', 'R2T1')
 ('T10', 'R1T0') ('T20', 'R2T0')]

I'd like to count number of the elements which are occurred in array_element in such way:

('T10', 'R1T0') is repeated twice as well as ('T20', 'R2T0') so the final output:

array_element_count = [('T10', 'R1T0', 2) ('T20', 'R2T0', 2) ('T31', 'R3T1', 1) 
('T21', 'R2T1', 1)]

For array_element is created by using numpy:

dt = np.dtype([('x', np.str_, 16), ('y', np.str_, 16)])
array_element = np.zeros((len(strs),), dtype=dt)

I have problems with calculating the occurrence number of each item which will be stored in this array:

dt = np.dtype([('x', np.str_, 16), ('y', np.str_, 16), , ('z', np.int32)])
array_element_count = np.zeros((len(strs),), dtype=dt)

Upvotes: 0

Views: 337

Answers (3)

DeepBlue
DeepBlue

Reputation: 448

You can use pandas which is fast.

import pandas as pd

array_element = [('T10', 'R1T0'), ('T20', 'R2T0'), ('T31', 'R3T1'),
                 ('T21', 'R2T1'), ('T10', 'R1T0'), ('T20', 'R2T0')]
k = pd.Index(tuple(array_element)).value_counts()
list(zip(k.index, k))

out

[(('T10', 'R1T0'), 2),
 (('T20', 'R2T0'), 2),
 (('T31', 'R3T1'), 1),
 (('T21', 'R2T1'), 1)]

or another solution with only numpy:

b = np.unique(array_element,return_counts=True, axis=0)
list(zip(zip(*b[0].T.tolist()), b[1]))

out

[(('T10', 'R1T0'), 2),
 (('T20', 'R2T0'), 2),
 (('T21', 'R2T1'), 1),
 (('T31', 'R3T1'), 1)]

Upvotes: 0

Algopark
Algopark

Reputation: 181

You can use 'unique' attribute in numpy.

array_element = np.array([('T10', 'R1T0'), ('T20', 'R2T0'), ('T31', 'R3T1'), ('T21', 'R2T1'),
 ('T10', 'R1T0'), ('T20', 'R2T0')])
uniq_array,count_array = np.unique(array_element,axis=0, return_counts=True)

Then you can get the answers.

print (uniq_array)
print (count_array)

[['T10' 'R1T0'] ['T20' 'R2T0'] ['T21' 'R2T1'] ['T31' 'R3T1']]

[2 2 1 1]

Upvotes: 2

Austin
Austin

Reputation: 26039

You can use collections.Counter approach to count occurances and later merge to existing tuples:

[k + (v,) for k, v in Counter(array_element).items()]

Example:

from collections import Counter

array_element = [('T10', 'R1T0'), ('T20', 'R2T0'), ('T31', 'R3T1'), ('T21', 'R2T1'),
 ('T10', 'R1T0'), ('T20', 'R2T0')]

print([k + (v,) for k, v in Counter(array_element).items()])
# [('T10', 'R1T0', 2) ('T20', 'R2T0', 2) ('T31', 'R3T1', 1) ('T21', 'R2T1', 1)]

Upvotes: 2

Related Questions