本文介绍了为什么我会收到"NameError:名称'...'未定义"在python模块中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
文件名:recom.py
# 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+sum_of_squares)
在尝试执行 reload(recom)
Am getting the error ,when i try to do reload(recom)
跟踪(最近一次通话结束): 文件",第1行,在NameError:名称"recom"未定义
Traceback (most recent call last): File "", line 1, inNameError: name 'recom' is not defined
推荐答案
我使用python 3.4.3,但我遇到了同样的问题.下面的解决方案为我解决了.
I use python 3.4.3, and I just encountered the same problem. The below solution solved it for me.
使用reload()
时,还应该在使用from imp import reload
之前使用它.
When you use reload()
you should also use from imp import reload
before you use it.
关于获得欧氏距离分数,您可以得到如下答案:
As to getting the Euclidean Distance Score, you can get your answer like:
from recom import critics
from recom import sim_distance
sim_distance(critics,'Lisa Rose','Gene Seymour')
结果是:0.29429805508554946
The result is: 0.29429805508554946
这篇关于为什么我会收到"NameError:名称'...'未定义"在python模块中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!