-2

文件名: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)

当我尝试重新加载(recom)时出现错误

Traceback(最近一次调用最后一次):文件“”,第 1 行,在 NameError:名称'recom' 未定义

4

2 回答 2

2

我使用python 3.4.3,我也遇到了同样的问题。下面的解决方案为我解决了它。

使用时reload()也应在使用from imp import reload前使用。

至于获得欧几里得距离分数,您可以得到如下答案:

 from recom import critics
 from recom import sim_distance
 sim_distance(critics,'Lisa Rose','Gene Seymour')

结果是:0.29429805508554946

于 2015-11-23T10:27:44.750 回答
1

You need to have imported the module using "import recom" before it can be reloaded. Also, make sure the code is executing where it can resolve the path to recom.

于 2012-06-10T17:46:05.127 回答