Example #1
0
def run_assignment_2_4():
    print "-"*50, "\n", "assignment 2.4: Item based collaborative filtering (ICF)\n", "-"*50
    transCritics = reco.transformCritics(critics)
    person = 'Toby'

    recommendations_euclid = reco.getRecommendedItems(transCritics, person, reco.sim_euclid)
    print "Recommendations for %s (calculated with Euclidean Distance):\n " % (person), recommendations_euclid

    recommendations_pearson = reco.getRecommendedItems(transCritics, person, reco.sim_pearson)
    print "Recommendations for %s (calculated with Pearson Distance):\n " % (person), recommendations_pearson

    print "-"*50
Example #2
0
  def get(self):
    user = self.user
    user_id = user.get_id()
    results = UserRating.query(UserRating.userid == str(user_id))
    
    user_ratings = dict()
    for ur in results:
      user_ratings[ur.userid] = dict(zip(ur.movies, ur.ratings))
    sim = item.itemMatch
    recs = recommendations.getRecommendedItems(user_ratings, sim, str(user_id))

    dictLs = []
    keys = ['index', 'rating', 'movie']
    for rec in recs:
      ls = []
      (rating, movie) = rec
      ls.append(len(dictLs)+1)
      ls.append(round(rating, 1))
      ls.append(movie)
      dictLs.append(dict(zip(keys, ls)))
    logging.info(dictLs[0])
     
    params = {
      'recs': dictLs[:10],
      'title': 'recommend',
      'icon': 'icon-thumbs-up'
    }
    self.render_template('recommend.html', params)
Example #3
0
 def recommendByItems(self, event):
     if not self.isValid(): # 保证输入框存在有效值
         return
     itemsim = recommendations.calculateSimilarItems(self.prefs, n = 50)
     self.resultList = recommendations.getRecommendedItems(self.prefs, itemsim,
             self.userId)[0:self.maxNum]
     self.showResult()
Example #4
0
 def test_getRecommendedItems(self):
     self.assertAlmostEqual(
         recommendations.getRecommendedItems(critics, self.item_sim,
                                             'Toby'),
         [(3.182634730538922, 'The Night Listener'),
          (2.5983318700614575, 'Just My Luck'),
          (2.4730878186968837, 'Lady in the Water')], 5)
Example #5
0
 def recommendByItems(self, event):
     if not self.isValid():  # 保证输入框存在有效值
         return
     itemsim = recommendations.calculateSimilarItems(self.prefs, n=50)
     self.resultList = recommendations.getRecommendedItems(
         self.prefs, itemsim, self.userId)[0:self.maxNum]
     self.showResult()
Example #6
0
 def test_getRecommendedItems(self):
     self.assertEqual(
         recommendations.getRecommendedItems(
             recommendations.critics,
             recommendations.calculateSimilarItems(recommendations.critics),
             'Toby'), [(3.182634730538922, 'The Night Listener'),
                       (2.5983318700614575, 'Just My Luck'),
                       (2.4730878186968837, 'Lady in the Water')])
Example #7
0
def testMovieLens():
    import recommendations

    prefs = loadMovieLens()

    userId = '87'
    print prefs[userId]

    start = time.clock()
    print recommendations.getRecommendations(prefs, userId)[0:30]
    print "Total Time:" + str(time.clock() - start)
    print

    print "Preprocessing..."
    itemMatches = recommendations.calculateSimilarItems(prefs, n=50)
    print

    start = time.clock()
    print recommendations.getRecommendedItems(prefs, itemMatches, userId)[0:30]
    print "Total Time:" + str(time.clock() - start)
Example #8
0
def getRecommendations(ratings, number):
    # logging.info(ratings)
    user_rating = dict()
    for key in ratings:
        user_rating[int(key)] = ratings[key]
    user_ratings = {'current':user_rating}
    rec_list = recommendations.getRecommendedItems(user_ratings, jokesim.sim_critics, 'current')
    if len(rec_list) > number:
        rec_list = rec_list[0:number]
    
    # logging.info(rec_list)
    rec_jokes = list()
    for item in rec_list:
        # rec_jokes[item[1]] = jokes.jokes[item[1]]
        rec_jokes.append((item[1], jokes.jokes[item[1]], item[0]))
    return rec_jokes
