Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多维numpy数组中列表的频率表_Python_Performance_Numpy_Scipy - Fatal编程技术网

Python 多维numpy数组中列表的频率表

Python 多维numpy数组中列表的频率表,python,performance,numpy,scipy,Python,Performance,Numpy,Scipy,我有一些(很多)二进制编码向量,如: [0, 1, 0, 0, 1, 0] #But with many more elements each one 它们都存储在numpy(2D)数组中,如: [ [0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0], ] 我想得到每个标签集的频率表。因此,在本例中,频率表将为: [2,1] 因为第一个标签集有两个外观,而第二个标签集只有一个外观 换句话说,我想从Scipy或nump

我有一些(很多)二进制编码向量,如:

[0, 1, 0, 0, 1, 0] #But with many more elements each one
它们都存储在numpy(2D)数组中,如:

[
 [0, 1, 0, 0, 1, 0],
 [0, 0, 1, 0, 0, 1],
 [0, 1, 0, 0, 1, 0],
]
我想得到每个标签集的频率表。因此,在本例中,频率表将为:

[2,1] 
因为第一个标签集有两个外观,而第二个标签集只有一个外观

换句话说,我想从Scipy或numpy实现,但不是针对单个元素,而是针对列表

现在,我实现了以下代码:

def get_label_set_freq_table(labels):
    uniques = np.empty_like(labels)
    freq_table = np.zeros(shape=labels.shape[0])
    equal = False

    for idx,row in enumerate(labels):
        for lbl_idx,label_set in enumerate(uniques):
            if np.array_equal(row,label_set):
                equal = True
                freq_table[lbl_idx] += 1
                break
        if not equal:
            uniques[idx] = row
            freq_table[idx] += 1
        equal = False

    return freq_table
作为二进制编码向量的标签

它工作得很好,但是当向量的数量很大(>58.000)并且每个向量中的元素数量也很大(>8.000)时,它的值非常低


如何以更有效的方式完成此操作?我假设您指的是仅包含1和0的数组。对于这些,我们可以通过二进制缩放将每一行减少为一个标量,然后使用
np.unique
-

In [52]: a
Out[52]: 
array([[0, 1, 0, 0, 1, 0],
       [0, 0, 1, 0, 0, 1],
       [0, 1, 0, 0, 1, 0]])

In [53]: s = 2**np.arange(a.shape[1])

In [54]: a1D = a.dot(s)

In [55]: _, start, count = np.unique(a1D, return_index=1, return_counts=1)

In [56]: a[start]
Out[56]: 
array([[0, 1, 0, 0, 1, 0],
       [0, 0, 1, 0, 0, 1]])

In [57]: count
Out[57]: array([2, 1])
这是一个广义的-

In [33]: unq_rows, freq = np.unique(a, axis=0, return_counts=1)

In [34]: unq_rows
Out[34]: 
array([[0, 0, 1, 0, 0, 1],
       [0, 1, 0, 0, 1, 0]])

In [35]: freq
Out[35]: array([1, 2])

对我来说这一点都不热。你是对的,我将把这个问题编辑成“二进制”向量。谢谢同样@Divakar也是正确的,我忘记了轴参数。。。哇!你的解决方案很棒!如此高效,如此优雅!非常感谢你!检查和工程像一个魅力,接受答案!