def get_recommend(request):
    result_list = list()

    if 1 == 1:
        #判斷是否登入,沒有登入則return not login
        if request.user.is_authenticated():
            user_id = request.user.id
            if user_id == 1:
                result_list = [{'result': 'no jimmy'}]
                return HttpResponse(json.dumps(result_list))
        else:
            result_list = [{'result': 'notlogin'}]
            return HttpResponse(json.dumps(result_list))

        #取回所有評分資料
        for row in course_score.objects.all().order_by('u_id'):
            temp_dict = {
                'user_id': row.u_id,
                'course_id': row.c_id.id,
                'score': row.cs_score,
                'comment': row.cs_comment
            }
            result_list.append(temp_dict)
        #將評分資料做成crab格式
        crabdata = getCrabFormat(result_list)

        #        return HttpResponse((result_list))
        #推薦系統
        model = MatrixPreferenceDataModel(crabdata)
        similarity = UserSimilarity(model, pearson_correlation)
        recommender = UserBasedRecommender(model,
                                           similarity,
                                           with_preference=True)
        recommend_list = recommender.recommend(user_id)

        #        return HttpResponse((recommend_list))
        recommend_list = [
            list(recommend_list[i]) for i in range(0, len(recommend_list))
        ]

        #推薦後資料加上課程名稱、課程資訊...
        result_list = list()
        for i in range(0, len(recommend_list)):
            for course_row in course.objects.filter(id=recommend_list[i][0]):
                tem_dict = {
                    'name': course_row.c_name,
                    'teacher': course_row.c_teacher,
                    'credit': course_row.raw_course_set.all()[0].rc_credit,
                    'depart': course_row.raw_course_set.all()[0].rc_department,
                    'semester': course_row.raw_course_set.all()[0].rc_semester,
                    'c_id': recommend_list[i][0],
                    'raw_cid': course_row.raw_course_set.all()[0].id,
                    'score': recommend_list[i][1]
                }
                result_list.append(tem_dict)

        return HttpResponse(json.dumps(result_list))
    else:
        result_list = [{'result': 'error'}]
        return HttpResponse(json.dumps(result_list))
示例#2
0
def recom(i):
    try:
        M = pd.read_pickle(path + 'DA_Rating.df')
        user = M.ix[:, i]
        user.name
        usernnz = user > 0
        usernnzIDX = np.where(usernnz)
        targets = user[usernnz].index.values
        N = M.copy()
        N.ix[:, i] = 0
        idx = np.random.random_integers(
            0, len(usernnzIDX[0]), min(len(usernnzIDX[0]) - 3, numFeedbacks))
        N.ix[idx, i] = user.iloc[idx]
        model = MatrixPreferenceDataModel(getRecommenderData(N))
        similarity = UserSimilarity(model, cosine_distances, 3)
        recommender = UserBasedRecommender(model,
                                           similarity,
                                           with_preference=True)
        rec = recommender.recommend(user.name, how_many=100)
        ranking = map(lambda x: x[0], rec)
        ap, mrr = AP(ranking, targets), MRR(ranking, targets)
        pd.to_pickle((ap, mrr),
                     outpath + 'numFB{}i{}.pkl'.format(numFeedbacks, i))
    except:
        pass
def main():
    import pprint
    pp = pprint.PrettyPrinter(indent=4)
    from scikits.crab import datasets
    movies = datasets.load_sample_songs()

    print "Movies Data:", pp.pformat(movies)
    print "Movies Data Matrix :", pp.pformat(movies.data)
    from scikits.crab.models import MatrixPreferenceDataModel
    #Build the model
    #new_movies = {u'1': [1, 2, 3, 4, 5], u'3': [2, 3, 4, 5, 6], u'2': [1, 2, 3, 4, 5, 6], u'5': [2, 3, 4], u'4': [1, 2, 3, 4, 5, 6], u'7': [1, 2, 4, 5], u'6': [1, 2, 3, 4, 5, 6]}
    #print "New moview hash", pp.pformat(new_movies)
    #model = MatrixBooleanPrefDataModel(new_movies)
    model = MatrixPreferenceDataModel(movies.data)
    import json
    from scikits.crab.metrics import spearman_coefficient
    from scikits.crab.similarities import UserSimilarity, ItemSimilarity
    #Build the similarity
    user_similarity = UserSimilarity(model, pearson_correlation)
    item_similarity = ItemSimilarity(model, pearson_correlation)

    from scikits.crab.recommenders.knn import UserBasedRecommender, ItemBasedRecommender
    #Build the User based recommender
    user_recommender = UserBasedRecommender(model, user_similarity, with_preference=True)
    item_recommender = ItemBasedRecommender(model, item_similarity, with_preference=True)
    print "recommendations for user 5", user_recommender.recommend("5")
    print "recommendations for item 3", item_recommender.recommend("3")
