• python-推荐


    users={"Angelica": {"Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones":4.5,"Phoenix":5.0,"Slightly Stoopid":1.5,"The Strokes":2.5,"Vampire Weekend": 2.0},
    "Bill":{"Blues Traveler": 2.0, "Broken Bells": 3.5, "Deadmau5": 4.0, "Phoenix": 2.0, "Slightly Stoopid": 3.5, "Vampire Weekend": 3.0},
    "Chan": {"Blues Traveler": 5.0, "Broken Bells": 1.0, "Deadmau5": 1.0, "Norah Jones": 3.0, "Phoenix": 5, "Slightly Stoopid": 1.0},
    "Dan": {"Blues Traveler": 3.0, "Broken Bells": 4.0, "Deadmau5": 4.5, "Phoenix": 3.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0, "Vampire Weekend": 2.0},
    "Hailey": {"Broken Bells": 4.0, "Deadmau5": 1.0, "Norah Jones": 4.0, "The Strokes": 4.0, "Vampire Weekend": 1.0},
    "Jordyn": {"Broken Bells": 4.5, "Deadmau5": 4.0, "Norah Jones": 5.0, "Phoenix": 5.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0, "Vampire Weekend": 4.0},
    "Sam": {"Blues Traveler": 5.0, "Broken Bells": 2.0, "Norah Jones": 3.0, "Phoenix": 5.0, "Slightly Stoopid": 4.0, "The Strokes": 5.0},
    "Veronica": {"Blues Traveler": 3.0, "Norah Jones": 5.0, "Phoenix": 4.0, "Slightly Stoopid": 2.5, "The Strokes": 3.0}}
    def manhattan(rating1, rating2):
    distance = 0
    for key in rating1:
    if key in rating2:
    distance += abs(rating1[key] - rating2[key])
    return distance
    def computeNearestNeighbor(username, users):
    distances = []
    for user in users:
    if user != username:
    distance = manhattan(users[user], users[username])
    distances.append((distance, user))
    distances.sort()
    return distances
    def recommend(username,users):
    nearest = computeNearestNeighbor(username, users)[0][1]
    recommendations = []
    neighborRatings = users[nearest]
    userRatings = users[username]
    for artist in neighborRatings:
    if not artist in userRatings:
    recommendations.append((artist, neighborRatings[artist]))
    return sorted(recommendations, key=lambda artistTuple: artistTuple[1], reverse = True)

  • 相关阅读:
    Java学习——字符串
    Java学习——字符串
    【费马小定理+快速幂取模】ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies
    【费马小定理+快速幂取模】ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies
    Java学习——Eclipse下载,java配置,新建,输入输出
    Java学习——Eclipse下载,java配置,新建,输入输出
    python使用scikit-learn计算TF-IDF
    scanf,printf函数细节
    UVA 4855 Hyper Box
    poj 2001 Shortest Prefixes(字典树)
  • 原文地址:https://www.cnblogs.com/iloveyoucc/p/5158026.html
Copyright © 2020-2023  润新知