Example #9
0
 def get_recommendations(self, type='user_similarity', similarity='pearson'):
     from recommendations import getRecommendations, calculateSimilarItems, get_similarity, getRecommendedItems
     if type == 'user_similarity':
         users_prefs = User.get_users_ratings_dict(lazy_evaluation=False)
         sim = get_similarity(similarity)
         return [
             (star, Product.get_by_id(productid))
             for star, productid in getRecommendations(users_prefs, self.id, similarity=sim)
             ]
     elif type == 'item_similarity':
         users_prefs = User.get_users_ratings_dict(lazy_evaluation=True)
         similar_items = ItemSimilarity.get_similarity_dict()
         return [
             (star, Product.get_by_id(productid))
             for star, productid in getRecommendedItems(users_prefs, similar_items, self.id)
             ]
     else:
         raise ValueError('Unknown recommendation type %s' % type)
Example #10
0
# recommendationTest.py

import recommendations

itemsim = recommendations.calculateSimilarItems(recommendations.critics)
'''
recommendedList = recommendations.getRecommendedItems(recommendations.critics, itemsim, 'Toby')

print(recommendedList)
'''

prefs = recommendations.loadMovieLens()
# print prefs['87']

print 'user-based recommendations : '
recommendeduser = recommendations.getRecommendations(prefs, '87')[0:30]
print recommendeduser

print 'item-based recommendations : '
itemsim = recommendations.calculateSimilarItems(prefs, n=50)
recommendeditems = recommendations.getRecommendedItems(prefs, itemsim,
                                                       '87')[0:30]
print recommendeditems
Example #11
0
import recommendations, jokesim, jokes

# The key of new user data must be in the following list, which repsents all the keys in the sim_critics

# [5, 7, 8, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150]


newusers = {
    "user1":{
        5: 4.5,
        8: 7.0,
        ## add more testing ratings here
    }
}


print len(recommendations.getRecommendedItems(newusers, jokesim.sim_critics, 'user1'))
print recommendations.getRecommendedItems(newusers, jokesim.sim_critics, 'user1')

print jokes.jokes[5]
Example #12
0
import recommendations
import time;


def loadMovieLens(path='/Users/yupeng/Downloads/ml-100K'):
    movies = {}
    for line in open(path + '/u.item'):
        (id, title) = line.split('|')[0:2]
        movies[id] = title

    prefs = {}
    for line in open(path + '/u.data'):
        (user, movieid, rating, ts) = line.split("\t")
        prefs.setdefault(user, {})
        prefs[user][movies[movieid]] = float(rating)
    return prefs


prefs = loadMovieLens()
start = time.time()
result = recommendations.getRecommendations(prefs, '87')[0:30]
print result
print time.time() - start
start = time.time()
itemsim = recommendations.calculateSimilarItems(prefs, n=50)
print itemsim
print recommendations.getRecommendedItems(prefs, itemsim, '87')[0:30]
print time.time() - start
Example #13
0
print sim_distance(critics, 'Lisa Rose', 'Gene Seymour')

print 'Pearson Correlation Score of Lisa Rose and Gene Seymour is '
print sim_pearson(critics, 'Lisa Rose', 'Gene Seymour')

print 'TopMatches 3 for Toby is '
print topMatches(critics, 'Toby', n=3)

# User-Based CF

print '通过按人群与 Toby 相似度,加权重新评分,为影片排名获得推荐: '
print getRecommendations(critics, 'Toby')

print '通过查看哪些人喜欢 Superman Returns,以及这些人喜欢哪些其他物品来确定相似度:'
movies = transformPrefs(critics)
print topMatches(movies, 'Superman Returns')

