资源描述:
《数据挖掘导论.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、19.对于下面的向量x和y,计算指定的相似性或距离度量。(a)x=(1,1,1,1),y=(2,2,2,2)余弦、相关、欧几里得。(b)x=(0,1,0,1),y=(1,0,1,0)余弦、相关、欧几里得、Jaccard。(c)x=(0,-1,0,1),y=(1,0,-1,0)余弦、相关、欧几里得。(d)x=(1,1,0,1,0,1),y=(1,1,1,0,0,1)余弦、相关、Jaccard。(e)x=(2,-1,0,2,0,-3),y=(-1,-1,-1,0,0,-1)余弦、相关。#(a)x=c(1,1,1,1)y=c(2,2,2,2)#欧几里得s2=sum((y-x)^2)p=sqr
2、t(s2)p#余弦相似性xy<-sum(x*y)x1=sqrt(sum(x^2))y1=sqrt(sum(y^2))cosxy=xy/(x1*y1)cosxy#相关性x=c(1,1,1,1)y=c(2,2,2,2)n=length(x)#求x的字符串长度x0=sum(x)/n#计算均值y0=sum(y)/ncovariance<-sum((x-x0)*(y-y0))/(n-1)covariancestandardx<-sqrt((sum((x-x0)^2))/(n-1))standardxstandardy<-sqrt((sum((y-y0)^2))/(n-1))standardyco
3、rrxy=covariance/(standardx*standardy)corrxy#(b)#余弦x<-c(0,1,0,1)y<-c(1,0,1,0)xy<-sum(x*y)x1=sqrt(sum(x^2))y1=sqrt(sum(y^2))cosxy=xy/(x1*y1)cosxy#相关性x<-c(0,1,0,1)y<-c(1,0,1,0)n=length(x)#求x的字符串长度x0=sum(x)/n#计算均值y0=sum(y)/ncovariance<-sum((x-x0)*(y-y0))/(n-1)covariancestandardx<-sqrt((sum((x-x0)^2)
4、)/(n-1))standardxstandardy<-sqrt((sum((y-y0)^2))/(n-1))standardycorrxy=covariance/(standardx*standardy)corrxy#欧几里得x<-c(0,1,0,1)y<-c(1,0,1,0)s2=sum((y-x)^2)p=sqrt(s2)p#Jaccardx<-c(0,1,0,1)y<-c(1,0,1,0)xy<-sum(x*y)x2=sum(x^2)y2=sum(y^2)EJ=xy/(x2+y2-xy)EJ#(c)#余弦x=c(0,-1,0,1)y=c(1,0,-1,0)xy<-sum(x*y
5、)x1=sqrt(sum(x^2))y1=sqrt(sum(y^2))cosxy=xy/(x1*y1)cosxy#相关x=c(0,-1,0,1)y=c(1,0,-1,0)n=length(x)#求x的字符串长度x0=sum(x)/n#计算均值y0=sum(y)/ncovariance<-sum((x-x0)*(y-y0))/(n-1)covariancestandardx<-sqrt((sum((x-x0)^2))/(n-1))standardxstandardy<-sqrt((sum((y-y0)^2))/(n-1))standardycorrxy=covariance/(stand
6、ardx*standardy)corrxy#欧几里得x=c(0,-1,0,1)y=c(1,0,-1,0)s2=sum((y-x)^2)p=sqrt(s2)p#(d)x=c(1,1,0,1,0,1),y=c(1,1,1,0,0,1))#余弦x=c(1,1,0,1,0,1)y=c(1,1,1,0,0,1)xy<-sum(x*y)x1=sqrt(sum(x^2))y1=sqrt(sum(y^2))cosxy=xy/(x1*y1)cosxy#相关x=c(1,1,0,1,0,1),y=c(1,1,1,0,0,1)n=length(x)#求x的字符串长度x0=sum(x)/n#计算均值y0=sum
7、(y)/ncovariance<-sum((x-x0)*(y-y0))/(n-1)covariancestandardx<-sqrt((sum((x-x0)^2))/(n-1))standardxstandardy<-sqrt((sum((y-y0)^2))/(n-1))standardycorrxy=covariance/(standardx*standardy)corrxy#Jaccardx=c(1,1,0,1,0,1)y=c(1,1,1,