有效地为lil_matrix分配一行 [英] Efficiently assign a row to a lil_matrix
问题描述
如何有效地将行分配给lil_matrix
?我当前正在使用:
How can I efficiently assign a row to a lil_matrix
? I'm currently using:
Q[mid, :] = new_Q
其中new_Q
是lil_matrix.getrow(x)
我使用Q.getrow(i)
和Q[i, :]
进行了测试,发现前者的速度快20倍.
I ran a test on using Q.getrow(i)
vs. Q[i, :]
, and found the former to be 20x faster.
推荐答案
这些时间测试是在小型lil
上进行的(密集,但我认为这并不重要),表明x[i,:]
不是问题设置.是的,由于某种原因,它用于获取行时速度很慢.
These time tests on small lil
(dense, but I don't think that matters), suggest that x[i,:]
is not a problem setting. Yes, for some reason, it is slow when used to fetch a row.
In [108]: x=sparse.lil_matrix(np.arange(120).reshape(24,5))
In [109]: timeit x[10,:]=10
1000 loops, best of 3: 235 us per loop
In [110]: timeit y=x.getrowview(10);y[:]=10
1000 loops, best of 3: 291 us per loop
In [111]: timeit x[10,:]
1000 loops, best of 3: 658 us per loop
In [112]: timeit x.getrowview(10)
10000 loops, best of 3: 51.4 us per loop
getrowview
的来源很有启发性,显示了如何处理此矩阵的基础数据结构.
The source for getrowview
is instructive, showing how the underlying data structures for this matrix are handled.
def getrowview(self, i):
"""Returns a view of the 'i'th row (without copying).
"""
new = lil_matrix((1, self.shape[1]), dtype=self.dtype)
new.rows[0] = self.rows[i]
new.data[0] = self.data[i]
return new
我认为x[10,:]
使用x.__getitem__
或x.__setitem__
.这两个功能都比此getrowview
更复杂.我猜x.__getitem__
是很慢的,因为它也在列上建立索引(请参见x._get1
). x[10,:]
花费的时间与x[10,0:5]
一样.
I think x[10,:]
uses x.__getitem__
or x.__setitem__
. Both of those functions are more complicated than this getrowview
. I'm guessing x.__getitem__
is slow because it is also indexing on columns (see. x._get1
). x[10,:]
takes just as much time as x[10,0:5]
.
表明如果只需要设置一行并直接访问rows
和data
,可能会完成的事情:
Indicative of what might be accomplished if you only need to set one row, and access rows
and data
directly:
In [133]: timeit x.rows[10]=range(5);x.data[10]=[10]*5
1000000 loops, best of 3: 1.36 us per loop
这与一般情况相去甚远,但是可以让您了解在特殊情况下可以做什么.
This is far from general, but it gives an idea of what you can do in special cases.
更多时间:
In [156]: timeit x[10,:]=x.getrow(12)[:,:]
1000 loops, best of 3: 940 us per loop
In [157]: timeit x[10,:]=x.getrow(12)
1000 loops, best of 3: 259 us per loop
额外的[:,:]
很慢. getrow
已经返回了副本,因此可能不需要.
That extra [:,:]
is slow. getrow
already returns a copy, so it probably is not needed.
In [160]: timeit b=x.getrowview(10);b=x.getrow(12)
10000 loops, best of 3: 104 us per loop
In [169]: timeit x.rows[10],x.data[10] = x.rows[12][:],x.data[12][:]
1000000 loops, best of 3: 1.25 us per loop
rows
和data
的直接修改需要仔细测试.
Direct modification of rows
and data
needs to be tested carefully.
这篇关于有效地为lil_matrix分配一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!