Reputation: 327
I have an array like
w = (1,3,4,5,6,2,9,2,4,2,1,3,3,6)
That is filled with repeated numbers. I want to make it look like:
w = ([[1, 5], [2, 2], [3, 1],..)
Which means I have to sort the array first and then count the repetitions of each values and then place the count beside them. So, the resulting array would be like:
w = ([value, count])
An example would be:
w = ( [1,2], [3,3], [4,2], [5,1],[6,2], [2,3],[9,1],[2,3], [4,2],[2,3],[1,2], [3,3], [3,3], [6,2])
I have already tried with "unique" but it sorts the values whereas i want the array as it is just with the counts beside every value. This was my attempt:
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)
np.asarray((unique, counts)).T
array([[1, 5],
[2, 2],
[3, 3],
[4, 6],
[5, 1]])
Upvotes: 1
Views: 113
Reputation: 74252
If I understand correctly, you want the first column in your output to be the full sequence of values in w
(including repetitions), and the second column to be the counts for each value (also repeated for repeated values in w
)?
You can do this using np.unique
by returning both the item counts and the set of 'inverse' indices that reconstruct the original array from the unique values (in the example below, uvals[idx]
would give you back w
). You can use the inverse indices to index into the count values according to wherever the corresponding unique items occur within w
:
w = np.array([1, 3, 4, 5, 6, 2, 9, 2, 4, 2, 1, 3, 3, 6])
uvals, idx, counts = np.unique(w, return_counts=True, return_inverse=True)
out = np.vstack((w, counts[idx])).T
print(out)
# [[1 2]
# [3 3]
# [4 2]
# [5 1]
# [6 2]
# [2 3]
# [9 1]
# [2 3]
# [4 2]
# [2 3]
# [1 2]
# [3 3]
# [3 3]
# [6 2]]
Upvotes: 1
Reputation: 41003
You can use scipy.stats.itemfreq
:
>>> from scipy.stats import itemfreq
>>> w = (1,3,4,5,6,2,9,2,4,2,1,3,3,6)
>>> itemfreq(w)
array([[1, 2],
[2, 3],
[3, 3],
[4, 2],
[5, 1],
[6, 2],
[9, 1]])
If you want a tuple of lists:
>>> tuple(itemfreq(w).tolist())
([1, 2], [2, 3], [3, 3], [4, 2], [5, 1], [6, 2], [9, 1])
Upvotes: 0