Ejemplo n.º 1
0
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))
Ejemplo n.º 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
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
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
Ejemplo n.º 5
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
Ejemplo n.º 6
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')
Ejemplo n.º 7
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')
Ejemplo n.º 8
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)
Ejemplo n.º 9
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)
    })
    #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)
Ejemplo n.º 11
0
        'You, Me and Dupree': 3.5
    },
    'Penny Frewman': {
        'Snakes on a Plane': 4.5,
        'You, Me and Dupree': 1.0,
        'Superman Returns': 4.0
    },
    'Maria Gabriela': {}
}

model = MatrixPreferenceDataModel(movies)
boolean_model = MatrixBooleanPrefDataModel(movies)
similarity = UserSimilarity(model, euclidean_distances)
boolean_similarity = UserSimilarity(boolean_model, jaccard_coefficient)
neighborhood = NearestNeighborsStrategy()
recsys = UserBasedRecommender(model, similarity, neighborhood)
boolean_recsys = UserBasedRecommender(boolean_model, boolean_similarity,
                                      neighborhood)


def test_root_CfEvaluator_evaluate():
    """Check evaluate method in CfEvaluator """
    evaluator = CfEvaluator()

    #Test with invalid metric
    assert_raises(ValueError, evaluator.evaluate, recsys, 'rank')

    #Test with specified metric
    rmse = evaluator.evaluate(recsys, 'rmse', permutation=False)
    assert_true(rmse['rmse'] >= 0.0 and rmse['rmse'] <= 1.0)