def custom_recommend(dataset, recommend_to, num_of_recommendations = 10):
	#This is based on the USER-BASED RECOMMENDATION Model
	#Build the model. It returns a dictionary of the recommendations
	model = MatrixPreferenceDataModel(dataset)

	similarity = UserSimilarity(model, euclidean_distances)

	#Build the Recommender
	recommender = UserBasedRecommender(model, similarity, with_preference = True)

	return recommender.recommend(recommend_to, num_of_recommendations)
def recommendPosts(dataModel):
    model = MatrixPreferenceDataModel(dataModel)
    print 'User ID`s: '
    print model.user_ids()
##    print 'Item ID`s: '
##    print model.item_ids()
    userID= input('Please enter a userID: ')
    print 'Loading recommended posts...'
    similarity = UserSimilarity(model, pearson_correlation)
    recommender = UserBasedRecommender(model, similarity, with_preference=True)
    return recommender.recommend(userID)
示例#6
0
def generate_recomendations(model):
    from scikits.crab.metrics import pearson_correlation
    from scikits.crab.similarities import UserSimilarity
    #Build the similarity
    similarity = UserSimilarity(model, pearson_correlation)
    from sklearn.base import BaseEstimator
    from scikits.crab.recommenders.knn import UserBasedRecommender
    #build the user Based recommender
    recommender = UserBasedRecommender(model, similarity, with_preference=True)
    #recommend item for the user 5 (Toby)
    recomendations = recommender.recommend(5)
    return recomendations
示例#7
0
def getRecommender(dataFile):
    from scikits.crab.models.classes import MatrixPreferenceDataModel
    model = MatrixPreferenceDataModel(loadData(dataFile))
    print model[0]
    from scikits.crab.metrics import pearson_correlation
    from scikits.crab.similarities import UserSimilarity
    similarity = UserSimilarity(model, pearson_correlation)
    from scikits.crab.recommenders.knn import UserBasedRecommender
    recommender = UserBasedRecommender(model, similarity, with_preference=True)
    a = 5
    print "For user %d we recommend movie %d" % (a, recommender.recommend(5))
    return recommender
def get_recommend(request):
    result_list = list()

    if 1 == 1:
        #判斷是否登入,沒有登入則return not login
        if request.user.is_authenticated():  
           user_id = request.user.id
           if user_id == 1:
               result_list = [{'result':'no jimmy'}]
               return HttpResponse(json.dumps(result_list))
        else:
            result_list = [{'result':'notlogin'}]
            return HttpResponse(json.dumps(result_list))
        
        #取回所有評分資料
        for row in course_score.objects.all().order_by('u_id'):
            temp_dict = {'user_id':row.u_id, 
                         'course_id':row.c_id.id, 
                         'score':row.cs_score, 
                         'comment':row.cs_comment}
            result_list.append(temp_dict)
        #將評分資料做成crab格式
        crabdata = getCrabFormat(result_list)

#        return HttpResponse((result_list))
        #推薦系統
        model = MatrixPreferenceDataModel(crabdata)
        similarity = UserSimilarity(model, pearson_correlation)
        recommender = UserBasedRecommender(model, similarity, with_preference=True)
        recommend_list = recommender.recommend(user_id)
  
