【问题标题】:numpy - count equal arraysnumpy - 计算相等的数组
【发布时间】:2016-08-22 05:21:05
【问题描述】:

我想计算拆分大矩阵后遇到的相等矩阵的数量。

mat1 = np.zeros((4, 8))

split4x4 = np.split(mat1, 4)

现在我想知道 split4x4 中有多少个相等的矩阵,但是collections.Counter(split4x4) 会抛出一个错误。 numpy 中是否有内置方法来执行此操作?

【问题讨论】:

  • 我是一个业余爱好者,所以这听起来很傻,但是 np.split() 默认情况下会将数组拆分为您指定的相等部分(例如:上面示例中的 4),如果可以的话t 比它抛出一个错误。那么,为什么你需要找出那些信息,那不就是 4 个吗?

标签: python-2.7 numpy


【解决方案1】:

这可以使用numpy_indexed 包以完全矢量化的方式完成(免责声明:我是它的作者):

import numpy_indexed as npi
unique_rows, row_counts = npi.count(mat1)

这应该比使用 collections.Counter 快得多。

【讨论】:

    【解决方案2】:

    也许最简单的方法是使用np.unique 并展平拆分数组以将它们作为元组进行比较:

    import numpy as np
    # Generate some sample data:
    a = np.random.uniform(size=(8,3))
    # With repetition:
    a = np.r_[a,a]
    # Split a in 4 arrays
    s = np.asarray(np.split(a, 4))
    s = [tuple(e.flatten()) for e in s]
    np.unique(s, return_counts=True)
    

    备注:np.uniquereturn_counts 参数在 1.9.0 版本中新增。

    另一个受that post启发的纯numpy解决方案

    # Generate some sample data:
    In: a = np.random.uniform(size=(8,3))
    # With some repetition
    In: a = r_[a,a]
    In: a.shape
    Out: (16,3)
    # Split a in 4 arrays
    In: s = np.asarray(np.split(a, 4))
    In: print s
    Out: [[[ 0.78284847  0.28883662  0.53369866]
           [ 0.48249722  0.02922249  0.0355066 ]
           [ 0.05346797  0.35640319  0.91879326]
           [ 0.1645498   0.15131476  0.1717498 ]]
    
          [[ 0.98696629  0.8102581   0.84696276]
           [ 0.12612661  0.45144896  0.34802173]
           [ 0.33667377  0.79371788  0.81511075]
          [ 0.81892789  0.41917167  0.81450135]]
    
          [[ 0.78284847  0.28883662  0.53369866]
           [ 0.48249722  0.02922249  0.0355066 ]
           [ 0.05346797  0.35640319  0.91879326]
           [ 0.1645498   0.15131476  0.1717498 ]]
    
          [[ 0.98696629  0.8102581   0.84696276]
           [ 0.12612661  0.45144896  0.34802173]
           [ 0.33667377  0.79371788  0.81511075]
           [ 0.81892789  0.41917167  0.81450135]]]
    In: s.shape
    Out: (4, 4, 3)
    # Flatten the array:
    In: s = asarray([e.flatten() for e in s])
    In: s.shape
    Out: (4, 12)
    # Sort the rows using lexsort:
    In: idx = np.lexsort(s.T)
    In: s_sorted = s[idx]
    # Create a mask to get unique rows
    In: row_mask = np.append([True],np.any(np.diff(s_sorted,axis=0),1))
    # Get unique rows:
    In: out = s_sorted[row_mask]
    # and count:
    In: for e in out:
            count = (e == s).all(axis=1).sum()
            print e.reshape(4,3), count
    Out:[[ 0.78284847  0.28883662  0.53369866]
         [ 0.48249722  0.02922249  0.0355066 ]
         [ 0.05346797  0.35640319  0.91879326]
         [ 0.1645498   0.15131476  0.1717498 ]] 2
        [[ 0.98696629  0.8102581   0.84696276]
         [ 0.12612661  0.45144896  0.34802173]
         [ 0.33667377  0.79371788  0.81511075]
         [ 0.81892789  0.41917167  0.81450135]] 2
    

    【讨论】:

    • 您在第一个示例中使用的是 python 3 吗?因为我来自a = r_[a,a]NameError: name 'r_' is not defined
    • @andandandand 不,我没有。这是我的错,我在r_ 之前忘记了np,这是一种快速构建数组的简单方法(参见:docs.scipy.org/doc/numpy/reference/generated/numpy.r_.html)。我刚刚更正了我的答案。
    猜你喜欢
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2021-07-13
    • 2012-09-18
    • 2020-11-26
    • 1970-01-01
    相关资源
    最近更新 更多