数据挖掘中的相似性度量指数

来源:互联网 发布:网络磁盘 编辑:程序博客网 时间:2024/05/19 14:51

       在数据挖掘分类算法中,常常提到根据数据表项的相似性进行分类,那么主要用到的相似性度量有哪些呢?这里总结一下最常用的两个:(1)欧几里德距离评价(2)皮尔逊相关评价:

      (1)欧几里德距离评价

           该系数就是我们从小到大的计算两点之间的距离。不同点在于,这里的两点不再是局限与二维,而是n维,每一维对应于原始数据中的一个属性。公式如下:

           d = sqrt((x1-x2)^+(y1-y2)^)

      (2)皮尔逊相关评价

           该系数是判断两组数据与某一条直线的拟合程度的一种度量。对数据不是很规范的时候,会倾向于给出更好的效果。公式如下: 

          

        最后,常有人问:究竟哪一种才是比较好的,换句话说,我应该选哪一个?心里话,我还真不知道,具体要看实际项目的需要了,要不这两个试试,看哪个效果好,就用哪个呗。

        结合以上的理论只是,我们看一个例子,偏好推荐。在偏好推荐中,时常是需要计算相似性来进行推荐的,相似性越高,当然就越值得推荐,相似性的度量值就用我们刚说的:

        (1)原始数据集

critics={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5, 'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5,  'The Night Listener': 3.0},'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5,  'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0,  'You, Me and Dupree': 3.5}, 'Michael Phillips': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0, 'Superman Returns': 3.5, 'The Night Listener': 4.0},'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0, 'The Night Listener': 4.5, 'Superman Returns': 4.0,  'You, Me and Dupree': 2.5},'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,  'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0, 'You, Me and Dupree': 2.0}, 'Jack Matthews': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0, 'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}}
(2)如果采用欧几里德距离度量,则计算方法如下:

def sim_distance(prefs,person1,person2):  # Get the list of shared_items  si={}  for item in prefs[person1]:     if item in prefs[person2]: si[item]=1  # if they have no ratings in common, return 0  if len(si)==0: return 0  # Add up the squares of all the differences  sum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2)                       for item in prefs[person1] if item in prefs[person2]])  return 1/(1+sum_of_squares)
(2)如果使用皮尔逊度量,则计算如下:

def sim_pearson(prefs,p1,p2):  # Get the list of mutually rated items  si={}  for item in prefs[p1]:     if item in prefs[p2]: si[item]=1  # if they are no ratings in common, return 0  if len(si)==0: return 0  # Sum calculations  n=len(si)    # Sums of all the preferences  sum1=sum([prefs[p1][it] for it in si])  sum2=sum([prefs[p2][it] for it in si])    # Sums of the squares  sum1Sq=sum([pow(prefs[p1][it],2) for it in si])  sum2Sq=sum([pow(prefs[p2][it],2) for it in si])    # Sum of the products  pSum=sum([prefs[p1][it]*prefs[p2][it] for it in si])    # Calculate r (Pearson score)  num=pSum-(sum1*sum2/n)  den=sqrt((sum1Sq-pow(sum1,2)/n)*(sum2Sq-pow(sum2,2)/n))  if den==0: return 0  r=num/den  return r
(3)上面得到了,用户件的相似型,其实我们更关心的用户物品,我们感兴趣的程度。这时我们只需要讲每个用户的相似性值与物品评分进行加权,就得到了该物品与自己的相似度评分了,而不是用户。

原创粉丝点击