#        return HttpResponse((recommend_list))
        recommend_list = [ list(recommend_list[i]) for i in range(0, len(recommend_list))]

        
        #推薦後資料加上課程名稱、課程資訊...
        result_list = list()
        for i in range(0, len(recommend_list)):
            for course_row in course.objects.filter(id=recommend_list[i][0]):
                tem_dict = {'name':course_row.c_name,
                            'teacher':course_row.c_teacher,
                            'credit':course_row.raw_course_set.all()[0].rc_credit,
                            'depart':course_row.raw_course_set.all()[0].rc_department,
                            'semester':course_row.raw_course_set.all()[0].rc_semester,
                            'c_id':recommend_list[i][0],
                            'raw_cid':course_row.raw_course_set.all()[0].id,
                            'score':recommend_list[i][1]}
                result_list.append(tem_dict)    

        return HttpResponse(json.dumps(result_list))
    else:
        result_list = [{'result':'error'}]
        return HttpResponse(json.dumps(result_list))
示例#9
0
def getRecommender(dataFile):
    from scikits.crab.models.classes import MatrixPreferenceDataModel
    model = MatrixPreferenceDataModel(loadData(dataFile))
    print model[0]
    from scikits.crab.metrics import pearson_correlation
    from scikits.crab.similarities import UserSimilarity
    similarity = UserSimilarity(model, pearson_correlation)
    from scikits.crab.recommenders.knn import UserBasedRecommender
    recommender = UserBasedRecommender(model, similarity, with_preference=True)
    a = 5
    print "For user %d we recommend movie %d" % (a, recommender.recommend(5))
    return recommender
示例#10
0
def custom_recommender(dataset, recommend_to):
	#This is based on the USER-BASED RECOMMENDATION Model
	#Build the model. It returns a dictionary of the recommendations
	model = MatrixPreferenceDataModel(dataset)

	similarity = UserSimilarity(model, euclidean_distances)

	#Build the Recommender
	recommender = UserBasedRecommender(model, similarity, with_preference = True)

	#Evaluate the module. This does not necessarily work in production.
	evaluator = CfEvaluator()
	all_scores = evaluator.evaluate(recommender, permutation=False)

	return recommender.recommend(recommend_to)
示例#11
0
def recom(i):
    try:
        M=pd.read_pickle(path+'DA_Rating.df')
        user=M.ix[:,i]
        user.name
        usernnz=user>0
        usernnzIDX=np.where(usernnz)
        targets=user[usernnz].index.values
        N=M.copy()
        N.ix[:,i]=0
        idx=np.random.random_integers(0,len(usernnzIDX[0]),min(len(usernnzIDX[0])-3,numFeedbacks))
        N.ix[idx,i]=user.iloc[idx]
        model = MatrixPreferenceDataModel(getRecommenderData(N))
        similarity = UserSimilarity(model,  cosine_distances, 3)
        recommender = UserBasedRecommender(model, similarity, with_preference=True)
        rec=recommender.recommend( user.name, how_many=100)
        ranking=map(lambda x: x[0],rec)
        ap,mrr=AP(ranking,targets) , MRR(ranking,targets)
        pd.to_pickle((ap,mrr), outpath+'numFB{}i{}.pkl'.format(numFeedbacks,i))
    except:
        pass
示例#12
0
def api_import1():
    movies = datasets.load_sample_movies()
    import pprint  ## to make printed items clearer
    pprint.pprint(movies.data)
    pprint.pprint(movies.user_ids)
    pprint.pprint(movies.item_ids)
    from scikits.crab.models import MatrixPreferenceDataModel
    #Build the model
    model = MatrixPreferenceDataModel(movies.data)
    from scikits.crab.metrics import pearson_correlation
    from scikits.crab.similarities import UserSimilarity
    #Build the similarity
    similarity = UserSimilarity(model, pearson_correlation)
    from sklearn.base import BaseEstimator
    from scikits.crab.recommenders.knn import UserBasedRecommender
    #build the user Based recommender
    recommender = UserBasedRecommender(model, similarity, with_preference=True)
    #recommend item for the user 5 (Toby)
    recomendations = recommender.recommend(5)
    pprint.pprint(recomendations)

    return 'List of ' + url_for('api_articles')
