Skip to content

Commit 05d030a

Browse files
committed
add one distance of ml
1 parent 4c2a216 commit 05d030a

File tree

3 files changed

+133
-3
lines changed

3 files changed

+133
-3
lines changed

0-Distance/blog_ml_distance.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# coding: utf-8
2+
3+
from numpy import *
4+
5+
print '[+]------------欧式距离-----------'
6+
def twoPointDistance(a,b):
7+
d = sqrt( (a[0]-b[0])**2 + (a[1]-b[1])**2 )
8+
return d
9+
10+
print 'a,b 二维距离为:',twoPointDistance((1,1),(2,2))
11+
12+
def threePointDistance(a,b):
13+
d = sqrt( (a[0]-b[0])**2 + (a[1]-b[1])**2 + (a[2]-b[2])**2 )
14+
return d
15+
16+
print 'a,b 三维距离为:',threePointDistance((1,1,1),(2,2,2))
17+
18+
def distance(a,b):
19+
sum = 0
20+
for i in range(len(a)):
21+
sum += (a[i]-b[i])**2
22+
return sqrt(sum)
23+
24+
print 'a,b 多维距离为:',distance((1,1,2,2),(2,2,4,4))
25+
26+
print '[+]------------标准欧式距离-----------'
27+
28+
def moreBZOSdis(a,b):
29+
sumnum = 0
30+
for i in range(len(a)):
31+
# 计算si 分量标准差
32+
avg = (a[i]-b[i])/2
33+
si = sqrt( (a[i] - avg) ** 2 + (b[i] - avg) ** 2 )
34+
sumnum += ((a[i]-b[i])/si ) ** 2
35+
36+
return sqrt(sumnum)
37+
38+
print 'a,b 标准欧式距离:',moreBZOSdis((1,2,1,2),(3,3,3,4))
39+
40+
print '[+]------------曼哈顿距离-----------'
41+
def twoMHDdis(a,b):
42+
return abs(a[0]-b[0])+abs(a[1]-b[1])
43+
44+
print 'a,b 二维曼哈顿距离为:', twoMHDdis((1,1),(2,2))
45+
46+
def threeMHDdis(a,b):
47+
return abs(a[0]-b[0])+abs(a[1]-b[1]) + abs(a[2]-b[2])
48+
49+
print 'a,b 三维曼哈顿距离为:', threeMHDdis((1,1,1),(2,2,2))
50+
51+
52+
def moreMHDdis(a,b):
53+
sum = 0
54+
for i in range(len(a)):
55+
sum += abs(a[i]-b[i])
56+
return sum
57+
58+
print 'a,b 多维曼哈顿距离为:', moreMHDdis((1,1,1,1),(2,2,2,2))
59+
60+
print '[+]------------切比雪夫距离-----------'
61+
def twoQBXFdis(a,b):
62+
return max( abs(a[0]-b[0]), abs(a[1]-b[1]))
63+
64+
print 'a,b二维切比雪夫距离:' , twoQBXFdis((1,2),(3,4))
65+
66+
def moreQBXFdis(a,b):
67+
maxnum = 0
68+
for i in range(len(a)):
69+
if abs(a[i]-b[i]) > maxnum:
70+
maxnum = abs(a[i]-b[i])
71+
return maxnum
72+
73+
print 'a,b多维切比雪夫距离:' , moreQBXFdis((1,1,1,1),(3,4,3,4))
74+
75+
76+
print '[+]------------夹角余弦-----------'
77+
78+
def twoCos(a,b):
79+
cos = (a[0]*b[0]+a[1]*b[1]) / (sqrt(a[0]**2 + b[0]**2) * sqrt(a[1]**2 + b[1]**2) )
80+
81+
return cos
82+
print 'a,b 二维夹角余弦距离:',twoCos((1,1),(2,2))
83+
84+
85+
def moreCos(a,b):
86+
sum_fenzi = 0
87+
sum_fenmu = 1
88+
for i in range(len(a)):
89+
sum_fenzi += a[i]*b[i]
90+
sum_fenmu *= sqrt(a[i]**2 + b[i]**2 )
91+
92+
return sum_fenzi/sum_fenmu
93+
print 'a,b 多维夹角余弦距离:',moreCos((1,1,1,1),(2,2,2,2))
94+
95+
print '[+]------------汉明距离-----------'
96+
97+
def hanmingDis(a,b):
98+
sumnum = 0
99+
for i in range(len(a)):
100+
if a[i]!=b[i]:
101+
sumnum += 1
102+
return sumnum
103+
104+
print 'a,b 汉明距离:',hanmingDis((1,1,2,3),(2,2,1,3))
105+
106+
print '[+]------------杰卡德距离-----------'
107+
108+
def jiekadeDis(a,b):
109+
set_a = set(a)
110+
set_b = set(b)
111+
dis = float(len( (set_a | set_b) - (set_a & set_b) ) )/ len(set_a | set_b)
112+
return dis
113+
114+
print 'a,b 杰卡德距离:', jiekadeDis((1,2,3),(2,3,4))
115+
116+
def jiekadeXSDis(a,b):
117+
set_a = set(a)
118+
set_b = set(b)
119+
dis = float(len(set_a & set_b) )/ len(set_a | set_b)
120+
return dis
121+
122+
print 'a,b 杰卡德相似系数:', jiekadeXSDis((1,2,3),(2,3,4))

0-Spider/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
> 此部分我会上传一些spider的代码吧,大部分会是以目标进行分类,部分对应的会有csdn的blog,路过的大神不要嘲笑我等小白
2+
3+
4+
1: Scrapy 爬取百度贴吧指定帖子的发帖人和回帖人 <br/>
5+
http://blog.csdn.net/gamer_gyt/article/details/75043398 <br/>

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
Machine-Learning-With-Python
22
========================
3-
此项目是我在学习《机器学习实战》这本书时的代码记录情况,用python实现,当然也会包括一些其他的机器学习算法,使用Python实现<br/><br/>
3+
此项目是我在学习《机器学习实战》这本书时的代码记录情况,用python实现,当然也会包括一些其他的机器学习
4+
5+
0: 【距离计算】MachingLearning中的距离和相似性计算以及python实现:<br/>
6+
http://blog.csdn.net/gamer_gyt/article/details/75165842 <br/>
47

58
1:【关联规则】Apriori算法分析与Python代码实现,具体分析请参考博客:<br/>
6-
http://blog.csdn.net/gamer_gyt/article/details/51113753<br/>
9+
http://blog.csdn.net/gamer_gyt/article/details/51113753 <br/>
710

811
2:【关联规则】FP-Tree算法分析与Python代码实现,具体分析请参考博客:<br/>
9-
http://blog.csdn.net/gamer_gyt/article/details/51113753<br/>
12+
http://blog.csdn.net/gamer_gyt/article/details/51113753 <br/>
1013

1114
3:【决策树算法】基于信息论的三种决策树算法之ID3算法分析与Python代码实现,具体分析请参考博客:<br/>
1215
http://blog.csdn.net/gamer_gyt/article/details/51242815<br/>

0 commit comments

Comments
 (0)