伯爵独特的元素排明智在ndarray [英] Count unique elements row wise in an ndarray

查看:138
本文介绍了伯爵独特的元素排明智在ndarray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是扩展这个问题。除了具有独特的元素行明智的,我想有一个类似形状的阵列,让我唯一值的计数。说最初的阵列看起来像这样:

An extension to this question. In addition to having the unique elements row-wise, I want to have a similarly shaped array that gives me the count of unique values. Say the initial array looks like this:

a = np.array([[1,  2, 2, 3,  4, 5],
              [1,  2, 3, 3,  4, 5],
              [1,  2, 3, 4,  4, 5],
              [1,  2, 3, 4,  5, 5],
              [1,  2, 3, 4,  5, 6]])

我想获得这从函数的输出:

I would like to get this as the output from the function:

np.array([[1,  2, 0, 1,  1, 1],
          [1,  1, 2, 0,  1, 1],
          [1,  1, 1, 2,  0, 1],
          [1,  1, 1, 1,  2, 0],
          [1,  1, 1, 1,  1, 1]])

在numpy的v.1.9似乎有一个额外的参数 return_counts 可以在一个扁平的数组中返回计数。是否有某种方式这可以被重新构建与零原始数组维度,其中值复制?

In numpy v.1.9 there seems to be an additional argument return_counts that can return the counts in a flattened array. Is there some way this can be re-constructed into the original array dimensions with zeros where values were duplicated?

推荐答案

这个答案背后的想法是非常类似于在这里所使用的 。我添加一个唯一的虚数到每一行。因此,从不同的行没有两个数可以是相等的。因此,你可以找到一个二维数组的所有唯一值的每行的只有一个调用 np.unique

The idea behind this answer is very similar to the one used here. I'm adding a unique imaginary number to each row. Therefore, no two numbers from different rows can be equal. Thus, you can find all the unique values in a 2D array per row with just one call to np.unique.

该指数 IND ,返回时 return_index = TRUE 为您提供了每个唯一的第一次出现的位置值。

The index, ind, returned when return_index=True gives you the location of the first occurrence of each unique value.

计数 CNT ,时返回 return_counts = TRUE 给你计数。

The count, cnt, returned when return_counts=True gives you the count.

np.put(B, IND,CNT) 地方的计数在每一个独特的价值中第一次出现的位置。

np.put(b, ind, cnt) places the count in the location of the first occurence of each unique value.

这里使用的伎俩一个明显的限制是,原始数组必须有整数或浮点数DTYPE。它不能有一个复杂的DTYPE,开始时,因为由一个唯一的虚数每行乘以可能来自不同的行产生重复对

One obvious limitation of the trick used here is that the original array must have int or float dtype. It can not have a complex dtype to start with, since multiplying each row by a unique imaginary number may produce duplicate pairs from different rows.

import numpy as np

a = np.array([[1,  2, 2, 3,  4, 5],
              [1,  2, 3, 3,  4, 5],
              [1,  2, 3, 4,  4, 5],
              [1,  2, 3, 4,  5, 5],
              [1,  2, 3, 4,  5, 6]])

def count_unique_by_row(a):
    weight = 1j*np.linspace(0, a.shape[1], a.shape[0], endpoint=False)
    b = a + weight[:, np.newaxis]
    u, ind, cnt = np.unique(b, return_index=True, return_counts=True)
    b = np.zeros_like(a)
    np.put(b, ind, cnt)
    return b

收益

In [79]: count_unique_by_row(a)
Out[79]: 
array([[1, 2, 0, 1, 1, 1],
       [1, 1, 2, 0, 1, 1],
       [1, 1, 1, 2, 0, 1],
       [1, 1, 1, 1, 2, 0],
       [1, 1, 1, 1, 1, 1]])

这篇关于伯爵独特的元素排明智在ndarray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