[英]Calculating the distance between 2 sets of lat/long coordinates with Pandas and Geopy
[英]Calculating the “distance” between two unordered sets
假設有兩組(無序,無重復元素):
A = set(["z", "x", "c"])
B = set(["x", "z", "d", "e"])
這些集合有兩個共同的元素:“z”和“x”,以及一些特定於集合的元素:c,d,e。
你怎么能給每一組得分,就像字符串距離一樣
?
正如您在示例中所看到的,每個集合的大小可以不同。
此算法的非關鍵要求是:
現在我一直在計算一個設定的距離得分:
score_A = len(common(a,b)) / len(a) # common(...) calculates intersection
score_B = len(common(a,b)) / len(b)
quadratic_score = sqrt(score_A * score_B)
您會如何建議解決此問題或改進我的解決方案?
是否有任何算法可以指定成本?
現在我要為集修改定義一個簡單的代數:
def calculate_distance( a, b, insertion_cost=1, deletion_cost=1 ):
"""
Virtually, a programmer-friendly set-minus.
@return the distance from A to B, mind that this is not
a commutative operation.
"""
score = 0
for e in a:
if e not in b: # implies deletion from A
score += deletion_cost
for e in b:
if e not in a: # implies insertion into A
score += insertion_cost
return score
我如何規范化這個值並反對什么?
設置交叉點的大小與較大集合的大小有何關系? 所以:
float(len(A.intersection(B)))/max(len(A),len(B))
它會給你一個在0.0到1.0范圍內縮放的數字,這通常是可取的。 1.0表示完全相等,0.0表示沒有共同點。
與此類似的問題
假設OP要求某些東西作為“距離”,我認為根據距離函數的一般要求,當兩組相同時,最好將其設為0
並且對稱和三角不等式也是好的
對稱是直觀的, 三角不等式意味着d(A,C)≤d(A,B)+ d(B,C)
我建議像:
C = A.intersection(B)
Distance = sqrt(len(A-C)*2 + len(B-C)*2)
但是我不知道如何證明三角不等式
要標准化OP的更新函數結果,只需score = score / (len(a) + len(b))
當a
不與b
相交時會給你1,當a == b
時會給0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.