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},
'Toby':{'Snakes on a Plane': 4.5, 'You, me and Dupreee': 1.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}}
from math import sqrt
def sim_distance(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 prefes[person1] if item in prefs[person2]])
return 1/(1+sqrt(sum_of_squares))>> import recommendations
>>recommendations.sim_distance(recommendations.critics, 'Lisa Rose', 'Gene Seymour')
Traceback(most recent call last):
File "<stdin>", line1, in?
AttributeError: 'module' object has no attribute 'sim_pearson'发布于 2014-05-05 14:37:17
假设以上代码位于单个文件中。
使用命令行:
1.打开命令行
2.将目录更改为python代码所在的位置(使用cd C:\user\python 27或其他位置)
3.输入python点击enter(它将启动python的控制台)
4.现在键入以下内容并按下每一行之后的enter键
从建议中引入批评 从建议导入sim_distance Sim_distance(评论家,“丽莎·罗斯”,“吉恩·西摩”)
希望这有助于运行您的代码。
https://stackoverflow.com/questions/22139040
复制相似问题