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]])
[[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]])
, ,