Python計(jì)算序列相似度的算法實(shí)例
Python計(jì)算序列相似度的算法
代碼
位方差(location square deviation, LSD)
def location_square_deviation(lst_1, lst_2=None):
n = len(lst_1)
lst = lst_1.copy()
if lst_2 is not None:
if n != len(lst_2):
return False
for i in range(n): # 以lst2為映射表,將lst1映射為lst可直接與[0,1,2,...]比較
lst[lst_1.index(lst_2[i])] = i
s = 0
for i in range(n):
s += (lst[i]-i) ** 2
s /= n
return s位均差(location mean deviation, LMD)
def location_mean_deviation(lst_1, lst_2=None):
n = len(lst_1)
lst = lst_1.copy()
if lst_2 is not None:
if n != len(lst_2):
return False
for i in range(n):
lst[lst_1.index(lst_2[i])] = i
s = 0
for i in range(n):
s += abs(lst[i]-i)
s /= n
return s交換差(swap deviation, SD)
def swap_deviation(lst_1, lst_2=None):
n = len(lst_1)
lst = lst_1.copy()
if lst_2 is not None:
if n != len(lst_2):
return False
for i in range(n):
lst[lst_1.index(lst_2[i])] = i
count = 0 # 計(jì)算序列中的循環(huán)數(shù)
for i in range(n):
if lst[i] == -1:
continue
p = i
while lst[p] != -1:
q = lst[p]
lst[p] = -1
p = q
count += 1
return n - count # 序列長(zhǎng)減去循環(huán)數(shù)即為最小交換次數(shù)交換距離差(swap distance deviation, SDD)
def swap_distance_deviation(lst_1, lst_2=None):
n = len(lst_1)
lst = lst_1.copy()
if lst_2 is not None:
if n != len(lst_2):
return False
for i in range(n):
lst[lst_1.index(lst_2[i])] = i
swap_lst = []
weight = 0
while location_mean_deviation(lst) != 0:
r_best = 0 # 最佳交換收益
i_best = 0
j_best = 0
for i in range(n):
for j in range(i+1, n): # 遍歷所有交換,尋找最佳交換步驟
# 交換收益r=交換后位均差的下降值ΔLMD(A,B)/交換距離(j-i)
# 令交換距離恒為1可求最少交換步驟&最少交換次數(shù)
r = ((abs(lst[i]-i)+abs(lst[j]-j)) - (abs(lst[j]-i)+abs(lst[i]-j)))/(j-i)
if r > r_best:
r_best = r
i_best = i
j_best = j
lst[i_best], lst[j_best] = lst[j_best], lst[i_best]
weight += (j_best-i_best)
swap_lst.append((i_best, j_best))
# return swap_lst # 求最小交換距離的步驟(交換距離為1則是求最少交換步驟)
return weight值方差(value square deviation, VSD)
def value_square_deviation(lst_1, lst_2=None):
n = len(lst_1)
if lst_2 is not None:
if n != len(lst_2):
return False
else:
lst_2 = [i for i in range(n)]
s = 0
for i in range(n):
s += (lst_1[i] - lst_2[i]) ** 2
s /= n
return s值均差(value mean deviation, VMD)
def value_mean_deviation(lst_1, lst_2=None):
n = len(lst_1)
if lst_2 is not None:
if n != len(lst_2):
return False
else:
lst_2 = [i for i in range(n)]
s = 0
for i in range(n):
s += abs(lst_1[i] - lst_2[i])
s /= n
return s點(diǎn)積比(dot product ratio, DPR)
def dot_product_ratio(lst_1, lst_2=None):
n = len(lst_1)
if lst_2 is not None:
if n != len(lst_2):
return False
else:
lst_2 = [i for i in range(n)]
s = 0
max_s = 0
for i in range(n):
s += lst_1[i] * lst_2[i]
max_s += lst_1[i] ** 2
s /= max_s
return s歸一化點(diǎn)積比(normalization dot product ratio, NDPR)
def normalization_dot_product_ratio(lst_1, lst_2=None):
n = len(lst_1)
if lst_2 is not None:
if n != len(lst_2):
return False
else:
lst_2 = [i for i in range(n)]
s = (2*n-1)/(n+1)*dot_product_ratio(lst_1, lst_2)-(n-2)/(n+1)
return s到此這篇關(guān)于Python計(jì)算序列相似度的算法實(shí)例的文章就介紹到這了,更多相關(guān)Python序列相似度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python實(shí)現(xiàn)快速搭建本地HTTP服務(wù)器
這篇文章主要介紹了如何使用Python快速搭建本地HTTP服務(wù)器,輕松實(shí)現(xiàn)一鍵 HTTP 文件共享,同時(shí)結(jié)合二維碼技術(shù),讓訪問(wèn)更簡(jiǎn)單,感興趣的小伙伴可以了解下2025-04-04
Python實(shí)現(xiàn)自動(dòng)發(fā)消息自定義內(nèi)容的操作代碼
這篇文章主要介紹了Python實(shí)現(xiàn)自動(dòng)發(fā)消息自定義內(nèi)容的操作代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Python 實(shí)現(xiàn)輸入任意多個(gè)數(shù),并計(jì)算其平均值的例子
今天小編就為大家分享一篇Python 實(shí)現(xiàn)輸入任意多個(gè)數(shù),并計(jì)算其平均值的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
Python快速轉(zhuǎn)換numpy數(shù)組中Nan和Inf的方法實(shí)例說(shuō)明
今天小編就為大家分享一篇關(guān)于Python快速轉(zhuǎn)換numpy數(shù)組中Nan和Inf的方法實(shí)例說(shuō)明,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
python日志通過(guò)不同的等級(jí)打印不同的顏色(示例代碼)
這篇文章主要介紹了python日志通過(guò)不同的等級(jí)打印不同的顏色,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
python3中bytes和string之間的互相轉(zhuǎn)換
這篇文章主要介紹了python3中bytes和string之間的互相轉(zhuǎn)換,文中給出了詳細(xì)的介紹和示例代碼,相信對(duì)大家具有一定的參考價(jià)值,有需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-02-02

