【问题标题】:How to get columns from big sparse csc matrix如何从大稀疏 csc 矩阵中获取列
【发布时间】:2018-06-14 09:48:10
【问题描述】:

我有一个稀疏矩阵 X

<1000000x153047 sparse matrix of type '<class 'numpy.float64'>'
with 5082518 stored elements in Compressed Sparse Column format>

我有一个数组

columns_to_use 

它由矩阵 X 的 10000 个 id 列组成。我只想使用这些列并删除其他列。我尝试使用这样的代码:

X_new = X[:, columns_to_use]

它适用于小 X(10 000 行),但有 100 000 行或更多行时出现内存错误。如何在没有内存错误的情况下获取特定列?

【问题讨论】:

  • 稀疏列选择是通过矩阵乘法完成的,如stackoverflow.com/questions/39500649/… 中所述。这样的选择将创建一个新的稀疏矩阵。你能用这个大矩阵做任何其他计算吗?复印一份?

标签: python matrix scipy sparse-matrix


【解决方案1】:

我得到了这样的决定:

cols = []
for i in columns_to_use:
    cols.append(X[:,i])
X_new = hstack(cols)

它运行得足够快并且没有任何错误。这很容易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2020-05-31
    • 2020-04-19
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 2021-09-22
    相关资源
    最近更新 更多