How do I count how many duplicate lists there are in a python numpy array?

Asked

Viewed 36 times

1

Hey, guys, what’s up? I am new to the machine learning study and I come here to ask for a help on how I can proceed to count how many sub-lists have the same value in an array in python numpy as the example below:

All array size is 395 sub lists.

array_1 = [[0,0,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[0,1,0,1,0],[0,0,1,1,1],[0,0,0,1,1],[1,1,1,1,1],[0,1,0,1,0]]
array_2 = [[0,0,2,2,,[0,0,0,2,2],[2,2,2,2,2],[0,2,0,2,0],[0,0,2,2,2],[0,0,0,2,2],[2,2,2,2,2],[0,2,0,2,0]]

as I have to display:

column_2 countage

0,0,1,1,1 2

1,1,1,1,1 2

0,2,0,2,0 2

0,2,0,2,0 2

I tried to use that code but it didn’t work.

h = pd.DataFrame(array_1, columns=[0,1,2,3,4])
duplicateRowsDF = h.pivot_table(index=[0,1,2,3,4], aggfunc='size')
print(duplicateRowsDF)

I would like some help from colleagues because I cannot find a clear idea.

1 answer

0

Hi, maybe it can be simpler,

import collections

array = [[0,0,1,1,1],[0,0,0,1,1],
           [1,1,1,1,1],[0,1,0,1,0],
           [0,0,1,1,1],[0,0,0,1,1],
           [1,1,1,1,1],[0,1,0,1,0]]

contador = collections.Counter(array)
for i in range(len(array)): 
    print('%s: %d'% (array[i],c[tuple(array[i])])) 

you’ll get out of

[0, 0, 1, 1, 1]: 2
[0, 0, 0, 1, 1]: 2
[1, 1, 1, 1, 1]: 2
[0, 1, 0, 1, 0]: 2
[0, 0, 1, 1, 1]: 2
[0, 0, 0, 1, 1]: 2
[1, 1, 1, 1, 1]: 2
[0, 1, 0, 1, 0]: 2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.