欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python計(jì)算序列相似度的算法實(shí)例

 更新時(shí)間:2023年07月28日 10:35:33   作者:Eyizoha  
這篇文章主要介紹了Python計(jì)算序列相似度的算法實(shí)例,求兩個(gè)序列轉(zhuǎn)換的最少交換步驟和最小交換距離,本文提供了部分實(shí)現(xiàn)代碼與解決思路,對(duì)開(kāi)發(fā)非常有幫助,需要的朋友可以參考下

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文件的方法

    這篇文章主要介紹了基于Python的接口自動(dòng)化讀寫(xiě)excel文件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • python使用PIL模塊實(shí)現(xiàn)給圖片打水印的方法

    python使用PIL模塊實(shí)現(xiàn)給圖片打水印的方法

    這篇文章主要介紹了python使用PIL模塊實(shí)現(xiàn)給圖片打水印的方法,涉及使用PIL模塊操作圖片的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Pandas直接讀取sql腳本的方法

    Pandas直接讀取sql腳本的方法

    這篇文章主要介紹了Pandas直接讀取sql腳本的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Django框架表單操作實(shí)例分析

    Django框架表單操作實(shí)例分析

    這篇文章主要介紹了Django框架表單操作,結(jié)合實(shí)例形式分析了Django框架表單數(shù)據(jù)發(fā)送、請(qǐng)求相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-11-11
  • Python數(shù)據(jù)分析之使用scikit-learn構(gòu)建模型

    Python數(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-08
  • 一文詳解如何在Python中使用Requests庫(kù)

    一文詳解如何在Python中使用Requests庫(kù)

    這篇文章主要介紹了如何在Python中使用Requests庫(kù)的相關(guān)資料,Requests庫(kù)是Python中常用的第三方庫(kù),用于簡(jiǎn)化HTTP請(qǐng)求的發(fā)送和響應(yīng)處理,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-02-02
  • Python 實(shí)現(xiàn)一個(gè)計(jì)時(shí)器

    Python 實(shí)現(xiàn)一個(gè)計(jì)時(shí)器

    這篇文章主要介紹了Python 實(shí)現(xiàn)一個(gè)計(jì)時(shí)器的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • 使用Pycharm分段執(zhí)行代碼

    使用Pycharm分段執(zhí)行代碼

    這篇文章主要介紹了使用Pycharm分段執(zhí)行代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • Python定制類你不知道的魔術(shù)方法

    Python定制類你不知道的魔術(shù)方法

    你知道什么是定制類?Python中包含很多內(nèi)置的(Built-in)函數(shù),異常,對(duì)象。分別有不同的作用,我們可以重寫(xiě)這些功能,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2022-07-07
  • 使用PyInstaller將Pygame庫(kù)編寫(xiě)的小游戲程序打包為exe文件及出現(xiàn)問(wèn)題解決方法

    使用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

最新評(píng)論