Ejemplo n.º 1
0
 def test_transformPrefs(self):
     self.assertEqual(
         recommendations.transformPrefs(
             recommendations.critics)['Lady in the Water'], {
                 'Lisa Rose': 2.5,
                 'Jack Matthews': 3.0,
                 'Michael Phillips': 2.5,
                 'Gene Seymour': 3.0,
                 'Mick LaSalle': 3.0
             })
 def test_topMatches_movies(self):
     movies = rec.transformPrefs(rec.critics)
     matches_long = rec.topMatches(movies, 'Superman Returns')
     matches = round_sequence(matches_long, 3, trunc_n) 
     self.assertEqual(
             matches,
             [(0.657, 'You, Me and Dupree'), 
              (0.487, 'Lady in the Water'), 
              (0.111, 'Snakes on a Plane'), 
              (-0.179, 'The Night Listener'), 
              (-0.422, 'Just My Luck')])
 def test_transformPrefs(self):
     byname = {'Lisa Rose': 
                   {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5},
               'Gene Seymour': 
                   {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5}}
     bymovie = {'Lady in the Water':
                   {'Lisa Rose':2.5,'Gene Seymour':3.0},
                'Snakes on a Plane':
                   {'Lisa Rose':3.5,'Gene Seymour':3.5}} 
     actual = rec.transformPrefs(byname)
     expected = bymovie
     self.assertEqual(actual, expected)
Ejemplo n.º 4
0
def testDelicious():
    import recommendations

    delusers = initializeUserDict('programming')
    fillItems(delusers)

    user = delusers.keys()[random.randint(0, len(delusers) - 1)]
    print user
    print recommendations.topMatches(delusers, user, similarity=recommendations.simEuclidean)
    recUrls = recommendations.getRecommendations(delusers, user, similarity=recommendations.simEuclidean)[0:10]
    print recUrls

    url = recUrls[0][1]
    print url
    print recommendations.topMatches(recommendations.transformPrefs(delusers), url)
Ejemplo n.º 5
0
 def real_you_favorite(self):
     print("List of movies :")
     
     prefs = recommendations.transformPrefs(self.prefs)
     idx = 1
     movies = []
     for movie in prefs:
         print("[%s] %s" % (idx, movie))
         movies.append(movie)
         idx = idx + 1
     
     fav = input("Select your most favorite movie")
     fav_title = movies[int(fav)-1]
     
     scores=[(recommendations.sim_pearson(prefs, fav_title, other),other) for other in prefs if other!=fav_title]
     scores.sort()
     scores.reverse()
     
     total = len(scores)
     return (fav_title, scores[0:5], scores[total-6:total-1])
Ejemplo n.º 6
0
 def check_database(self, data_dir='data'):
     conv = convert_data.Converter(self.db)
     conv.setup()
     
     rs = self.cursor.execute("SELECT COUNT(*) FROM user;")
     for count, in rs.fetchall():
         if count < len(self.prefs):
             conv.import_user(data_dir + '/u.user')
     
     prefs = recommendations.transformPrefs(self.prefs)
     rs = self.cursor.execute("SELECT COUNT(*) FROM item;")
     for count, in rs.fetchall():
         if count < len(prefs):
             conv.import_item(data_dir + '/u.item')
     
     num_data = 0
     for mov in prefs:
         num_data = num_data + len(prefs[mov])
     
     rs = self.cursor.execute("SELECT COUNT(*) FROM data;")
     for count, in rs.fetchall():
         if count < num_data:
             conv.import_data(data_dir + '/u.data')
 def testBasics(self):
   d = { 'a': {'b': 0.4}, 'c': {'d': 0.5} }
   expected = { 'b': {'a': 0.4}, 'd': {'c' : 0.5} }
   self.assertEquals(expected, recommendations.transformPrefs(d))
Ejemplo n.º 8
0
        # calculate the simmilarity
        sim = similarity(prefs, person, other)

        # ignore any people who aren't simmilar
        if sim <= 0:
            continue

        for item in prefs[other]:
            # only consider movies they haven't seen
            if item not in prefs[person]:
                # if it doesn't exist in the list of totals, set it's count as 0
                totals.setdefault(item, 0)
                totals[item] += prefs[other][item] * sim

                simmilarity_sums.setdefault(item, 0)
                simmilarity_sums[item] += sim

    rankings = [(total / simmilarity_sums[item], item)
                for item, total in totals.items()]

    rankings.sort(key=lambda x: x[0])
    rankings.reverse()
    return rankings


for score, item in getRecommendations(critics, 'Toby'):
    print("For ", item, " the score is ", score)

