繁体   English   中英

Scipy:稀疏矩阵条件删除列

[英]Scipy: sparse matrix conditional removal of columns

我有一个大型(79 000 x 480 000)稀疏csr矩阵。 我正在尝试删除每个值<k的所有列(在一定范围内)。

在常规的numpy矩阵中,只需通过掩码即可完成:

m = np.array([[0,2,1,1],
                [0,4,2,0],
                [0,3,4,0]])
mask = (arr < 2)
idx = mask.all(axis=0)
result = m[:, ~idx]
print result
>>> [[2 1]
     [4 2]
     [3 4]]

但是,一元按位取反运算符〜和布尔掩码功能不适用于稀疏矩阵。 最好的方法是:

  1. 获取所有值均满足条件e <k的列的索引。
  2. 根据索引列表删除这些列。

注意事项:

  1. 这些列代表ngram文本特征:矩阵中没有任何元素为零的列。

为此使用csr矩阵格式是否是合理的选择? 我是否转置并使用.nonzero()? 我有大量的工作内存(192GB),因此时间效率优于内存效率。

如果我做

M = sparse.csr_matrix(m)

M < 2

我收到效率警告; M的所有0值都满足条件,

In [1754]: print(M)
  (0, 1)    2
  (0, 2)    1
  (0, 3)    1
  (1, 1)    4
  (1, 2)    2
  (2, 1)    3
  (2, 2)    4
In [1755]: print(M<2)
/usr/lib/python3/dist-packages/scipy/sparse/compressed.py:275: SparseEfficiencyWarning: Comparing a sparse matrix with a scalar greater than zero using < is inefficient, try using >= instead.
  warn(bad_scalar_msg, SparseEfficiencyWarning)
  (0, 0)    True     # not in M
  (0, 2)    True
  (0, 3)    True
  (1, 0)    True    # not in M
  (1, 3)    True
  (2, 0)    True    # not in M
  (2, 3)    True
In [1756]: print(M>=2)   # all a subset of M
  (0, 1)    True
  (1, 1)    True
  (1, 2)    True
  (2, 1)    True
  (2, 2)    True

如果I=M>=2 ; 没有all方法,但是有一个sum

In [1760]: I.sum(axis=0)
Out[1760]: matrix([[0, 3, 2, 0]], dtype=int32)

sum实际上是使用矩阵乘法执行的

In [1769]: np.ones((1,3),int)*I
Out[1769]: array([[0, 3, 2, 0]], dtype=int32)

使用nonzero查找非零列:

In [1778]: np.nonzero(I.sum(axis=0))
Out[1778]: (array([0, 0], dtype=int32), array([1, 2], dtype=int32))
In [1779]: M[:,np.nonzero(I.sum(axis=0))[1]]
Out[1779]: 
<3x2 sparse matrix of type '<class 'numpy.int32'>'
    with 6 stored elements in Compressed Sparse Row format>
In [1780]: M[:,np.nonzero(I.sum(axis=0))[1]].A
Out[1780]: 
array([[2, 1],
       [4, 2],
       [3, 4]], dtype=int32)

一般要点:

  • 比较时要注意那些0值

  • 在稀疏矩阵上执行逻辑时要注意False值

  • 稀疏矩阵针对数学进行了优化,尤其是矩阵乘法

  • 稀疏索引的功能不及数组索引。 也不是那么快。

  • 注意操作产生密集矩阵时

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM