Finding Similar Users-Euclidean Distance Score

Purpose: Finding Similar Users

Method:  Euclidean Distance Score

ex2.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, ‘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}}

from math import sqrt

# Returns a distance-based similarity score for person1 and person2
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+sqrt(sum_of_squares))

test

>>> import imp
>>> import ex2
>>> imp.reload(ex2)
<module ‘ex2‘ from ‘/home/qiu/桌面/collective programming/code/PCI_Code Folder/chapter2/ex2.py‘>
>>> ex2.sim_distance(ex2.critics,‘Lisa Rose‘,‘Gene Seymour‘)
0.29429805508554946

problems in the test

1.>>> reload(ex2)

NameError: name ‘reload‘ is not defined

solution: For python 3.6

>>> import imp
>>> import ex2
>>> imp.reload(ex2)

时间: 2024-10-02 18:32:01

Finding Similar Users-Euclidean Distance Score的相关文章

论文学习-Euclidean Distance Matrices-theory,algorithms,applications(1)

翻译自Euclidean Distance Matrices: Essential theory, algorithms, and applications EDMs是点之间的平均距离矩阵.该文的目标是介绍EMD在信号处理领域的应用,展示EDM如何被用来设计算法--对距离数据进行修复和去噪.同时,介绍了其在麦克风的位置校准(microphone position calibration).超声波断层扫描(ultrosound tomography).room reconstruction fro

论文学习-Euclidean Distance Matrices-theory,algorithms,applications(2)

接上篇 ORTHOGONAL PROCRUSTES PROBLEM  普式分析(Procrustes analysis)  纠结了好久,还是没能完全看懂.. 奇异值分解(singular value decomposition):,其中. 弗罗贝尼乌斯范数(Frobenius norm): COUNTING THE DEGREES OF FREEDOM EDMs AS A PRACTICAL TOOL 我们很少有完美的EDM,使用接收功率或TOA来测量距离很容易有噪声.抽样误差和模型失配等问题.

简单推荐系统的SQL实现

根据集体智慧编程第二章内容,运用欧几里得距离算法或者皮尔逊相关系数算法,可以在数据库(SQL Server)中实现一个简单的推荐系统. 项目背景: 假设现在有一组来自基金销售网站的数据,记录了投资者购买基金的品种和购买的数量占该基金发售总量的百分比,我们可以利用这组数据为购买者提供一份推荐购买的基金列表. 数据准备: 1 CREATE TABLE tbl_Fund( 2 UserID int NULL, 3 ItemID int NULL, 4 Score decimal(15,5) NULL)

What does the distance attribute in DMatches mean?

In this context, a feature is a point of interest on the image. In order to compare features, you "describe" them using a feature detector. Each feature is then associated to a descriptor. When you match features, you actually match their descri

相似性度量(Similarity Measurement)与“距离”(Distance)

在做分类时常常需要估算不同样本之间的相似性度量(Similarity Measurement),这时通常采用的方法就是计算样本间的“距离”(Distance).采用什么样的方法计算距离是很讲究,甚至关系到分类的正确与否. 本文的目的就是对常用的相似性度量作一个总结. 本文目录: 1. 欧氏距离 2. 曼哈顿距离 3. 切比雪夫距离 4. 闵可夫斯基距离 5. 标准化欧氏距离 6. 马氏距离 7. 夹角余弦 8. 汉明距离 9. 杰卡德距离 & 杰卡德相似系数 10. 相关系数 & 相关距离

欧几里德投影(Euclidean projection)

Euclidean projection on a set An Euclidean projection of a point on a set is a point that achieves the smallest Euclidean distance from to the set. That is, it is any solution to the optimization problem When the set is convex, there is a unique solu

distance

1 /* 2 * Copyright 2016 E-Tool, Inc. 3 */ 4 5 #ifndef distance_h 6 #define distance_h 7 #include <math.h> 8 #include <vector> 9 using std::vector; 10 11 #define max(a,b) (a>b?a:b) 12 #define min(a,b) (a<b?a:b) 13 14 class Distance { 15 p

paper 114:Mahalanobis Distance(马氏距离)

(from:http://en.wikipedia.org/wiki/Mahalanobis_distance) Mahalanobis distance In statistics, Mahalanobis distance is a distance measure introduced by P. C. Mahalanobis in 1936.It is based on correlations between variables by which different patterns

三分题两道:lightoj1146 Closest Distance、lightoj1240 Point Segment Distance (3D)

lightoj1146 Two men are moving concurrently, one man is moving from A to B and other man is moving from C to D. Initially the first man is at A, and the second man is at C. They maintain constant velocities such that when the first man reaches B, at