合并多个 numpy.uniques 的计数

发布于 2025-01-17 18:38:43 字数 1510 浏览 2 评论 0原文

我有多个返回 numpy .unique(a, return_counts=True) 不幸的是,无法访问原始数组。我想将这些结果合并到一个具有唯一值的数组中,并合并到一个存储相应计数的数组中。 我不想使用 < 反向创建数组code>np.repeat() ,因为这些数据对于我的 RAM 来说太大了。

我还找到了Python的 collection.Counter 但由于我将结果用作 numpy 数组,因此我更愿意保留在 numpy“内”。 (除非,你会建议我这样做?)

有没有一种有效的方法来解决这个问题?

我想要这样的东西,而不使用 np.repeat() :

mmulti_unique_values = np.array([[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])
multi_unique_counts = np.array([[2,2,2,2],[1,2,3,1],[1,1,2,3],[1,2,2,1]])

values_ravel = multi_unique_values.ravel()
counts_ravel = multi_unique_counts.ravel()

np.unique(np.repeat(values_ravel,counts_ravel), return_counts=True)

> (array([1, 2, 3, 4]), array([5, 7, 9, 7]))

我可以使用 for 循环实现我想要的结果,但我正在寻找一种(更快)更快的方法!

all_unique_values, indices_ = np.unique(values_ravel, return_inverse=True)

all_unique_counts = np.zeros(all_unique_values.shape)

for count_index, unique_index in enumerate(indices_):
    all_unique_counts[unique_index] += counts_ravel[count_index]
    
(all_unique_values, all_unique_counts)
> (array([1, 2, 3, 4]), array([5., 7., 9., 7.]))

I have multiple returns of numpy.unique(a, return_counts=True) and unfortunately do not have access to the original arrays. I want to combine these results to one array with the unique values and to one storing the respective counts.
I do not want to create the arrays reversely by using np.repeat() , since these data is too big for my RAM.

I also found Python's collection.Counter but since I'm using the results as numpy-arrays, I would prefer to stay "within" numpy. (Except, you would advise me to do it?)

Is there a efficient way to solve this problem?

I want something like this, without using np.repeat():

mmulti_unique_values = np.array([[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])
multi_unique_counts = np.array([[2,2,2,2],[1,2,3,1],[1,1,2,3],[1,2,2,1]])

values_ravel = multi_unique_values.ravel()
counts_ravel = multi_unique_counts.ravel()

np.unique(np.repeat(values_ravel,counts_ravel), return_counts=True)

> (array([1, 2, 3, 4]), array([5, 7, 9, 7]))

I can achieve my desired result using a for-loop, but I'm looking for a (much) faster way!

all_unique_values, indices_ = np.unique(values_ravel, return_inverse=True)

all_unique_counts = np.zeros(all_unique_values.shape)

for count_index, unique_index in enumerate(indices_):
    all_unique_counts[unique_index] += counts_ravel[count_index]
    
(all_unique_values, all_unique_counts)
> (array([1, 2, 3, 4]), array([5., 7., 9., 7.]))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我喜欢麦丽素 2025-01-24 18:38:43

您只需应用np.nique即可获得具有所有唯一值的数组,并同时获得排序的数组中每个项目的位置。然后,您可以根据先前的索引积累项目数,以获取合并的项目数。

all_unique_values, index = np.unique(multi_unique_values, return_inverse=True)
all_unique_counts= np.zeros(all_unique_values.size, np.int64)
np.add.at(all_unique_counts, index, multi_unique_counts.ravel())  # inplace
all_unique_counts

You can simply apply np.unique to get the array with all the unique values and get at the same time the location for each item in the sorted array. Then you can accumulate the number of items based on the previous index so to get the merged number of item.

all_unique_values, index = np.unique(multi_unique_values, return_inverse=True)
all_unique_counts= np.zeros(all_unique_values.size, np.int64)
np.add.at(all_unique_counts, index, multi_unique_counts.ravel())  # inplace
all_unique_counts
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文