当前位置: 首页 > 工具软件 > JAC > 使用案例 >

Python简单匹配相关系数SMC、JAC及L1、L2、Lr、L无穷范式、余弦相似度、jac距离&重叠程度、海明距离

傅志文
2023-12-01

1、简单匹配相关系数SMC

def smc(x,y):
    s=0
    for i in range(len(x)):
        if x[i]==y[i]:
            s+=1
    return s/len(x)
m=[0,1,0,0,1,0,0,1,1,1]
n=[0,0,0,0,1,1,0,0,1,1]
res1=smc(m,n)
res1

2、简单匹配相关系数JAC

def jac(x,y):
    s1=0
    s2=0
    for i in range(len(x)):
        if x[i]!=y[i]:
            s1+=1
        if x[i]==y[i]==1:
            s2+=1
    return s2/(s1+s2)
m=[0,1,0,0,1,0,0,1,1,1]
n=[0,0,0,0,1,1,0,0,1,1]
res2=jac(m,n)
res2

3、L1范式(曼哈顿距离)

def L1(x,y):
    d=x.copy()
    for i in range(len(x)):
        d[i]= abs(x[i]-y[i])
    return sum(d)
m=[22,1,42,10]
n=[20,0,36,8]
L1(m,n)

4、L2范式(欧式距离)

def L2(x,y):
    d=x.copy()
    for i in range(len(x)):
        d[i]=abs(x[i]-y[i])**2
    return sum(d)**0.5
m=[22,1,42,10]
n=[20,0,36,8]
L2(m,n)

5、Lr范式(闵可夫斯基距离)

def Lr(x,y,r):
    d=x.copy()
    for i in range(len(x)):
        d[i]=abs(x[i]-y[i])**r
    return sum(d)**(1/r)
m=[22,1,42,10]
n=[20,0,36,8]
Lr(m,n,3) //r=3的情况下

6、L无穷范式(切比雪夫距离)

def Lm(x,y):
    d=x.copy()
    for i in range(len(x)):
        d[i]=abs(x[i]-y[i])
    return max(d)
m=[22,1,42,10]
n=[20,0,36,8]
Lm(m,n)

7、余弦相似度

def cosS(x,y):
    d=x.copy()
    c=x.copy()
    e=x.copy()
    for i in range(len(x)):
        d[i]=x[i]*y[i]
        c[i]=x[i]**2
        e[i]=y[i]**2
    return sum(d)/(sum(c)*sum(e))
m=[3,-1,2]
n=[-2,3,1]
cosS(m,n)

8、jac距离、重叠程度

def calI(x,y):#计算交集
    count = [ ]
    for i in x:
        if i in y:
            count.append(i)
    return count

def calU(x,y):#计算并集
    count = x.copy( )
    for i in x:
        if i not in y:
            count.append(i)
    return count

def IoUd(x,y):
    I = calI(x,y)
    U = calU(x,y)
    return 1-len(I)/len(U)

m1=[1,2,3,4]
n1=[2,3,4,5]
res1=IoUd(m1,n1)
m2=[1,2,3]
n2=[4,5,6]
res2=IoUd(m2,n2)
res1,res2

9、海明距离

def haimming(x,y):
    count = 0
    for i in range(len(y)):
        if x[i]!=y[i]:
            count +=1
    return count
data=[[0,0,0,0,0,0],[1,1,0,0,1,1],[0,1,0,1,0,1],[0,1,1,1,0,0]]
for i in range(len(data)):
    for j in range(i+1,len(data)):
        print(haimming(data[i],data[j])) #循环
 类似资料: