Effectively count the number of repetitions for each number in a two-dimensional array

I need to find duplicate numbers in several one-dimensional arrays and the number of repetitions for each repetition. This is useful for np.unique one-dimensional arrays, but doesn't seem to apply to two-dimensional arrays, I have searched for similar answers, but I need a more detailed report. (Number of occurrences of all numbers, position index)

Can numpy work in two languages ​​with 2D arrays? This answer does not correspond, I hope to get a map containing more information about some data, for example, about the number of the most, and I don’t like the processing, maybe it doesn’t work, but I’ll try to find ways not to use a loop, because I have very harsh demand for speed.

For instance:

a = np.array([[1,2,2,2,3],
              [0,1,1,1,2],
              [0,0,0,1,0]])

# The number of occurrences for each number
# int  count
# 0.     0
# 1.     1
# 2.     3
# 3.     1

#need the output:
#Index = the number of statistics, the number of repetitions
[[0 1 3 1]  
 [1 3 1 0]
 [4 1 0 0]]

, , .

. , 1, , key2 1s, , , . .

from numpy_indexed import group_by

def unique2d(x):
    x = x.astype(int); mx = np.nanmax(x)+1

    ltbe = np.tile(np.arange(x.shape[0])[:,None],(1,x.shape[1]))

    vtbe = np.zeros(x.shape).astype(int) + 1

    groups = npi.group_by((ltbe.ravel(),x.ravel().astype(int)))
    unique, median = groups.sum(vtbe.ravel())

    ctbe = np.zeros(x.shape[0]*mx.astype(int)).astype(int)
    ctbe[(unique[0] * mx + unique[1]).astype(int)] = median
    ctbe.shape=(x.shape[0],mx)

    return ctbe

unique2d(a)

>array([[0, 1, 3, 1],
        [1, 3, 1, 0],
        [4, 1, 0, 0]])

, ,

+4
1

, , :

import numpy as np
import numpy_indexed as npi

a = np.array([[1,2,2,2,3],
              [0,1,1,1,2],
              [0,0,0,1,0]])

row_idx = np.indices(a.shape, dtype=np.int32)[0]
axes, table = npi.Table(row_idx.flatten(), a.flatten()).count()

havnt , - for-loops; , numpy . , , . int .

, , a ; axes; , . Table .

- ; , , numba.

0

Source: https://habr.com/ru/post/1694041/


All Articles