for score, item in getRecommendations(transformPrefs(critics), 'Just My Luck'):
    print("For ", item, " the score is ", score)
 def testEmptyPrefsList(self):
   d = { 'a': {}, 'c': {'d': 0.5} }
   expected = { 'd': {'c' : 0.5} }
   self.assertEquals(expected, recommendations.transformPrefs(d))
Ejemplo n.º 10
0
__author__ = 'ponomarevandrew'


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 "-----------"
 def testAllEmpty(self):
   d = { }
   expected = { }
   self.assertEquals(expected, recommendations.transformPrefs(d))
 def testOnlyEmptyPrefs(self):
   d = { 'a': {} }
   expected = { }
   self.assertEquals(expected, recommendations.transformPrefs(d))
Ejemplo n.º 13
0
# 
from appdata import AppData
import recommendations as rec
import sys
import pickle

if __name__ == '__main__':
  prefs = rec.loadMovieLens('../data/movielens-100k')
  itemPrefs=rec.transformPrefs(prefs)

  try:
    input = open('itemNeighbours.pkl', 'rb')
    itemNeighbours = pickle.load(input)
    input.close()
  except Exception, e:
    print "Calculating itemNeighbours.pkl"
    itemNeighbours = rec.calcNarestNeighbours(prefs, 10)
    output = open('itemNeighbours.pkl', 'wb')
    pickle.dump(itemNeighbours, output)
    output.close()

  try:
    input = open('userNeighbours.pkl', 'rb')
    userNeighbours = pickle.load(input)
    input.close()
  except Exception, e:
    print "Calculating userNeighbours.pkl"
    userNeighbours = rec.calcNarestNeighbours(itemPrefs, 10)
    output = open('userNeighbours.pkl', 'wb')
    pickle.dump(userNeighbours, output)
    output.close()
Ejemplo n.º 14
0
#
from appdata import AppData
import recommendations as rec
import sys
import pickle

if __name__ == '__main__':
    prefs = rec.loadMovieLens('../data/movielens-100k')
    itemPrefs = rec.transformPrefs(prefs)

    try:
        input = open('itemNeighbours.pkl', 'rb')
        itemNeighbours = pickle.load(input)
        input.close()
    except Exception, e:
        print "Calculating itemNeighbours.pkl"
        itemNeighbours = rec.calcNarestNeighbours(prefs, 10)
        output = open('itemNeighbours.pkl', 'wb')
        pickle.dump(itemNeighbours, output)
        output.close()

    try:
        input = open('userNeighbours.pkl', 'rb')
        userNeighbours = pickle.load(input)
        input.close()
    except Exception, e:
        print "Calculating userNeighbours.pkl"
        userNeighbours = rec.calcNarestNeighbours(itemPrefs, 10)
        output = open('userNeighbours.pkl', 'wb')
        pickle.dump(userNeighbours, output)
        output.close()
Ejemplo n.º 15
0
        print recommendations.topMatches(critics, user, 3,
                                         recommendations.sim_distance), '\n'
        print 'Recommended books according to the euclidian distance\n'
        print recommendations.getRecommendations(
            critics, user, similarity=recommendations.sim_distance)[0:3], '\n'
    else:
        if dischoice == '2':
            print 'top matched user according to the pearson corelation\n'
            print recommendations.topMatches(critics, user, 3), '\n'
            print 'Recommended books according to the pearson corelaton\n'
            print recommendations.getRecommendations(critics, user)[0:3], '\n'
        else:
            print 'invalid choice... try again'
else:
    if choice == '2':
        critic = recommendations.transformPrefs(critics)
        book = raw_input('enter the book name:\t')
        print '\n'
        print 'Users who have read the book till now are:\n'
        print critic[book], '\n'
        disch = raw_input(
            'enter your choice\n1.euclidian distance\n2.pearson corelation\nchoice:\t'
        )
        if disch == '1':
            print 'top matched book according to the euclidian distance\n'
            print recommendations.topMatches(
                critic, book, 3, recommendations.sim_distance), '\n'
            print 'Recommended user according to the euclidian distance\n'
            print recommendations.getRecommendations(
                critic, book,
                similarity=recommendations.sim_distance)[0:3], '\n'
Ejemplo n.º 16
0
print 'Euclidean Distance Score of Lisa Rose and Gene Seymour is '
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 testCollect(self):
   d = { 'a': {'z': 0.1}, 'b': {'z' : 0.2}, 'c': {'z': 0.3} }
   expected = { 'z': {'a': 0.1, 'b': 0.2, 'c': 0.3} }
   self.assertEquals(expected, recommendations.transformPrefs(d))
Ejemplo n.º 18
0
#!/usr/local/bin/python
import sys
import recommendations

