python3 集体智慧编程第九章advancedclassify.py代码
对于python2版本的集体智慧编程第九章SVM进行了修改,该代码适用于python3版本。
以下是advancedclassify.py
import matplotlib.pyplot as plt
import numpy as npclass matchrow:def __init__(self,row,allnum=False):if allnum:self.data=[float(row[i]) for i in range(len(row)-1)]else:self.data=row[0:len(row)-1]self.match=int(row[len(row)-1])def loadmatch(f,allnum=False):rows=[]for line in open(f):rows.append(matchrow(line.split(','),allnum))return rowsfrom pylab import *
def plotagematches(rows):xdm,ydm=[r.data[0] for r in rows if r.match==1],\[r.data[1] for r in rows if r.match==1]xdn,ydn=[r.data[0] for r in rows if r.match==0],\[r.data[1] for r in rows if r.match==0] plt.plot(xdm,ydm,'bo')plt.plot(xdn,ydn,'b+')plt.show()def lineartrain(rows):averages={}counts={}for row in rows:# Get the class of this pointcl=row.matchaverages.setdefault(cl,[0.0]*(len(row.data)))counts.setdefault(cl,0)# Add this point to the averagesfor i in range(len(row.data)):averages[cl][i]+=float(row.data[i])# Keep track of how many points in each classcounts[cl]+=1# Divide sums by counts to get the averagesfor cl,avg in averages.items():for i in range(len(avg)):avg[i]/=counts[cl]return averagesdef dotproduct(v1,v2):return sum([v1[i]*v2[i] for i in range(len(v1))])def veclength(v):return sum([p**2 for p in v])def dpclassify(point,avgs):b=(dotproduct(avgs[1],avgs[1])-dotproduct(avgs[0],avgs[0]))/2y=dotproduct(point,avgs[0])-dotproduct(point,avgs[1])+bif y>0: return 0else: return 1def yesno(v):if v=='yes': return 1elif v=='no': return -1else: return 0def matchcount(interest1,interest2):l1=interest1.split(':')l2=interest2.split(':')x=0for v in l1:if v in l2: x+=1return xyahookey="YOUR API KEY"
from xml.dom.minidom import parseString
from urllib.request import urlopen
from urllib.parse import quote_plusloc_cache={}
def getlocation(address):if address in loc_cache: return loc_cache[address]data=urlopen('http://api.local.yahoo.com/MapsService/V1/'+\'geocode?appid=%s&location=%s' %(yahookey,quote_plus('1 alewife center'))).read()doc=parseString(data)lat=doc.getElementsByTagName('Latitude')[0].firstChild.nodeValuelong=doc.getElementsByTagName('Longitude')[0].firstChild.nodeValue loc_cache[address]=(float(lat),float(long))return loc_cache[address]def milesdistance(a1,a2):#lat1,long1=getlocation(a1)#lat2,long2=getlocation(a2)#latdif=69.1*(lat2-lat1)#longdif=53.0*(long2-long1)#return (latdif**2+longdif**2)**.5return 0def loadnumerical():oldrows=loadmatch('matchmaker.csv')newrows=[]for row in oldrows:d=row.datadata=[float(d[0]),yesno(d[1]),yesno(d[2]),float(d[5]),yesno(d[6]),yesno(d[7]),matchcount(d[3],d[8]),milesdistance(d[4],d[9]),row.match]newrows.append(matchrow(data))return newrowsdef scaledata(rows):low=[999999999.0]*len(rows[0].data)high=[-999999999.0]*len(rows[0].data)# 寻找最大值和最小值for row in rows:d=row.datafor i in range(len(d)):if d[i]<low[i]: low[i]=d[i]if d[i]>high[i]: high[i]=d[i]# 对数据进行缩放处理函数def scaleinput(d):return [(d[i]-low[i])/(high[i]-low[i])for i in range(len(low)-1)]# 对所有数据进行缩放处理newrows=[matchrow(scaleinput(row.data)+[row.match])for row in rows]# 返回新数据和缩放处理函数return newrows,scaleinputdef rbf(v1,v2,gamma=10):dv=[v1[i]-v2[i] for i in range(len(v1))]l=veclength(dv)return math.e**(-gamma*l)def nlclassify(point,rows,offset,gamma=10):sum0=0.0sum1=0.0count0=0count1=0for row in rows:if row.match==0:sum0+=rbf(point,row.data,gamma)count0+=1else:sum1+=rbf(point,row.data,gamma)count1+=1y=(1.0/count0)*sum0-(1.0/count1)*sum1+offsetif y>0: return 0else: return 1def getoffset(rows,gamma=10):l0=[]l1=[]for row in rows:if row.match==0: l0.append(row.data)else: l1.append(row.data)sum0=sum(sum([rbf(v1,v2,gamma) for v1 in l0]) for v2 in l0)sum1=sum(sum([rbf(v1,v2,gamma) for v1 in l1]) for v2 in l1)return (1.0/(len(l1)**2))*sum1-(1.0/(len(l0)**2))*sum0
篇幅有限,具体练习见另一篇。
python3 集体智慧编程第九章advancedclassify.py代码相关推荐
- 《集体智慧编程》——第一章导读
为什么80%的码农都做不了架构师?>>> 什么是集体智慧 其含义是指:为了长早新的想法,而将一群人的行为.偏好或思想组合在一起. 完成这项工作的一种最为基础的方法,便是使用调查 ...
- 《集体智慧编程》数学公式
这篇博客的目的主要是为了记录这些公式,取自原书附录B. 1.欧几里得距离(Euclidean Distance) 用途:计算距离,衡量相似度 公式: 代码实现: def euclidean(p, q) ...
- 《集体智慧编程》读书笔记2
最近重读<集体智慧编程>,这本当年出版的介绍推荐系统的书,在当时看来很引领潮流,放眼现在已经成了各互联网公司必备的技术. 这次边阅读边尝试将书中的一些Python语言例子用C#来实现,利于 ...
- 《集体智慧编程》读书笔记10
最近重读<集体智慧编程>,这本当年出版的介绍推荐系统的书,在当时看来很引领潮流,放眼现在已经成了各互联网公司必备的技术. 这次边阅读边尝试将书中的一些Python语言例子用C#来实现,利于 ...
- 《集体智慧编程》读书笔记4
最近重读<集体智慧编程>,这本当年出版的介绍推荐系统的书,在当时看来很引领潮流,放眼现在已经成了各互联网公司必备的技术. 这次边阅读边尝试将书中的一些Python语言例子用C#来实现,利于 ...
- 《集体智慧编程》笔记(1 / 12):集体智慧导言
文章目录 什么是集体智慧 什么是机器学习 机器学习的局限性 真实生活中的例子 学习型算法的其他用途 小结 Netflix, Google都适用了先进算法,将来自不同人群的数据加以组合,进而得出新的结论 ...
- 《集体智慧编程》第九章
1.P210 函数scaledata()在运行时会报错: AttributeError: 'list' object has no attribute 'data' 这是由于函数scaledata() ...
- 《集体智慧编程》第8章
1.P175 在计算高斯函数时,代码中的默认标准差为10.0,如果默认标准差为10是得不到正文中的数据的,这里的默认值应该改为1.0 附上高斯函数的公式和图像 公式中的a代表高斯函数的最大值,b代表平 ...
- 《集体智慧编程》第六章
1.P126代码 为了定义阈值,请修改初始化方法,在classifier中加入一个新的实例变量: def __init__(self, getfeatures):classifier.__init__ ...
- 《集体智慧编程》第二章(一)
一.计算用户相似度 1.欧几里得距离 为了方便以后的读者学习,代码(基于python2.6)全部在最后. 这个没什么好说的,在二维空间中就是两点之间线段的长度.多维空间中,例如A(x1,x2,x3,- ...
最新文章
- 计算机三年工作经验和研究生,三年工作经验和读三年研究生到底哪个更值?这个回答很权威...
- 使用librosa计算pcen
- 四、HTTP响应报文格式
- hdu 4099 字典树 + 斐波那契
- 时间选择控件在不同场景下的应用
- Chisel 学习笔记(四)
- 算法9---二叉树的遍历不用栈和递归
- python到底能干啥-Python到底可以干什么?主要应用领域
- 汇编调用C函数--利用堆栈传递参数
- 计算机组成原理r型指令logisim实现_全国计算机二级MS office选择题增分速记宝典!...
- 字符串数组排序,如果可以保证前一个字符的末尾与后一个字符的开头相同,返回1,否则返回-1...
- [转]让内网MOSS门户也用域名访问
- 系统crontab指令
- 元数据:数据治理的基石
- 【WinHex篇】WinHex磁盘克隆教程
- 基于NMF的推荐系统实例
- CentOS7 阻止笔记本合盖时休眠
- 1064 例题5-1-5 连续自然数求和
- 因上努力,果上随缘。
- 百度前端实战训练营第二弹
热门文章
- 基于linux的触摸屏组态软件,基于LINUX的控制系统组态软件研究与开发
- 103规约测试软件,Protocoltester(国电南自103规约调试软件)
- java web 基础知识 流程图
- matlab混合copula,​MATLAB实战—最优Copula函数的选择
- 程序员又惹祸!B站网站后台工程源码泄露 内含部分用户名密码
- 副业 | 程序员开启副业之路经验之谈!
- 硬盘可以分为几类,监控专用硬盘和普通硬盘有什么区别?
- linux使用samba实现文件共享
- UE4官方文档学习笔记材质篇——彩色半透明阴影
- Fiddler Everywhere v3.4.0