您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 大型Numpy Scipy CSR矩阵,行式操作

5b51 2022/1/14 8:22:09 python 字数 4087 阅读 518 来源 www.jb51.cc/python

我想迭代CSR矩阵的行并将每个元素除以行的总和,类似于此处: numpy divide row by row sum 我的问题是我正在处理一个大矩阵:(96582,350138) 当从链接的帖子应用操作时,它会膨胀我的记忆,因为返回的矩阵是密集的. 所以这是我的第一次尝试: for row in counts: row = row / row.sum() 不幸的是,这根本不会影响矩阵,所以

概述

numpy divide row by row sum

我的问题是我正在处理一个大矩阵:(96582,350138)

当从链接的帖子应用操作时,它会膨胀我的记忆,因为返回的矩阵是密集的.

所以这是我的第一次尝试:

for row in counts:
    row = row / row.sum()

不幸的是,这根本不会影响矩阵,所以我想出了第二个想法来创建一个新的csr矩阵并使用vstack连接行:

from scipy import sparse
import time

start_time = curr_time = time.time()
mtx = sparse.csr_matrix((0,counts.shape[1]))
for i,row in enumerate(counts):
   prob_row = row / row.sum()
   mtx = sparse.vstack([mtx,prob_row])
   if i % 1000 == 0:
      delta_time = time.time() - curr_time
      total_time = time.time() - start_time
      curr_time = time.time()
      print('step: %i,total time: %i,delta_time: %i' % (i,total_time,delta_time))

这很好用,但经过一些迭代后,它变得越来越慢:

step: 0,total time: 0,delta_time: 0
step: 1000,total time: 1,delta_time: 1
step: 2000,total time: 5,delta_time: 4
step: 3000,total time: 12,delta_time: 6
step: 4000,total time: 23,delta_time: 11
step: 5000,total time: 38,delta_time: 14
step: 6000,total time: 55,delta_time: 17
step: 7000,total time: 88,delta_time: 32
step: 8000,total time: 136,delta_time: 47
step: 9000,total time: 190,delta_time: 53
step: 10000,total time: 250,delta_time: 59
step: 11000,total time: 315,delta_time: 65
step: 12000,total time: 386,delta_time: 70
step: 13000,total time: 462,delta_time: 76
step: 14000,total time: 543,delta_time: 81
step: 15000,total time: 630,delta_time: 86
step: 16000,total time: 722,delta_time: 92
step: 17000,total time: 820,delta_time: 97

有什么建议?知道为什么vstack越来越慢?

您只需使用@L_403_1@即可完成操作:

>>> res = counts.multiply(1 / counts.sum(1))  # multiply with inverse
>>> res.todense()
matrix([[ 0.33333333,0.,0.66666667],[ 0.,1.        ],[ 0.26666667,0.33333333,0.4       ]])

但是使用np.lib.stride_tricks.as_strided进行你想要的操作(相对高效)也很容易.这个as_strided函数还允许对数组执行更复杂的操作(如果您的情况没有方法函数).

例如,使用scipy documentation的示例csr:

>>> from scipy.sparse import csr_matrix
>>> import numpy as np
>>> row = np.array([0,1,2,2])
>>> col = np.array([0,2])
>>> data = np.array([1.,3,4,5,6])
>>> counts = csr_matrix( (data,(row,col)),shape=(3,3) )
>>> counts.todense()
matrix([[ 1.,2.],3.],[ 4.,5.,6.]])

您可以将每行除以它的总和,如下所示:

>>> row_start_stop = np.lib.stride_tricks.as_strided(counts.indptr,shape=(counts.shape[0],2),strides=2*counts.indptr.strides)
>>> for start,stop in row_start_stop:   
...    row = counts.data[start:stop]
...    row /= row.sum()
>>> counts.todense()
matrix([[ 0.33333333,0.4       ]])

总结

以上是编程之家为你收集整理的python – 大型Numpy Scipy CSR矩阵,行式操作全部内容,希望文章能够帮你解决python – 大型Numpy Scipy CSR矩阵,行式操作所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