【问题标题】:Sampling few rows of a scipy sparse matrix into another将几行 scipy 稀疏矩阵采样到另一个
【发布时间】:2018-11-05 14:27:01
【问题描述】:

如何对 scipy 稀疏矩阵的一些行进行采样,并从这些采样行中形成一个新的 scipy 稀疏矩阵?

例如。如果我有一个 10 行的 scipy 稀疏矩阵 A,并且我想从 A 中创建一个具有第 1、3、4 行的新 scipy 稀疏矩阵 B,该怎么做?

【问题讨论】:

    标签: python python-3.x numpy scipy


    【解决方案1】:

    用适当的指标矩阵左乘。指标矩阵可以使用scipy.sparse.block_diag构建,也可以直接使用csr格式构建,如下图。

    >>> import numpy as np
    >>> from scipy import sparse
    >>> 
    # create example
    >>> m, n = 10, 8
    >>> subset = [1,3,4]
    >>> A = sparse.csr_matrix(np.random.randint(-10, 5, (m, n)).clip(0, None))
    >>> A.A
    array([[3, 2, 4, 0, 0, 0, 2, 0],
           [0, 0, 2, 0, 0, 0, 0, 0],
           [4, 0, 0, 0, 0, 2, 0, 0],
           [0, 0, 0, 0, 0, 0, 4, 0],
           [3, 0, 0, 0, 1, 4, 0, 0],
           [0, 0, 0, 0, 0, 0, 2, 0],
           [0, 0, 0, 4, 0, 4, 4, 0],
           [0, 2, 0, 0, 0, 3, 0, 0],
           [4, 0, 3, 3, 0, 0, 0, 2],
           [4, 0, 0, 0, 0, 2, 0, 1]], dtype=int64)
    >>>
    # build indicator matrix
    # either using block_diag ...
    >>> split_points = np.arange(len(subset)+1).repeat(np.diff(np.concatenate([[0], subset, [m-1]])))
    >>> indicator = sparse.block_diag(np.split(np.ones(len(subset), int), split_points)).T
    >>> indicator.A
    array([[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]], dtype=int64)
    >>>
    # ... or manually---this also works for non sorted non unique subset,
    # and is therefore to be preferred over block_diag
    >>> indicator = sparse.csr_matrix((np.ones(len(subset), int), subset, np.arange(len(subset)+1)), (len(subset), m))
    >>> indicator.A
    array([[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]])
    >>> 
    # apply
    >>> result = indicator@A
    >>> result.A
    array([[0, 0, 2, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 4, 0],
           [3, 0, 0, 0, 1, 4, 0, 0]], dtype=int64)
    

    【讨论】:

    • 有没有办法对行进行索引,然后将它们附加到新矩阵中?我真正想做的是创建一个新矩阵,假设原始矩阵的第 1 行 k 次,原始矩阵的第 3 行 l 次和第 4 行原始矩阵 m 次。
    • 看起来使用 vstack 是一种方法。但它的效率非常低,因为它会创建一个新矩阵来垂直堆叠每一行。
    • @Sujay_K 你的意思是缩放还是重复?在第一种情况下使用indicator = sparse.csr_matrix((your_weights, subset, np.arange(len(subset)+1)), (len(subset), m))。在第二种情况下,使用 `indicator = sparse.csr_matrix((np.ones(np.sum(your_repeats), int), np.repeat(subset, your_repeats), np.arange(np.sum(your_repeats)+1)) , (np.sum(your_repeats), m))
    • @hpaulj 很高兴知道。谢谢!
    • @Sujay_K,警告主要是为了阻止它反复循环使用。对于一次性操作,编入索引的csr 设置可能仍然比替代设置更快(例如转换为lil)。
    猜你喜欢
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 2013-11-13
    • 2017-03-26
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多