示例#13
0
def user_base(input_file, output_file, data_file):
    # 基础数据-测试数据
    from scikits.crab import datasets
    #	movies = datasets.load_sample_movies()
    user_name = read_user_name(input_file)

    shopping_history = load_test_data(data_file)

    user_order_num = userName_to_userOrderNum(data_file, user_name)

    #Build the model
    from scikits.crab.models import MatrixPreferenceDataModel
    model = MatrixPreferenceDataModel(shopping_history.data)

    #Build the similarity
    # 选用算法 pearson_correlation
    #    from scikits.crab.metrics import pearson_correlation # need score
    from scikits.crab.metrics import cosine_distances  # DO NOT need score
    from scikits.crab.similarities import UserSimilarity
    similarity = UserSimilarity(model, cosine_distances)

    # 选择 基于User的推荐
    from scikits.crab.recommenders.knn import UserBasedRecommender
    recommender = UserBasedRecommender(model, similarity, with_preference=True)
    ret = recommender.recommend(
        user_order_num)  # 输出个结果看看效果 Recommend items for the user 5 (Toby)

    if ret:
        # 所有推荐结果都保存到文件,交给服务器处理
        f_w = open(output_file, 'w')
        for i in ret:
            product_order_num = i[0]
            product_score = i[1]
            product_name = productOrderNum_to_productName(
                data_file, product_order_num)
            f_w.write(str(product_name) + ',' + str(product_score) + '\n')
示例#14
0
#print movies.item_ids


from scikits.crab.models import MatrixPreferenceDataModel
from scikits.crab.metrics import pearson_correlation
from scikits.crab.similarities import UserSimilarity
from scikits.crab.recommenders.knn import UserBasedRecommender

#Build the model
model = MatrixPreferenceDataModel(movies.data)
#Build the similarity
similarity = UserSimilarity(model, pearson_correlation)
#Build the User based recommender
recommender = UserBasedRecommender(model, similarity, with_preference=True)
#Recommend items for the user 5 (Toby)
rec1 = recommender.recommend(4)
print rec1
#rec2 = recommender.recommend(movies.inv_user_ids['Toby'])
#rec2




'''
from scikits.crab.recommenders.svd import SVDRecommender
#Build the User based recommender
recommender = UserBasedRecommender(model, similarity, with_preference=True)
#Recommend items for the user 5 (Toby)
recommender.recommend(5)

'''
示例#15
0
#! /env/bin/python

from scikits.crab import datasets
from scikits.crab.metrics import pearson_correlation
from scikits.crab.models import MatrixPreferenceDataModel
from scikits.crab.recommenders.knn import UserBasedRecommender
from scikits.crab.similarities import UserSimilarity

# Get the data
movies = datasets.load_sample_movies()
print type(movies)
for key in movies:
    print key, '\t', movies[key]
print

# Build the model
model = MatrixPreferenceDataModel(movies.data)

# Build the similarity
similarity = UserSimilarity(model, pearson_correlation)

# Build the User based recommender
recommender = UserBasedRecommender(model, similarity, with_preference=True)

# Recommend items for the user
for x in range(1, 6):
    print movies.user_ids[x], recommender.recommend(x)
示例#16
0
from scikits.crab import datasets
from scikits.crab.models import MatrixPreferenceDataModel
from scikits.crab.metrics import pearson_correlation
from scikits.crab.recommenders.knn import UserBasedRecommender
from scikits.crab.similarities import UserSimilarity


movies = datasets.load_sample_movies()
songs = datasets.load_sample_songs()

print movies.data
print movies.user_ids
print movies.item_ids

# Build the model
model = MatrixPreferenceDataModel(movies.data)

# Build the similarity
similarity = UserSimilarity(model, pearson_correlation)

# Build the User based recommender_sample.py
recommender = UserBasedRecommender(model, similarity, with_preference=True)

