简体   繁体   中英

Euclidean distance between features vectors

I have a dataset such as: `

team  y            
A     African Dance    [[1.059685349464416, 0.328705966472625, 0.3115...
      Ballet           [[0.486603736877441, 1.678925514221191, 0.0157...
      Contemporary     [[0.06553386151790601, 2.121821165084839, 0, 0...

B     African Dance    [[1.129618763923645, 0.775617241859436, 0.0577...
      Ballet           [[1.164714455604553, 0.6662477850914, 0, 0.138...
      Contemporary     [[0.050464563071727, 0.856616079807281, 0, 0.3...

`

I wanna go through each row to calculate euclidean distance between all 2 pairs of array instances in a specific row.

`

 for i in range(features_vectors.size):
   for j in range(len(features_vectors[i])-1):
     fv1 = np.array(features_vectors[i][j])
     fv2 = np.array(features_vectors[i][j+1])
     print(np.linalg.norm(fv1 - fv2))

`

but I know in this way it won't see all the instances in an array because I want to calculate the distance between [0][0] and [0][1], then [0,0] and [0,2], and so on. how should I use the nested loop to see the data in this order?

This will skip duplicates:

for i,fv1 in enumerate(features_vectors):
    for fv2 in features_vectors[i+1:]:
        print(np.linalg.norm(fv1 - fv2))

You haven't really told us anything about the input data (is it one column? Several columns?), so this might need adapting.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM