python距离计算公式
2017-09-09 19:09
169 查看
import math import numpy as np from math import sqrt x = np.array([1,3,2]) y = np.array([-2,1,-1]) # 相似度 def cosDis (vector1,vector2): num = 0.0 xdenom = 0.0 ydenom = 0.0 for i in range(0,len(vector1)): xdenom += vector1[i]**2 ydenom += vector2[i]**2 num += vector1[i]*vector2[i] return num/(math.sqrt(xdenom)*math.sqrt(ydenom)) a = cosDis(x,y) print(a) # def OSDistance1 (v1,v2): sq = sum((v1-v2)**2) dist = math.sqrt(sq) return dist b = OSDistance1(x,y) print(b) # def multipl(a,b): sumofab=0.0 for i in range(len(a)): temp=a[i]*b[i] sumofab+=temp return sumofab # def pearson(x,y): N=len(x) sumofx=sum(x) sumofy=sum(y) sumofxy=multipl(x,y) sumofxx=multipl(x,x) sumofyy=multipl(y,y) upside=sumofxy-sumofx*sumofy/N downside=sqrt((sumofxx-float(sumofx**2)/N)*(sumofyy-float(sumofy**2)/N)) #此处要转成浮点数保证精度 return upside/downside c = pearson(x,y) print(c)
相关文章推荐
- python函数——根据经纬度计算距离公式的错误及修正
- 百度地图2地之间的距离计算公式
- Python实现计算最小编辑距离
- python实现根据两点经纬度计算实际距离
- 基于python的空间距离权重计算——arcgis中的arcpy
- python 金融应用(一)期权定价公式的计算
- 如何运用Python计算地图上两点的实际距离
- 计算地球上A B两点间距离的计算公式
- Python计算地图上两点经纬度间的距离
- python 计算地球上两点距离和方位角(bearing)的包geographiclib
- Python Numpy计算各类距离
- 无线传输距离计算公式
- ML中相似性度量和距离的计算&及Python实现
- MachingLearning中的距离和相似性计算以及python实现
- Python基于动态规划算法计算单词距离
- 根据经纬度计算距离的公式、百度坐标转换成GPS坐标(PHP版)
- 计算两点间距离公式
- MachingLearning中的距离和相似性计算以及python实现
- c#中利用google map距离计算公式实现两点间距离的计算
- 利用python对中文文本数据进行LDA训练、计算概率距离