最简单的分类器(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])
    


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

posted on 2013-08-15 09:24  江小鱼2015  阅读(404)  评论(0编辑  收藏  举报

导航