I would like to evaluate the euclidian distance from a fixed point to several points, I want to do it through a loop. Why is not working? I also tried without the '-1' for the range but still not working
from scipy.spatial import distance
vettore = np.array(np.mat('1 2; 3 4;6,7;8,9;10,12'))
posizione= np.array(np.mat('2,2'))
codio= []
for i in range(0,len(vettore)-1):
codio[i]=distance.euclidean(vettore[i],posizione)
codio
>>> IndexError: list assignment index out of range
How about distance_matrix
:
from scipy.spatial import distance_matrix
distance_matrix(vettore, posizione).ravel()
Output:
array([ 1. , 2.23606798, 6.40312424, 9.21954446, 12.80624847])
Use append
to add item into list
codio= []
for i in range(0,len(vettore)-1):
codio.append(distance.euclidean(vettore[i], posizione))
However a better approach is to use distance_metrics
as @Quang's answer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.