日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

距离与相似度计算

發布時間:2024/7/23 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 距离与相似度计算 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一.余弦相似度

加速計算參考這篇文章

from math import *def square_rooted(x):return round(sqrt(sum([a*a for a in x])), 3)def cosine_similarity(x,y):numerator = sum(a*b for a, b in zip(x,y))denominator = square_rooted(x)*square_rooted(y)return round(numerator/float(denominator),3)res = cosine_similarity([1, 0], [0,1]) print('==res:', res)

二.歐式距離

from math import * def euclidean_distance(x, y):return sqrt(sum(pow(a - b, 2) for a, b in zip(x, y)))res = euclidean_distance([0, 1], [1, 0]) print('res:', res)

三.曼哈頓距離

from math import *def manhattan_distance(x,y):return sum(abs(a-b) for a, b in zip(x,y)) print(manhattan_distance([1, 0], [0, 1]))

四.漢明距離

兩個等長字符串在對應位置上不同字符的數目。

def hamming_distance(s1, s2):"""Return the Hamming distance between equal-length sequences"""if len(s1) != len(s2):raise ValueError("Undefined for sequences of unequal length")return sum(a != b for a, b in zip(s1, s2))res = hamming_distance('12','13') print('res:', res)

五.切比雪夫距離

切比雪夫距離起源于國際象棋中國王的走法,國際象棋中國王每次只能往周圍的8格中走一步,那么如果要從棋盤中A格(x1,y1)走到B格(x2,y2)最少需要走幾步?你會發現最少步數總是max(|x2-x1|,|y2-y1|)步。有一種類似的一種距離度量方法叫切比雪夫距離。

def chebyshev_distance(p, q):assert len(p) == len(q)return max([abs(x - y) for x, y in zip(p, q)]) res = chebyshev_distance([0,0], [1,3]) print('res:', res)

六.蘭氏距離

def canberra_distance(p, q):n = len(p)distance = 0for i in range(n):if p[i] == 0 and q[i] == 0:distance += 0else:distance += abs(p[i] - q[i]) / (abs(p[i]) + abs(q[i]))return distanceres = canberra_distance([1,0], [0,1]) print('res:', res)

七.閔可夫斯基距離

p=2即為歐氏距離,而p=1時則為曼哈頓距離。當p取無窮時的極限情況下,可以得到切比雪夫距離:

def minkowski_distance(p, q, n):assert len(p) == len(q)return sum([abs(x - y) ** n for x, y in zip(p, q)]) ** (1. / n)res = minkowski_distance([1, 0], [0, 1], n=2.) print('res:', res)

八.編輯距離

編輯距離,又稱Levenshtein距離(萊文斯坦距離也叫做Edit Distance),是指兩個字串之間,由一個轉成另一個所需的最少編輯操作次數,如果它們的距離越大,說明它們越是不同。許可的編輯操作包括將一個字符替換成另一個字符插入一個字符刪除一個字符

方法1:調包

import Levenshtein texta = '者記聞新' textb = '浪(第' print(Levenshtein.distance(texta, textb))

方法2:動態規劃

import os import numpy as np def edit_distance(S1,S2):#S1列 S2行mat = [[0] *(len(S1)+1) for i in range(len(S2)+1)]# print('mat:', mat)for i in range(len(S2)):mat[i+1][0] = mat[i][0]+1# print('mat:', mat)for i in range(len(S1)):mat[0][i+1] = mat[0][i]+1# print('mat:\n', np.array(mat))#相等就為0 不想等加1for i in range(len(S2)):for j in range(len(S1)):if S2[i] == S1[j]:# print('S2[i]:', S2[i])mat[i + 1][j + 1] = min(mat[i][j] + 0, mat[i + 1][j]+1, mat[i][j + 1]+1)else:mat[i + 1][j + 1] = min(mat[i][j] + 1, mat[i + 1][j]+1, mat[i][j + 1]+1)# print('mat:\n', np.array(mat))dis = mat[-1][-1]print('dis:', dis)return dis # S1 = 'iva1' # S2 = 'iva' S2 = '者記聞新' S1 = '浪(第' dis = edit_distance(S1, S2) print('dis:', dis)

九.杰卡德相似度

def jaccard_sim(a, b):unions = len(set(a).union(set(b)))intersections = len(set(a).intersection(set(b)))return intersections / unionsa = ['1', '0'] b = ['1', '1', '1'] res = jaccard_sim(a, b) print('res:', res)

十.Dice距離

def dice_coefficient(a, b):"""dice coefficient 2nt/na + nb."""intersections = len(set(a).intersection(set(b)))return intersections * 2.0/(len(set(a)) + len(set(b)))res = dice_coefficient(a = [1, 0], b =[0, 1]) print('===res:',res)

?

總結

以上是生活随笔為你收集整理的距离与相似度计算的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。