python numpy - 高效的密集矩阵创建
我目前正在编写函数来根据运输包的输入文件生成矩阵。它是一个 1850x1850 矩阵,表示网络中的区域。这 1850 个区域还分为 10 个不同的区域组。基本上,我需要为每个组合起始区域到目标区域分配一个时间因子(数字)。
我的主要循环是:
for i,start in enumerate(starts):
#Create the matrix
mat,data = creatematrix(ID) #1850x1850
#Read in the time factors
lines = picklines(range(start+1,starts+21))
lines = [lines[i]+lines[i+1] for i in range(0,len(lines),2)]
lines = [[float(v) for v in line] for line in lines]
#Now Lines contains the time periods for each combination of origin to destination zone
#Generated the matrix
for i in xrange(10):
for j in xrange(10):
#Assign the time factor for each O-D pair
I,J = PythonMatrixGroups[i,j]
data[I,J] = lines[i,j]
#Save matrix
mat.raw_data = data
自然大部分时间都花在双循环上。这是生成此类密集矩阵的最快方法吗?我也尝试了该方法:
PQ = PythonMatrixGroups
output = np.sum([coo_matrix((lines[i,j]*ones(len(PQ[i][j][0])), PQ),shape=((1850,1850))])
mat.raw_data = output.toarray()
但它的时钟速度几乎慢了一倍
非常感谢,
I am currently writing function to generate matrices based on a input file for a transportation package. It's an 1850x1850 matrix representing the zones in a network. These 1850 zones are also partitioned into 10 different zone groups. Basically I am required to assign a Time Factor (number) to each combination origin zone to destination zone.
My main loop is:
for i,start in enumerate(starts):
#Create the matrix
mat,data = creatematrix(ID) #1850x1850
#Read in the time factors
lines = picklines(range(start+1,starts+21))
lines = [lines[i]+lines[i+1] for i in range(0,len(lines),2)]
lines = [[float(v) for v in line] for line in lines]
#Now Lines contains the time periods for each combination of origin to destination zone
#Generated the matrix
for i in xrange(10):
for j in xrange(10):
#Assign the time factor for each O-D pair
I,J = PythonMatrixGroups[i,j]
data[I,J] = lines[i,j]
#Save matrix
mat.raw_data = data
Naturally the bulk of the time is spent in the double loop. Is this the quickest way to generated a dense matrix of this sort? I also tried the method:
PQ = PythonMatrixGroups
output = np.sum([coo_matrix((lines[i,j]*ones(len(PQ[i][j][0])), PQ),shape=((1850,1850))])
mat.raw_data = output.toarray()
But it clocked almost twice as slow
Many thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该可以在一个循环中完成它,如下所示:
但我没有测试这一点。
It should be possible to do it in one loop as this :
But I did not test this.