我正在計算矢量元素之間的歐氏成對距離。我使用sklearn包中的pairwise_distances函數。然而,一些元素的結果矩陣僅僅是近似對稱的:在一個例子中,應該相等的元素的值僅等於小數點後15位數。sklearn的成對距離結果意外不對稱
我意識到這一點,因爲我在假定輸入矩陣對稱的下游分析中出現錯誤。我知道我可以將價值整理起來,但是造成這種情況的原因是什麼?
這裏是我試圖計算用於成對距離向量(它是一個大熊貓數據幀的一列):
lag_measure_data[['bios_level']].values
array([[ 0.76881030949999995538490793478558771312236785888671875 ],
[ 0. ],
[ 0.67783090619999997183953155399649403989315032958984375 ],
[ 0.3228176074999999922710003374959342181682586669921875 ],
[ 0.75822395549999999087020796650904230773448944091796875 ],
[ 0.469808621599999975959605080788605846464633941650390625],
[ 0.989529862699999984698706612107343971729278564453125 ],
[ 0. ],
[ 0.5575436799999999859522858969285152852535247802734375 ],
[ 0.9756440299999999954394525047973729670047760009765625 ],
[ 0.66511863289999995085821637985645793378353118896484375 ],
[ 0.978062709200000046649847718072123825550079345703125 ],
[ 0.473957179800000016900440868994337506592273712158203125],
[ 0.82409385540000001935112550199846737086772918701171875 ],
[ 0.56548685279999999497846374651999212801456451416015625 ],
[ 0.399505730399999980928527065771049819886684417724609375],
[ 0.474232963900000026313819034839980304241180419921875 ],
[ 0.34276307189999999369689476225175894796848297119140625 ],
[ 0.9985316859999999739017084721126593649387359619140625 ],
[ 0.9063241512999999915933813099400140345096588134765625 ],
[ 0. ]])
這裏是我使用來獲得距離矩陣的命令:
d_matrix_lag = pairwise_distances(lag_measure_data[['bios_level']].values)
這裏我就不打印輸出距離矩陣,因爲它是太亂了,但作爲第一行中的例子爲第4列的值是
0.445992701999999907602756366 031826473772525787353515625
,而第4行和第一列的值是
0.4459927019999998520916051347739994525909423828125
什麼是向量提供這樣的距離? – Dmitry
因此,如果您比較已打印的數組的第1個和第4個元素,則可以重現這些結果。 pairwise_distances(0.3228176074999999922710003374959342181682586669921875,0.76881030949999995538490793478558771312236785888671875) 缺貨[700]:陣列([[0.4459927019999998520916051347739994525909423828125]]) pairwise_distances(0.76881030949999995538490793478558771312236785888671875,0.3228176074999999922710003374959342181682586669921875) 缺貨[701]:陣列([[0.445992701999999907602756366031826473772525787353515625]]) – user277194
在上述我的註釋對兩個花車進行兩次計算,同時切換兩個參數的位置,結果略有不同 – user277194