print '可能最喜欢 Just My Luck 的人群列表(对调人和物不一定能获得有用信息):'
print getRecommendations(movies, 'Just My Luck')

# Item-Based CF

print '构造物品比较数据集:'
itemsim = calculateSimilarItems(critics)
print itemsim

print '基于物品的推荐为 Toby 提供推荐列表:'
print getRecommendedItems(critics, itemsim, 'Toby')


 def testBasics(self):
   d = {'N': {'p': 1.0, 'j': 0.3}, 'Y':{'p': 0.8, 'j': 0.2, 'r':1.0} }
   itemsim = recommendations.calculateSimilarItems(d)
   r = recommendations.getRecommendedItems(d, itemsim, 'N')
   self.assertEquals(1, len(r))
   self.assertEquals('r', r[0][1])
import recommendations as rec

# 2.4.1.1
# transformation of the critics matrix to {"movie":{"Person1":"similarity", ...}}
transCritics = rec.transposeMatrix(rec.critics)

# 2.4.1.2
#
similar_items_euclidean = rec.calculateSimilarItems(transCritics, rec.sim_euclid_normed)
similar_items_pearson = rec.calculateSimilarItems(transCritics, rec.sim_pearson)
print rec.topMatches(transCritics, 'Lady in the Water', rec.sim_euclid)

# 2.4.1.3
print "Recommended Movies (euclidean): " + str(rec.getRecommendedItems(rec.critics, 'Toby Segaran', similar_items_euclidean))
print "Recommended Movies (pearson): " + str(rec.getRecommendedItems(rec.critics, 'Toby Segaran', similar_items_pearson))
Example #16
0
from recommendations import critics
import recommendations

print critics['Lisa Rose']['Lady in the Water']
critics['Toby']['Snakes on a Plane'] = 4.5
print critics['Toby']
print critics
print "-----------"
print recommendations.getRecommendations(recommendations.critics, 'Toby')
print "-----------"
movies = recommendations.transformPrefs(recommendations.critics)
print  recommendations.topMatches(movies, 'Superman Returns')
print "-----------"
itemsim = recommendations.calculateSimilarItems(recommendations.critics)
print itemsim
print "-----------"
print recommendations.getRecommendedItems(recommendations.critics, itemsim, 'Toby')
print "-----------"
prefs = recommendations.loadMovieLens()
print prefs['1']
print "-----------"
print recommendations.getRecommendations(prefs,'42')[31:60]
print "-----------"
##itemsim = recommendations.calculateSimilarItems(prefs, n =50)
##print itemsim
print "-----------"
##print recommendations.getRecommendedItems(prefs,itemsim, '87')[0:30]
print "-----------"
print recommendations.getKNNRecommendations(prefs,'42',30)[31:60]

import recommendations
import datetime

prefers = recommendations.loadMovieLens()

startTime = datetime.datetime.now()
itemMatch = recommendations.calculateSimilarItems(prefers, n = 50)
print(recommendations.getRecommendedItems(prefers, itemMatch, "87")[0:30])
print((datetime.datetime.now() - startTime).total_seconds())

startTime = datetime.datetime.now()
print(recommendations.getRecommendedItems(prefers, itemMatch, "88")[0:30])
print((datetime.datetime.now() - startTime).total_seconds())
Example #18
0
 created by gjwei on 2017/10/11
  