# Recommend items for the user 5 (Toby)
print recommender.recommend(5)
示例#17
0
"""
print '==========movies data============'
print 'user_ids :', movies.user_ids
print 'user_data:', movies.data
print 'item_ids :', movies.item_ids
print '==========movies data============'
"""
"""
#data format of movies
user_ids : {1: 'Jack Matthews', 2: 'Mick LaSalle', 3: 'Claudia Puig', 4: 'Lisa Rose', 5: 'Toby', 6: 'Gene Seymour', 7: 'Michael Phillips'}
user_data: {
1: {1: 3.0, 2: 4.0, 3: 3.5, 4: 5.0, 5: 3.0        },
2: {1: 3.0, 2: 4.0, 3: 2.0, 4: 3.0, 5: 3.0, 6: 2.0},
3: {        2: 3.5, 3: 2.5, 4: 4.0, 5: 4.5, 6: 3.0},
4: {1: 2.5, 2: 3.5, 3: 2.5, 4: 3.5, 5: 3.0, 6: 3.0},
5: {        2: 4.5, 3: 1.0, 4: 4.0                },
6: {1: 3.0, 2: 3.5, 3: 3.5, 4: 5.0, 5: 3.0, 6: 1.5},
7: {1: 2.5, 2: 3.0,         4: 3.5, 5: 4.0        }}
item_ids : {1: 'Lady in the Water', 2: 'Snakes on a Planet', 3: 'You, Me and Dupree', 4: 'Superman Returns', 5: 'The Night Listener', 6: 'Just My Luck'}
"""

model = MatrixPreferenceDataModel(movies.data)  #build the model
similarity = UserSimilarity(model, pearson_correlation)  #build the similarity
recommender = UserBasedRecommender(
    model, similarity, with_preference=True)  #build the recommender

# print recommender.recommend(3)
# print recommender.recommend(1, how_many=None, minimal_similarity=5.0)
print recommender.recommend(1, how_many=100, model__minimal_similarity=0.5)
# print recommender.most_similar_users(1)
示例#18
0
文件: main.py 项目: KinGoverm/ai
movies = datasets.load_sample_movies()
songs = datasets.load_sample_songs()
print movies.data
print movies.user_ids
print movies.item_ids

from scikits.crab.models import MatrixPreferenceDataModel
#Build the model
model = MatrixPreferenceDataModel(movies.data)

from scikits.crab.metrics import pearson_correlation
from scikits.crab.similarities import UserSimilarity
#Build the similarity
similarity = UserSimilarity(model, pearson_correlation)

print similarity[1]

from scikits.crab.metrics.pairwise import euclidean_distances
similarity = UserSimilarity ( model , euclidean_distances)

print similarity[5]


from scikits.crab.recommenders.knn import UserBasedRecommender

#Build the User based recommender
recommender = UserBasedRecommender(model, similarity, with_preference=True)
#Recommend items for the user 5 (Toby)
recommender.recommend(5)

示例#19
0
print data.user_ids
# {1: 'Jack Matthews',
#  2: 'Mick LaSalle',
#  3: 'Claudia Puig',
#  4: 'Lisa Rose',
#  5: 'Toby',
#  6: 'Gene Seymour',
#  7: 'Michael Phillips'}

print data.item_ids
# {1: 'Lady in the Water',
#  2: 'Snakes on a Planet',
#  3: 'You, Me and Dupree',
#  4: 'Superman Returns',
#  5: 'The Night Listener',
#  6: 'Just My Luck'}


# Build the data model
model = MatrixPreferenceDataModel(data.data)

# Build the similarity
similarity = UserSimilarity(model, pearson_correlation)


# Build the User based recommender
recommender = UserBasedRecommender(model, similarity, with_preference=True)

# Recommend items for the user 5 (Toby)
print recommender.recommend(5, how_many=3)
# [(5, 3.3477895267131013), (1, 2.8572508984333034), (6, 2.4473604699719846)]
示例#20
0
文件: example1.py 项目: rcarmo/crab
# This code depicts the snippets appeared in Chapter 2 of the Crab's Official Tutorial:
# 
#   http://muricoca.github.io/crab/tutorial.html#introducing-recommendation-engines
#

from scikits.crab import datasets
from scikits.crab.models import MatrixPreferenceDataModel
from scikits.crab.metrics import pearson_correlation
from scikits.crab.similarities import UserSimilarity
from scikits.crab.recommenders.knn import UserBasedRecommender

# input
movies = datasets.load_sample_movies()
print movies.data

# build the model
model = MatrixPreferenceDataModel(movies.data)
print model

# build the similarity
similarity = UserSimilarity(model, pearson_correlation)

# build the user based recommender
recommender = UserBasedRecommender(model, similarity, with_preference=True)

# recommend items for the user 5
result = recommender.recommend(5)
print result
示例#21
0

from scikits.crab.models import MatrixPreferenceDataModel
#Build the model
model = MatrixPreferenceDataModel(movies.data)

