2
恢復這個問題:Compute the pairwise distance in scipy with missing values如何有效地計算一系列不同長度(na內)之間的成對距離?
測試用例:我想計算不同長度taht一系列成對距離被組合在一起,我必須這樣做最有效的可行方法(使用歐氏距離)。這使得它的工作可能是這個
一個辦法:
import pandas as pd
import numpy as np
from scipy.spatial.distance import pdist
a = pd.DataFrame(np.random.rand(10, 4), columns=['a','b','c','d'])
a.loc[0, 'a'] = np.nan
a.loc[1, 'a'] = np.nan
a.loc[0, 'c'] = np.nan
a.loc[1, 'c'] = np.nan
def dropna_on_the_fly(x, y):
return np.sqrt(np.nansum(((x-y)**2)))
pdist(starting_set, dropna_on_the_fly)
,但我覺得建在pdist
功能的方法是內部優化,而功能簡單地越過這可能是非常低效的。
我有一種預感,在numpy
一個量化的解決方案,這是我broadcast
減法,然後我繼續進行np.nansum
爲na
耐總和,但我對如何進行把握。
如平常,非常感謝 – Asher11