zoukankan      html  css  js  c++  java
  • 欧几里得距离评价(Euclidean Distance Score)

    recommendations.py

    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, 'You, Me and Dupree' : 3.5, 'The Night Listener' : 3.0},
        
        '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, 'Superman Returns' : 4.0, 'You, Me and Dupree' : 2.5, 'The Night Listener' : 4.5},
        
        'Mick LaSalle' : {'Lady in the Water' : 3.0, 'Snakes on a Plane' : 4.0, 'Just My Luck' : 2.0, 'Superman Returns' : 3.0, 'You, Me and Dupree' : 2.0, 'The Night Listener' : 3.0},
        
        'Jack Matthews' : {'Lady in the Water' : 3.0, 'Snakes on a Plane' : 4.0, 'Superman Returns' : 3.0, 'You, Me and Dupree' : 3.5, 'The Night Listener' : 3.0},
        
        'Toby' : {'Snakes on a Plane' : 4.5, 'Superman Returns' : 4.0, 'You, Me and Dupree' : 1.0}
    }

    EDS.py

    from recommendations import critics
    from math import sqrt
    
    
    def sim_distance(prefs, person1, person2):
        si = {}
    
        for item in prefs[person1]:
            if item in prefs[person2]:
                si[item] = 1
    
        if len(si) == 0: return 0
    
        sum_of_squares = sum([pow(prefs[person1][item] - prefs[person2][item], 2) for item in si])
    
        return 1 / (1 + sqrt(sum_of_squares))
    
    print sim_distance(critics, 'Lisa Rose', 'Gene Seymour')

    基本原理就和它的名字一样,就是通过计算一个坐标轴中,两点之间的距离。

    设有点P1(X1, Y1),点P2(X2, Y2),SQRT代表平方根,POW代表平方

    则两点之间的距离D = SQRT( POW(X1-X2) + POW(Y1-Y2) )

    在本例中X与Y分别代表两部电影平分,如果距离D越小,则代表两个人的相似度越近。

  • 相关阅读:
    Shiro自定义密码匹配认证
    logback 发送邮件和自定义发送邮件;java类发送邮件
    webVR全景图多种方案实现(pannellum,aframe,Krpano,three,jquery-vrview)
    前端接受后端文件流并下载的几种方法
    回流(reflow)与重绘(repaint)
    JS数组去重的几种常见方法
    React 生命周期
    浅谈React工作原理
    如何在Vue项目中使用vw实现移动端适配
    移动端web整理 移动端问题总结,移动web遇到的那些坑
  • 原文地址:https://www.cnblogs.com/tuwenmin/p/3025721.html
Copyright © 2011-2022 走看看