from scikits.crab.metrics import pearson_correlation
from scikits.crab.similarities import UserSimilarity
#Build the similarity
similarity = UserSimilarity(model, pearson_correlation)

from scikits.crab.recommenders.knn import UserBasedRecommender
#Build the User based recommender
recommender = UserBasedRecommender(model, similarity, with_preference=True)
#Recommend items for the user 5 (Toby)
rec1 = recommender.recommend(5)
rec1
rec2 = recommender.recommend(movies.inv_user_ids['Toby'])
rec2




'''
from scikits.crab.recommenders.svd import SVDRecommender
#Build the User based recommender
recommender = UserBasedRecommender(model, similarity, with_preference=True)
#Recommend items for the user 5 (Toby)
recommender.recommend(5)

'''
示例#22
0
def recommender(request):

    freelancer = {"user_ids": {}, 'data': {}, 'item_ids': {}}
    for user in User.objects.all():
        freelancer['user_ids'][user.id] = user.username

    for user in User.objects.all():
        freelancer['item_ids'][user.id] = user.username

    test_item = {"ios": 1, "ui": 2, "android": 3, "website": 4}

    for user in User.objects.all():
        freelancer['data'][user.id] = {}
        for project in Project.objects.all():
            if project.user_id.username == project.user_id.id:
                freelancer['data'][user.id][test_item[proj_type]] = 4
                break
        for id, item in test_item.items():
            freelancer['data'][user.id][item] = random.randint(1, 4)

    print freelancer['data']

    freelancer['data'] = {
        1: {
            1: 3.0,
            2: 4.0,
            3: 3.5,
            4: 5.0,
            5: 3.0
        },
        2: {
            1: 3.0,
            2: 4.0,
            3: 2.0,
            5: 3.0,
            6: 2.0
        },
        3: {
            2: 3.5,
            3: 2.5,
            4: 4.0,
            5: 4.5,
            6: 3.0
        },
        4: {
            1: 2.5,
            2: 3.5,
            3: 2.5,
            4: 3.5,
            5: 3.0,
            6: 3.0
        },
        5: {
            2: 4.5,
            3: 1.0,
            4: 4.0
        },
        6: {
            1: 3.0,
            2: 3.5,
            3: 3.5,
            4: 5.0,
            5: 3.0,
            6: 1.5
        },
        7: {
            1: 2.5,
            4: 3.5,
            5: 4.0
        }
    }

    model = MatrixPreferenceDataModel(freelancer['data'])
    similarity = UserSimilarity(model, pearson_correlation)
    recommender = UserBasedRecommender(model, similarity, with_preference=True)
    print freelancer['data']
    print recommender.recommend(request.user.id)
    return JsonResponse({
        "status": True,
        "data": recommender.recommend(request.user.id)
    })
示例#23
0
# -*- coding:utf-8 -*-

