最简单的分类器(nearest neighbor )

原理:先计算出与物品最近的物品(用manhattan距离),然后以最相似物品的评分来估计现在的。

users = {"Angelica": {"Dr Dog/Fate": "L", "Phoenix/Lisztomania": "L", 
                      "Heartless Bastards/Out at Sea": "D", 
                      "Todd Snider/Don't Tempt Me": "D", "The Black Keys/Magic Potion": "D", 
                      "Glee Cast/Jessie's Girl": "L", "La Roux/Bulletproof": "D", 
                      "Mike Posner": "D", "Black Eyed Peas/Rock That Body": "D", 
                      "Lady Gaga/Alejandro": "L"},         
         "Bill":  {"Dr Dog/Fate": "L", "Phoenix/Lisztomania": "L", 
                   "Heartless Bastards/Out at Sea": "L", "Todd Snider/Don't Tempt Me": "D", 
                   "The Black Keys/Magic Potion": "L", "Glee Cast/Jessie's Girl": "D", 
                   "La Roux/Bulletproof": "D", "Mike Posner": "D",                       
                   "Black Eyed Peas/Rock That Body": "D", "Lady Gaga/Alejandro": "D"}
         }

items = {"Dr Dog/Fate": [2.5, 4, 3.5, 3, 5, 4, 1],         
         "Phoenix/Lisztomania": [2, 5, 5, 3, 2, 1, 1],         
         "Heartless Bastards/Out at Sea": [1, 5, 4, 2, 4, 1, 1],         
         "Todd Snider/Don't Tempt Me": [4, 5, 4, 4, 1, 5, 1],         
         "The Black Keys/Magic Potion": [1, 4, 5, 3.5, 5, 1, 1],         
         "Glee Cast/Jessie's Girl": [1, 5, 3.5, 3, 4, 5, 1],         
         "La Roux/Bulletproof": [5, 5, 4, 2, 1, 1, 1],         
         "Mike Posner": [2.5, 4, 4, 1, 1, 1, 1],         
         "Black Eyed Peas/Rock That Body": [2, 5, 5, 1, 2, 2, 4],         
         "Lady Gaga/Alejandro": [1, 5, 3, 2, 1, 2, 1]
         }

def manhattan(v1, v2):
    """Computes the Manhattan distance."""
    distance = 0
    n = len(v1)
    for i in range(n):
        distance += abs(v1[i]-v2[i])
    return distance

def computeNearestNeighbor(itemName,itemVector, items):
    """creates a sorted list of items based on their distance to item"""
    distances = []
    for otherItem in items:
        if otherItem<>itemName:
            distance = manhattan(itemVector, items[otherItem])
            distances.append((distance,otherItem))

    distances.sort()
    return distances

def classify(user, itemName, itemVector):    
   """Classify the itemName based on user ratings       
   Should really have items and users as parameters"""    
   # first find nearest neighbor    
   nearest = computeNearestNeighbor(itemName, itemVector,items)[0][1]    
   rating = users[user][nearest]    
   return rating

print classify('Angelica', 'Enya/Wild Child', [1, 5, 2.5, 1, 1, 5, 1])
    


版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/huaweiquankaiyueweiyuan/p/4964428.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值