python 2.7 - numpy 2D matrix- How to improve performance in this case? -


i came know numpy slow individual element accesses big matrix. following part of code takes 7-8 minutes run. size of matrix 3000*3000

import numpy np ................ ................ arraylength=len(coordinates) adjmatrix=np.zeros((len(angles),len(angles))) x in range(0, arraylength):     y in range(x+1, arraylength-x):         distance=distance(coordinates[x],coordinates[y)             if(distance<=radius)                 adjmatrix[x][y]=distance                 adjmatrix[y][x]=distance 

i trying construct adjacency matrix graph consists of 3000 nodes. can me in doing numpy way? or alternatives?

edit: here distance() function

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

by way passing coordinates tuples.. in p[0]=x-coordinate , p[1]= y- coordinate.

can post distance() function? if it's common function, scipy.spatial.distance.cdist can calculate distance matrix quickly:

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

edit:

you can use pdist indeed, here 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 

output:

[[ 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.        ]] 

if want mask data:

dist[dist > 3.0] = 0 print dist 

output:

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

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -