Search code examples
pythonnumpy-ndarraycount-unique

Python count how many unique values in ndarray


Sorry, I have been trying all the strategies found on Stackoverflow for similar questions but it still didn't work for my type of ndarray. It gave errors and I don't know where to start

I have a (maybe complex) ndarray that is like this:

I have multiple of the following stacked in an array:

my_array = array([mini_array1], [mini_array2], [mini_array3] ...)

Each mini_array is like this:

mini_array1 = ([(array([[ 5.95341123, 14.6624946 ],
   [ 5.88628672, 14.70790709],
   [ 5.79873942, 14.68244049]]), 
array([[48.208864, 48.218841, 48.228818]]), 
array([[4832, 4833, 4834]], dtype=uint16))])

mini_array2 = (array([[14.08362876, 17.88082237], [14.42213575, 18.09923094]]), 
array([[48.18891 , 48.198887]]), 
array([[4830, 4831]], dtype=uint16))

mini_array3 = (array([[14.08362876, 17.88082237], [14.42213575, 18.09923094]]), 
array([[48.18891 , 48.198887]]), 
array([[4831, 4832]], dtype=uint16))

I would like to get the number(count) of unique values in the third array of all the mini_arrays.

For example, for mini_array1,mini_array2 and mini_array3, the unique values would be 4830, 4831, 4832, 4833, 4834, answer = 5.

I tried flattening the array but it still has "array" in the list, and I'm really lost here, sorry!


Solution

  • Seems like the given arrays are not really cleanly formatted, as mini_array1 is a single-element list containing one np.array of nested arrays. This sould be avoided.

    If you still want to do the operation given on your example, consider the following code :

    values = mini_array1[0][2]
    for mini_array in mini_array2, mini_array3:
        values = np.concatenate((values, mini_array[2]), axis=1)
    
    np.unique(values)
    # >>> array([4830, 4831, 4832, 4833, 4834], dtype=uint16)
    

    Yet, I would strongly advise you to remove the nested-array structure, which is often source of such mistakes.