【问题标题】:stacking sparse and dense matrices堆叠稀疏和密集矩阵
【发布时间】:2013-08-27 09:28:48
【问题描述】:

是否可以在 python 中堆叠稀疏和密集的 numpy 数组?我知道这可以使用 vstack/hstack 为密集的 numpy 数组完成。我有一些列想添加到稀疏矩阵中以增加特征向量的数量

【问题讨论】:

    标签: python arrays numpy matrix scipy


    【解决方案1】:

    是的,您可以使用scipy.sparse.vstackscipy.sparse.hstack,就像对密集数组使用numpy.vstacknumpy.hstack 一样。

    例子:

    from scipy.sparse import coo_matrix
    m = coo_matrix(np.array([[0,0,1],[1,0,0],[1,0,0]]))
    a = np.ones(m.shape)
    

    np.vstack:

    np.vstack((a,m))
    #ValueError: all the input array dimensions except for the concatenation axis must match exactly
    

    scipy.sparse.vstack:

    scipy.sparse.vstack((a,m))
    #<6x3 sparse matrix of type '<type 'numpy.float64'>'
    #        with 12 stored elements in COOrdinate format>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-25
      • 2019-09-02
      • 1970-01-01
      • 2014-03-06
      • 2021-02-23
      • 1970-01-01
      • 2021-01-31
      • 2014-10-19
      相关资源
      最近更新 更多