jeudi 13 juillet 2017

Generate euclidean distance matrix from two dictionaries - python

I have two dictionaries that represents Areas and Police_Dept. My goal should be to calculate the euclidean distance among each areas/Police_Dept. Starting from this two dictionaries with networkx i created a graph as shown in the code below:

This is the first dict representing areas

This is the second dict representing Police_Dept

Now I generate a graph with networkx:

G = nx.Graph()

#nodes with f=1 refers to area
i = 1 
for k in data1:
G.add_node(i, areaName=str(data0[k][0]), numberOfCrimes=str(data0[k]
[1]),D=random.randint(10,20), f=1)
i+=1
#nodes with f=0 refers to Police_dept
for k in data0:
  G.add_node(i, numberOfD=str(data0[k][1]), f=0)
  i+=1
#I simply calculate random distance but this is not the case 
distances= {}
  for i in G.nodes():
     if G.node[i]['f']==1:
       for j in G.nodes():
         if G.node[j]['f'] == 0:
            distances[i,j]=random.randint(i,j)

Is there a solution to substitute my "distances" with the euclidean distance well calculate for each pair of area-->Police_Dept node using random order(position) of areas and departments like in the real life?




Aucun commentaire:

Enregistrer un commentaire