《数据挖掘导论(共4页).doc》由会员分享,可在线阅读,更多相关《数据挖掘导论(共4页).doc(4页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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-
2、x)2)p=sqrt(s2)p#余弦相似性xy-sum(x*y)x1=sqrt(sum(x2)y1=sqrt(sum(y2)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)standardycorrxy=covarian
3、ce/(standardx*standardy)corrxy#(b)#余弦x-c(0,1,0,1)y-c(1,0,1,0)xy-sum(x*y)x1=sqrt(sum(x2)y1=sqrt(sum(y2)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(s
4、um(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(x2)y2=sum(y2)EJ=xy/(x2+y2-xy)EJ#(c)#余弦x=c(0,-1,0,1)y=c(1,0,-1,0)xy-sum(x*y)x1=sqrt(sum(x2)y1=sqrt(sum(y2)cosxy=xy/(x1*y1)cosxy#
5、相关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/(standardx*standardy)corrxy#欧几里得x=c(0,-1,0,1)y=c(1,0,-1,0)s2=sum(y-x)2)p=sqrt(s2
6、)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(x2)y1=sqrt(sum(y2)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(y)/ncovariance-sum(x-x0)*(y-y0)/(n-1)covariancestandardx-sqrt(sum(x-x0)2)/(n-1)st
7、andardxstandardy-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,0,0,1)xy-sum(x*y)x2=sum(x2)y2=sum(y2)EJ=xy/(x2+y2-xy)EJ#(e)x=(2,-1,0,2,0,-3),y=(-1,-1,-1,0,0,-1)#余弦x=c(2,-1,0,2,0,-3)y=c(-1,-1,-1,0,0,-1)xy-sum(x*y)x1=sqrt(sum(x2)y1=sqrt(sum(y2)cosxy=xy/(x1*y1)cosxy#相关x=c(2,-1,0,2,0,-3)y=c(-1,-1,-1,0,0,-1)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/(standardx*standardy)corrxy专心-专注-专业
限制150内