1

I am using python-3.x and I would like to calculate the number of duplicates in numpy array.... for example:

import numpy as np
my_array = np.array([[2, 3, 5],
                     [2, 3, 5], # duplicate of row 0 (this will be count as 1)
                     [2, 3, 5], # duplicate of row 0 (this will be count as 2)
                     [1, 0, 9], 
                     [3, 6, 6], 
                     [3, 6, 6], # duplicate of row 0 (this will be count as 3)
                     [1, 0, 9]])

What I would like to get from the outptu is the number of duplicates in this array:

the number of the duplicate is 3

most of the methods are returning the values such as collections.Counter or return_counts and they not returning what I want if I am using them right.

Any advice would be much appreciated

2 Answers 2

1

You can get the duplicate count of array by take length of array - length of unique members of array:

the_number_of the duplicate = len(my_array) - len(np.unique(my_array, axis=0))

And the result of your example is 4 ([1,0,9] is duplicate also).

Sign up to request clarification or add additional context in comments.

Comments

0

Here's a slight variation from @Anh Ngoc's answer. (for older versions of numpy where axis is not supported by np.unique)

number_of_duplicates = len(my_array) - len(set(map(tuple, my_array)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.