if __name__ == '__main__':
    fav_moviename = "Die Hard (1988)"
    hate_moviename = "Kazaam (1996)"
    prefs = recommendations.loadMovieLens()
    itemPrefs = recommendations.transformPrefs(prefs)
    fav_results = recommendations.topMatches(itemPrefs, fav_moviename, 2000)
    hate_results = recommendations.topMatches(itemPrefs, hate_moviename, 2000)

print "Most 5 correlated for my top favourite movie"
for i in fav_results[0:5]:
    print i[0], i[1]
print '\n'
print "Least 5 correlated for my top favourite movie"
fav_results.reverse()
for i in fav_results[0:5]:
    print i[0], i[1]
print '\n'
print "Most 5 correlated for my least favourite movie"
for i in hate_results[0:5]:
    print i[0], i[1]
print '\n'
print "Least 5 correlated for my least favourite movie"
hate_results.reverse()
for i in hate_results[0:5]:
    print i[0], i[1]
Ejemplo n.º 19
0
def main():
	movies=recommendations.transformPrefs(recommendations.critics)
	print recommendations.getRecommendations(movies,'Just My Luck')
Ejemplo n.º 20
0
#!/usr/local/bin/python
import sys
import recommendations

if __name__ == '__main__':
    fav_moviename   = "Die Hard (1988)"
    hate_moviename = "Kazaam (1996)"
    prefs = recommendations.loadMovieLens()
    itemPrefs = recommendations.transformPrefs(prefs)
    fav_results   = recommendations.topMatches(itemPrefs,fav_moviename,2000)
    hate_results   = recommendations.topMatches(itemPrefs,hate_moviename,2000)
    
print "Most 5 correlated for my top favourite movie"
for i in fav_results[0:5]:
	print i[0],i[1]
print '\n'
print "Least 5 correlated for my top favourite movie"    
fav_results.reverse()
for i in fav_results[0:5]:
	print i[0],i[1]
print '\n'        
print "Most 5 correlated for my least favourite movie"
for i in hate_results[0:5]:
	print i[0],i[1]
print '\n'
print "Least 5 correlated for my least favourite movie"    
hate_results.reverse()
for i in hate_results[0:5]:
	print i[0],i[1]
        
Ejemplo n.º 21
0
def product(product_id):
    ratings = Rating.query.filter_by(product_id=product_id).all()
    product = Product.query.filter_by(id=product_id).first()
    category = Category.query.filter_by(id=product.category_id).first()

    total = 0
    for r in ratings:
        total += r.rating

    if product.rating_count != 0:
        product.rating = total / product.rating_count

    product.rating = round(product.rating, 2)

    total = 0

    users = []
    comments = []
    rating = []

    ratings = db.engine.execute(
        text(
            "select User.username, Rating.rating, Rating.comment from Rating left join User on Rating.user_id = User.id where Rating.product_id = "
            + str(product_id)))
    for r in ratings:
        rating.append(r[1])
        users.append(r[0])
        comments.append(r[2])
        total += 1

    ratings = Rating.query.all()
    recommended_products = {}

    for i in range(len(ratings)):
        user = User.query.filter_by(id=ratings[i].user_id).first()
        product_check = Product.query.filter_by(
            id=ratings[i].product_id).first()

        if user.username not in recommended_products.keys():
            recommended_products[user.username] = {}

        if product_check.name not in recommended_products[
                user.username].keys():
            recommended_products[user.username][
                product_check.name] = ratings[i].rating

    recommended_products = transformPrefs(recommended_products)
    if product.name not in recommended_products:
        product_list = []
    else:
        recommended_products = topMatches(recommended_products, product.name,
                                          4)
        product_list = [
            Product.query.filter_by(name=product[1]).first()
            for product in recommended_products
        ]

    return render_template("product_details.html",
                           recommended_products=product_list,
                           product=product,
                           category=category,
                           ratings=rating,
                           users=users,
                           comments=comments,
                           total=total)
Ejemplo n.º 22
0
#print recommendations.critics['Lisa Rose']['Lady in the Water']

print recommendations.critics['Lisa Rose']
print recommendations.critics['Gene Seymour']
print recommendations.sim_distance(recommendations.critics,'Lisa Rose','Gene Seymour')
print recommendations.sim_pearson(recommendations.critics,'Lisa Rose','Gene Seymour')

print recommendations.topMatches(recommendations.critics,'Toby',n=3)

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()
 def test_transformPrefs(self):
     self.assertEqual(recommendations.transformPrefs(recommendations.critics)['Lady in the Water'],
                     {'Lisa Rose': 2.5, 'Jack Matthews': 3.0, 'Michael Phillips': 2.5, 'Gene Seymour': 3.0, 'Mick LaSalle': 3.0})