How to gather all the values that repeated the most in each row of the numpy array, like the result of np.unique
.
However, I want to avoid loops as the data to be handle will be much larger (much more rows).
请参见下面的示例
Input: a
is a 2D array, had the shape of (x, k)
, where x
would be very large.
a = np.asarray([[2, 7, 7, 2, 1], [1, 2, 3, 5, 5], [6, 6, 6, 6, 6]])
理想输出:[[2,7], [5], [6]]
个,第一行有[2,7]
个,两次都存在
使用循环几乎可以完成这项工作,但np.unique
似乎不适用于多维数组
[np.array(np.unique(i, return_counts=1)) for i in a]
# decent output
> [array([[1, 2, 7],[1, 2, 2]], dtype=int64),
array([[1, 2, 3, 5], [1, 1, 1, 2]], dtype=int64),
array([[6], [5]], dtype=int64)]
# Multi-dimension Input
np.unique(a, return_counts=1, axis=1)
# Useless Output
> (array([[1, 2, 2, 7, 7],
[5, 1, 5, 2, 3],
[6, 6, 6, 6, 6]]),
array([1, 1, 1, 1, 1], dtype=int64))