栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用int列表进行稀疏矩阵切片

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用int列表进行稀疏矩阵切片

我想我

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]: MOut[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])*MOut[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 loopIn [190]: timeit extractor(indices, M.shape[0])*M1000 loops, best of 3: 399 µs per loop

在您的情况下,提取器矩阵的形状为(len(train_indices),347),只有

len(training_indices)
值。所以不大。

但是,如果a

matrix
太大(或至少第二维那么大),从而在矩阵乘法例程中产生一些错误,则可能会导致分段错误而没有python / numpy捕获它。

确实

matrix.sum(axis=1)
有效。尽管使用了1s的密集矩阵,但它也使用了矩阵乘法。或者
sparse.eye(347)*M
,类似的大小矩阵乘法?



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/611408.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号