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

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])) #循环
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linda .

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值