使用 int 列表的稀疏矩阵切片
Sparse matrix slicing using list of int
我正在为大量稀疏数据编写机器学习算法(我的矩阵形状为 (347, 5 416 812 801) 但非常稀疏,只有 0.13% 的数据不为零。
我的稀疏矩阵大小为 105 000 字节 (<1Mbytes) 并且是 csr
类型。
我试图通过为每个集合选择示例索引列表来分离 train/test 集合。
所以我想使用 :
将我的数据集一分为二
training_set = matrix[train_indices]
形状(len(training_indices), 5 416 812 801)
,仍然稀疏
testing_set = matrix[test_indices]
形状(347-len(training_indices), 5 416 812 801)
也稀疏
和training_indices
和testing_indices
两个list
的int
但是 training_set = matrix[train_indices]
似乎失败了 return 一个 Segmentation fault (core dumped)
这可能不是内存问题,因为我运行这段代码是在具有 64GB RAM 的服务器上运行的。
关于可能是什么原因的任何线索?
我想我已经重新创建了 csr
行索引:
def extractor(indices, N):
indptr=np.arange(len(indices)+1)
data=np.ones(len(indices))
shape=(len(indices),N)
return sparse.csr_matrix((data,indices,indptr), shape=shape)
在 csr
我闲逛的
上进行测试:
In [185]: M
Out[185]:
<30x40 sparse matrix of type '<class 'numpy.float64'>'
with 76 stored elements in Compressed Sparse Row format>
In [186]: indices=np.r_[0:20]
In [187]: M[indices,:]
Out[187]:
<20x40 sparse matrix of type '<class 'numpy.float64'>'
with 57 stored elements in Compressed Sparse Row format>
In [188]: extractor(indices, M.shape[0])*M
Out[188]:
<20x40 sparse matrix of type '<class 'numpy.float64'>'
with 57 stored elements in Compressed Sparse Row format>
与许多其他 csr
方法一样,它使用矩阵乘法来产生最终值。在这种情况下,稀疏矩阵在选定行中为 1。时间其实还好一点。
In [189]: timeit M[indices,:]
1000 loops, best of 3: 515 µs per loop
In [190]: timeit extractor(indices, M.shape[0])*M
1000 loops, best of 3: 399 µs per loop
在您的情况下,提取器矩阵的形状为 (len(training_indices),347),只有 len(training_indices)
个值。所以不大。
但是如果 matrix
太大(或者至少第二维太大)以至于它在矩阵乘法例程中产生一些错误,它可能会在没有 python/numpy 的情况下引起分段错误捕获它。
matrix.sum(axis=1)
有效吗?这也使用了矩阵乘法,尽管有一个 1s 的密集矩阵。还是sparse.eye(347)*M
,一个相似大小的矩阵乘法?
我正在为大量稀疏数据编写机器学习算法(我的矩阵形状为 (347, 5 416 812 801) 但非常稀疏,只有 0.13% 的数据不为零。
我的稀疏矩阵大小为 105 000 字节 (<1Mbytes) 并且是 csr
类型。
我试图通过为每个集合选择示例索引列表来分离 train/test 集合。 所以我想使用 :
将我的数据集一分为二training_set = matrix[train_indices]
形状(len(training_indices), 5 416 812 801)
,仍然稀疏
testing_set = matrix[test_indices]
形状(347-len(training_indices), 5 416 812 801)
也稀疏
和training_indices
和testing_indices
两个list
的int
但是 training_set = matrix[train_indices]
似乎失败了 return 一个 Segmentation fault (core dumped)
这可能不是内存问题,因为我运行这段代码是在具有 64GB RAM 的服务器上运行的。
关于可能是什么原因的任何线索?
我想我已经重新创建了 csr
行索引:
def extractor(indices, N):
indptr=np.arange(len(indices)+1)
data=np.ones(len(indices))
shape=(len(indices),N)
return sparse.csr_matrix((data,indices,indptr), shape=shape)
在 csr
我闲逛的
In [185]: M
Out[185]:
<30x40 sparse matrix of type '<class 'numpy.float64'>'
with 76 stored elements in Compressed Sparse Row format>
In [186]: indices=np.r_[0:20]
In [187]: M[indices,:]
Out[187]:
<20x40 sparse matrix of type '<class 'numpy.float64'>'
with 57 stored elements in Compressed Sparse Row format>
In [188]: extractor(indices, M.shape[0])*M
Out[188]:
<20x40 sparse matrix of type '<class 'numpy.float64'>'
with 57 stored elements in Compressed Sparse Row format>
与许多其他 csr
方法一样,它使用矩阵乘法来产生最终值。在这种情况下,稀疏矩阵在选定行中为 1。时间其实还好一点。
In [189]: timeit M[indices,:]
1000 loops, best of 3: 515 µs per loop
In [190]: timeit extractor(indices, M.shape[0])*M
1000 loops, best of 3: 399 µs per loop
在您的情况下,提取器矩阵的形状为 (len(training_indices),347),只有 len(training_indices)
个值。所以不大。
但是如果 matrix
太大(或者至少第二维太大)以至于它在矩阵乘法例程中产生一些错误,它可能会在没有 python/numpy 的情况下引起分段错误捕获它。
matrix.sum(axis=1)
有效吗?这也使用了矩阵乘法,尽管有一个 1s 的密集矩阵。还是sparse.eye(347)*M
,一个相似大小的矩阵乘法?