Recommendation systems

This function will use the parameters set at the beginning of the views.py file:

nminimumrates=5
numrecs=5
recmethod = 'loglikelihood'

This defines the minimum number of movies to rate before obtaining recommendations, the number of recommendations to show to the user, and the recommendation system method respectively. To show recommendations the user can click on the Recommendations button on the top bar:

Recommendation systems

This action will trigger the movies_recs function in the views.py file (through the corresponding URL defined in the urls.py file):

def movies_recs(request):
    
    userprofile = None
    if request.user.is_superuser:
        return render_to_response(
            'books_recsys_app/superusersignin.html', RequestContext(request))
    elif request.user.is_authenticated():
        userprofile = UserProfile.objects.get(user=request.user)
    else:
        return render_to_response(
            'books_recsys_app/pleasesignin.html', RequestContext(request))
    ratedmovies=userprofile.ratedmovies.all()
    context = {}
    if len(ratedmovies)<nminimumrates:
        context['nrates'] = len(ratedmovies)
        context['nminimumrates']=nminimumrates
        return render_to_response(
            'books_recsys_app/underminimum.html', RequestContext(request, context))
            
    u_vec = np.array(userprofile.array)
    Umatrix = cache.get('umatrix')
    movieslist = cache.get('titles')
    #recommendation...
    u_rec = None
    if recmethod == 'cf_userbased':
        u_rec = CF_userbased(u_vec,numrecs,Umatrix)      
    elif recmethod == 'cf_itembased':
        cf_itembased = cache.get('cf_itembased')
        if cf_itembased == None:
            cf_itembased = CF_itembased(Umatrix)
        u_rec = cf_itembased.CalcRatings(u_vec,numrecs)        
    elif recmethod == 'loglikelihood':
        llr = cache.get('loglikelihood')
        if llr == None:
            llr = LogLikelihood(Umatrix,movieslist)
        u_rec = llr.GetRecItems(u_vec,True)
    #save last recs
    userprofile.save(recsvec=u_rec)
    context['recs'] = list(np.array(movieslist)[list(u_rec)][:numrecs])
    return render_to_response(
        'books_recsys_app/recommendations.html', 
          RequestContext(request, context))

The function will retrieve the rated movies vector from the corresponding UserProfile object and it will load the recommendation system method (specified by the recmethod parameter) from cache. The recommendations are first stored in the userprofile object and then returned to the recommendations.html page. For example, using the cf_itembased method:

Recommendation systems

This is a sample result page after rating the five movies related to the word war (see preceding screenshot). The reader can play more with the parameters and the different algorithms to evaluate the differences.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.187.210