[英]Finding the distance of the 5th closest point to each of the points in a list of points
[英]Finding closest points to a multidimensional data point?
我有一個數據集,它給出了一些歌曲的值,即看起來像:
acousticness danceability energy instrumentalness key liveness loudness
0 0.223 0.780 0.72 0.111 1 0.422 0.231
1 0.4 0.644 0.88 0.555 0.5 0.66 0.555
2 0.5 0.223 0.145 0.76 0 0.144 0.567
.
.
.
我想找到這些點在數字上最接近的歌曲/行。 我已經通過刪除其他高度相關的變量來減少維度,所以這似乎是最低維度。 有誰知道我該怎么做? 它可能需要機器學習算法,但我真的不知道從哪里開始。
您可以使用scipy.spital.distance.euclidean來計算兩個多維點之間的距離。
在你的情況下,例如
from scipy.spatial import distance
d = distance.euclidean([0.223, 0.780, 0.72, 0.111, 1, 0.422, 0.231], [0.4, 0.644, 0.88, 0.555, 0.5, 0.66, 0.555])
print(d)
為此,您不需要機器學習。
試試這個:會給出每行與所有其他行的距離
from scipy.spatial import distance
song_index_and_dist = []
for i in range(df.shape[0]):
for k in range(i+1,df.shape[0]):
song_index_and_dist.append([i,k,distance.euclidean(df.loc[i],df.loc[k])])
print(pd.DataFrame(song_index_and_dist))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.