from scikits.crab import datasets
movies = datasets.load_sample_movies()
songs = datasets.load_sample_songs()
from scikits.crab.models import MatrixPreferenceDataModel
model = MatrixPreferenceDataModel(movies.data)
from scikits.crab.metrics import pearson_correlation
from scikits.crab.similarities import UserSimilarity
similarity = UserSimilarity(model, pearson_correlation)
from scikits.crab.recommenders.knn import UserBasedRecommender
recommender = UserBasedRecommender(model, similarity, with_preference=True)
print(recommender.recommend(5))
示例#24
0
文件: crabTest.py 项目: anty-zhang/dm
print 'user_data:', movies.data
print 'item_ids :', movies.item_ids
print '==========movies data============'
"""
"""
#data format of movies
user_ids : {1: 'Jack Matthews', 2: 'Mick LaSalle', 3: 'Claudia Puig', 4: 'Lisa Rose', 5: 'Toby', 6: 'Gene Seymour', 7: 'Michael Phillips'}
user_data: {
1: {1: 3.0, 2: 4.0, 3: 3.5, 4: 5.0, 5: 3.0        },
2: {1: 3.0, 2: 4.0, 3: 2.0, 4: 3.0, 5: 3.0, 6: 2.0},
3: {        2: 3.5, 3: 2.5, 4: 4.0, 5: 4.5, 6: 3.0},
4: {1: 2.5, 2: 3.5, 3: 2.5, 4: 3.5, 5: 3.0, 6: 3.0},
5: {        2: 4.5, 3: 1.0, 4: 4.0                },
6: {1: 3.0, 2: 3.5, 3: 3.5, 4: 5.0, 5: 3.0, 6: 1.5},
7: {1: 2.5, 2: 3.0,         4: 3.5, 5: 4.0        }}
item_ids : {1: 'Lady in the Water', 2: 'Snakes on a Planet', 3: 'You, Me and Dupree', 4: 'Superman Returns', 5: 'The Night Listener', 6: 'Just My Luck'}
"""

model = MatrixPreferenceDataModel(movies.data)   #build the model
similarity = UserSimilarity(model, pearson_correlation)  #build the similarity
recommender = UserBasedRecommender(model, similarity, with_preference=True)   #build the recommender

# print recommender.recommend(3)
# print recommender.recommend(1, how_many=None, minimal_similarity=5.0)
print recommender.recommend(1, how_many=100, model__minimal_similarity=0.5)
# print recommender.most_similar_users(1)




    #recommend_top_5_item  = recsys_item.recommended_because(new_active_user_id,42,how_many=5)
    recommend_list_item = recsys_item.recommend(new_active_user_id, how_many=5)
    #print("Item : " +recommend_list_to_json(recommend_list_item))
    #evaluator = CfEvaluator()
    #test_item_a = evaluator.evaluate_on_split(recsys_item,at=4, sampling_ratings=0.5,permutation=False,cv=5)
    #pprint(test_item_a)
    #elapsed = timeit.default_timer() - start_time
    #print elapsed

    #start_time = timeit.default_timer()
    myDataset_course = create_finally_dataset(150, active_user, student_ID)
    #pprint(myDataset_course)
    new_active_user_id = find_new_user_id(myDataset_course, str(student_ID))
    model = MatrixPreferenceDataModel(myDataset_course['data'])
    similarity_user = UserSimilarity(model, cosine_distances)
    neighborhood_user = NearestNeighborsStrategy()
    recsys_user = UserBasedRecommender(model,
                                       similarity_user,
                                       neighborhood_user,
                                       with_preference=True)
    recommend_list_user = recsys_user.recommend(new_active_user_id, how_many=5)
    #elapsed = timeit.default_timer() - start_time
    #print elapsed
    print recommend_list_to_json(
        recommend_list_item, myDataset_course,
        type="ITEM") + recommend_list_to_json(
            recommend_list_user, myDataset_course, type="USER")
    #evaluator = CfEvaluator()
    #test_user_a = evaluator.evaluate_on_split(recsys_user,at=4,sampling_ratings=0.7,permutation=False,cv=5)
    #pprint(test_user_a)
示例#26
0
from scikits.crab.similarities import UserSimilarity
from scikits.crab.metrics import euclidean_distances
from scikits.crab.recommenders.knn import UserBasedRecommender

# For reproducibility
np.random.seed(1000)

if __name__ == '__main__':
    # Define a user-item matrix
    user_item_matrix = {
        1: {1: 2, 2: 5, 3: 3},
        2: {1: 5, 4: 2},
        3: {2: 3, 4: 5, 3: 2},
        4: {3: 5, 5: 1},
        5: {1: 3, 2: 3, 4: 1, 5: 3}
    }

    # Build a matrix preference model
    model = MatrixPreferenceDataModel(user_item_matrix)

    # Build a similarity matrix
    similarity_matrix = UserSimilarity(model, euclidean_distances)

    # Create a recommender
    recommender = UserBasedRecommender(model, similarity_matrix, with_preference=True)

    # Test the recommender for user 2
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        print(recommender.recommend(2))
示例#27
0
# {1: 'Jack Matthews',
#  2: 'Mick LaSalle',
#  3: 'Claudia Puig',
#  4: 'Lisa Rose',
#  5: 'Toby',
#  6: 'Gene Seymour',
#  7: 'Michael Phillips'}

print data.item_ids
# {1: 'Lady in the Water',
#  2: 'Snakes on a Planet',
#  3: 'You, Me and Dupree',
#  4: 'Superman Returns',
#  5: 'The Night Listener',
#  6: 'Just My Luck'}


# Build the data model
model = MatrixPreferenceDataModel(data.data)

# Build the similarity
similarity = UserSimilarity(model, pearson_correlation)


# Build the User based recommender
recommender = UserBasedRecommender(model, similarity, with_preference=True)

# Recommend items for the user 5 (Toby)
print recommender.recommend(5, how_many=3)
# [(5, 3.3477895267131013), (1, 2.8572508984333034), (6, 2.4473604699719846)]
示例#28
0
    for (item, pre) in pre:
        # print 'item=', item, ',pre=', pre
        if np.isnan(pre):
            print 'item=', item

def testParam(how_many=None, **param):
    print 'how_many:', how_many
    print 'param:', param

if __name__ == '__main__':
    # testParam(how_many=12, tt=10, dd=40)
    start_time = time.time()
    file_name = 'score_data.txt'
    with open(file_name) as f:
        d = f.read()
    # print 'd=', d
    d = eval(d)
    model = MatrixPreferenceDataModel(d)   #build the model
    similarity = UserSimilarity(model, pearson_correlation, num_best=50)  #build the similarity
    recommender = UserBasedRecommender(model, similarity, with_preference=True)   #build the recommender

    print recommender.recommend(user_id=31071, how_many=30, minimal_similarity=0.8)
    # print recommender.recommend(20832)
    # print recommender.most_similar_users(user_id=31071)
    # print d[20832]
    print '====recommand time:', time.time() - start_time
    '''
    for key in d:
        print '===key===', key
        print recommender.recommend(key)
    '''
示例#29
0
    7: {
        1: 2.5,
        4: 3.5,
        5: 4.0
    }
}

movies['user_ids'] = {
    1: 'Jack Matthews',
    2: 'Mick LaSalle',
    3: 'Claudia Puig',
    4: 'Lisa Rose',
    5: 'Toby',
    6: 'Gene Seymour',
    7: 'Michael Phillips'
}

movies['item_ids'] = {
    1: 'Lady in the Water',
    2: 'Snakes on a Planet',
    3: 'You, Me and Dupree',
    4: 'Superman Returns',
    5: 'The Night Listener',
    6: 'Just My Luck'
}

model = MatrixPreferenceDataModel(movies['data'])
similarity = UserSimilarity(model, pearson_correlation)
recommender = UserBasedRecommender(model, similarity, with_preference=True)
print recommender.recommend(5)
示例#30
0
from scikits.crab import datasets

movies = datasets.load_sample_movies()
songs = datasets.load_sample_songs()
print movies.data
print movies.user_ids
print movies.item_ids

from scikits.crab.models import MatrixPreferenceDataModel
#Build the model
model = MatrixPreferenceDataModel(movies.data)

from scikits.crab.metrics import pearson_correlation
from scikits.crab.similarities import UserSimilarity
#Build the similarity
similarity = UserSimilarity(model, pearson_correlation)

print similarity[1]

from scikits.crab.metrics.pairwise import euclidean_distances
similarity = UserSimilarity(model, euclidean_distances)

print similarity[5]

from scikits.crab.recommenders.knn import UserBasedRecommender

#Build the User based recommender
recommender = UserBasedRecommender(model, similarity, with_preference=True)
#Recommend items for the user 5 (Toby)
recommender.recommend(5)
示例#31
0
# This code depicts the snippets appeared in Chapter 2 of the Crab's Official Tutorial:
#
#   http://muricoca.github.io/crab/tutorial.html#introducing-recommendation-engines
#

from scikits.crab import datasets
from scikits.crab.models import MatrixPreferenceDataModel
from scikits.crab.metrics import pearson_correlation
from scikits.crab.similarities import UserSimilarity
from scikits.crab.recommenders.knn import UserBasedRecommender

# input
movies = datasets.load_sample_movies()
print movies.data

# build the model
model = MatrixPreferenceDataModel(movies.data)
print model

# build the similarity
similarity = UserSimilarity(model, pearson_correlation)

# build the user based recommender
recommender = UserBasedRecommender(model, similarity, with_preference=True)

# recommend items for the user 5
result = recommender.recommend(5)
print result