本文介绍了numpy 2D矩阵-在这种情况下如何提高性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道对于很大的矩阵,单个元素访问的numpy速度很慢.代码的以下部分大约需要7-8分钟才能运行.矩阵的大小约为3000 * 3000

I came to know that numpy is slow for individual element accesses for a very big matrix. The following part of the code takes about 7-8 minutes to run. Size of the Matrix is about 3000*3000

import numpy as np
................
................
ArrayLength=len(Coordinates)
AdjMatrix=np.zeros((len(Angles),len(Angles)))
for x in range(0, Arraylength):
    for y in range(x+1, Arraylength-x):
        distance=Distance(Coordinates[x],Coordinates[y)
            if(distance<=radius)
                AdjMatrix[x][y]=distance
                AdjMatrix[y][x]=distance

我基本上正在尝试为包含约3000个节点的图构造一个邻接矩阵.有人可以帮我做这种麻木的方式吗?还是其他选择?

I am basically trying to construct an adjacency matrix for a graph that consists of about 3000 nodes. Can someone help me in doing this numpy way? Or any alternatives?

这是Distance()函数

Here is the Distance() function

Def Distance(p1,p2):
    distance=np.sqrt(np.square(p1[0]-p2[0])+np.square(p1[1]-p2[1]))
    return distance

顺便说一下,我将坐标作为元组传递.如p [0] = x坐标和p [1] = y坐标.

By the way I am passing coordinates as tuples.. As in p[0]=x-coordinate and p[1]= y- coordinate.

推荐答案

可以发布Distance()函数吗?如果是常用功能,scipy.spatial.distance.cdist可以非常快速地计算距离矩阵:

Can you post the Distance() function? If it's common function, scipy.spatial.distance.cdist can calculate the distance matrix very quickly:

http://docs.scipy.org/doc/scipy/reference/generation/scipy.spatial.distance.cdist.html#scipy.spatial.distance.cdist

修改:

您确实可以使用pdist,这是一个示例:

You can use pdist indeed, here is an example:

from scipy.spatial.distance import pdist, squareform
coordinates = [(0.0, 0), (1.0, 2.0), (-1.0, 0.5), (3.1, 2.1)]
dist = squareform(pdist(coordinates))
print dist

输出:

[[ 0.          2.23606798  1.11803399  3.74432905]
 [ 2.23606798  0.          2.5         2.1023796 ]
 [ 1.11803399  2.5         0.          4.40113622]
 [ 3.74432905  2.1023796   4.40113622  0.        ]]

如果要屏蔽某些数据:

dist[dist > 3.0] = 0
print dist

输出:

[[ 0.          2.23606798  1.11803399  0.        ]
 [ 2.23606798  0.          2.5         2.1023796 ]
 [ 1.11803399  2.5         0.          0.        ]
 [ 0.          2.1023796   0.          0.        ]]

这篇关于numpy 2D矩阵-在这种情况下如何提高性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 19:48