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的接口自動(dòng)化讀寫(xiě)excel文件的方法
這篇文章主要介紹了基于Python的接口自動(dòng)化讀寫(xiě)excel文件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01python使用PIL模塊實(shí)現(xiàn)給圖片打水印的方法
這篇文章主要介紹了python使用PIL模塊實(shí)現(xiàn)給圖片打水印的方法,涉及使用PIL模塊操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-05-05Python數(shù)據(jù)分析之使用scikit-learn構(gòu)建模型
這篇文章主要介紹了Python數(shù)據(jù)分析之使用scikit-learn構(gòu)建模型,sklearn提供了model_selection模型選擇模塊、preprocessing數(shù)據(jù)預(yù)處理模塊、decompisition特征分解模塊,更多相關(guān)內(nèi)容需要朋友可以參考下面文章內(nèi)容2022-08-08Python 實(shí)現(xiàn)一個(gè)計(jì)時(shí)器
這篇文章主要介紹了Python 實(shí)現(xiàn)一個(gè)計(jì)時(shí)器的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07使用PyInstaller將Pygame庫(kù)編寫(xiě)的小游戲程序打包為exe文件及出現(xiàn)問(wèn)題解決方法
這篇文章主要介紹了使用PyInstaller將Pygame庫(kù)編寫(xiě)的小游戲程序打包為exe文件的方法,給大家介紹了通過(guò)Pyinstaller打包Pygame庫(kù)寫(xiě)的小游戲程序出現(xiàn)的問(wèn)題及解決方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09