"""
import pydelicious
from deliciouserc import initializeUserDict
from recommendations import get_recommmendations, getRecommendedItems, calculate_similarity_items


def load_movielens(path='./data/movielens'):
    movies = {}
    for line in open(path + '/u.item'):
        (id, title) = line.split('|')[0:2]
        movies[id] = title

    prefs = {}
    for line in open(path + '/u.data'):
        (user, movieid, rating, ts) = line.split('\t')
        prefs.setdefault(user, {})
        prefs[user][movies[movieid]] = float(rating)

    return prefs


if __name__ == '__main__':
    prefs = load_movielens()
    print(prefs['87'])

    print(get_recommmendations(prefs, '87'))[:3]

    print(getRecommendedItems(prefs, calculate_similarity_items(prefs),
                              '87'))[:5]
Example #19
0
movies = recommendations.transformPrefs(recommendations.critics)
print '调换人与物品:'
print movies

print '打印相似类型的电影:'
print recommendations.topMatches(movies, 'Superman Returns')
print recommendations.getRecommendations(movies, 'Just My Luck')

# import pydelicious
# print pydelicious.get_popular(tag='programming')

# print '开始填充数据:'
# from deliciousrec import *
# delusers = initializeUserDict('programming')
# delusers['tsegaran'] ={}
# fillItems(delusers)
# print delusers

itemSim = recommendations.calculateSimilarItems(recommendations.critics)
print itemSim
print '基于物品推荐:'
print recommendations.getRecommendedItems(recommendations.critics, itemSim,
                                          'Toby')

print '电影数据:'
prefs = recommendations.loadMovieLens()
print prefs['87']
print recommendations.getRecommendations(prefs, '87')[0:30]
itemSim2 = recommendations.calculateSimilarItems(prefs, n=50)
print recommendations.getRecommendedItems(prefs, itemSim2, '87')[0:30]
Example #20
0
#Get recommendations of movies using Euclidian
print(
    recommendations.getRecommendations(
        recommendations.critics,
        'Toby',
        similarity=recommendations.sim_distance))

# Transpose the matrix to movies vs user
movies = recommendations.transformPrefs(recommendations.critics)

#Print similar movies
print(recommendations.topMatches(movies, 'Superman Returns'))

#Similarity matirx of items
itemsim = recommendations.calculateSimilarItems(recommendations.critics)
print(itemsim)

#Get recommendations based on similarity matrix
print(
    recommendations.getRecommendedItems(recommendations.critics, itemsim,
                                        'Toby'))

# print(recommendations.sim_pearson(critics,'Lisa Rose','Gene Seymour'))

# print(recommendations.getRecommendations(recommendations.critics,'Toby'))

# delusers=deliciousrec.initializeUserDict('programming')

# itemsim=recommendations.calculateSimilarItems(recommendations.critics)
#
# print(itemsim)
 def test_getRecommendedItems(self):
     self.assertEqual(recommendations.getRecommendedItems(recommendations.critics, recommendations.calculateSimilarItems(recommendations.critics),'Toby'),
                     [(3.182634730538922, 'The Night Listener'),(2.5983318700614575, 'Just My Luck'),(2.4730878186968837, 'Lady in the Water')])
Example #22
0
print recommendations.topMatches(recommendations.critics,'Toby',n=3,similarity=recommendations.sim_distance)

recommendations.getRecommendations(recommendations.critics,'Toby')

recommendations.getRecommendations(recommendations.critics,'Toby',similarity=recommendations.sim_distance)

movies=recommendations.transformPrefs(recommendations.critics)

#print movies

recommendations.topMatches(movies,'Superman Returns')

recommendations.getRecommendations(movies,'Just My Luck')

recommendations.getRecommendations(movies,'Lady in the Water')

itemsim=recommendations.calculateSimilarItems(recommendations.critics,n=8)
itemsim

recommendations.getRecommendedItems(recommendations.critics,itemsim,'Toby')

prefs=recommendations.loadMovieLens()
prefs['87']


recommendations.getRecommendations(prefs,'87')[0:30]

itemsim1=recommendations.calculateSimilarItems(prefs,n=50)
recommendations.getRecommendedItems(prefs,itemsim1,'87')[0:30]
Example #23
0
 def test_getRecommendedItems(self):
     self.assertAlmostEqual(recommendations.getRecommendedItems(critics, self.item_sim, 'Toby'),
                            [(3.182634730538922, 'The Night Listener'),
                             (2.5983318700614575, 'Just My Luck'),
                             (2.4730878186968837, 'Lady in the Water')], 5)