Untitled
unknown
plain_text
a year ago
2.4 kB
0
Indexable
Never
def get_recommended_jobs(user,heta=0.001,K=20, precision=0.00001, per_view_score = 5., number_of_recommendations = 20): users = User.query.all() user_count = User.query.count() #its faster to reconstruct the query #map id to matrix index user_id_to_index_map = {} index_to_user_id_map = {} for i in range(user_count): user_id_to_index_map.update({users[i].id:i}) index_to_user_id_map.update({i:users[i].id}) job_postings = JobPostings.query.all() job_postings_count = JobPostings.query.count() #the recommender system is useful only if it can pick out a small portion of the total number of job postings if number_of_recommendations > job_postings_count/3 + 1: number_of_recommendations = job_postings_count/3 + 1 #map job_id to matrix index job_id_to_index_map = {} index_to_job_id_map = {} for i in range(job_postings_count): job_id_to_index_map.update({job_postings[i].id:i}) index_to_job_id_map.update({i:job_postings[i].id}) print(user_count,job_postings_count) #build the matrix for known job posting views and the list of known indexes X = np.zeros( (user_count,job_postings_count) ) visits = db.session.query(user_visited_job_posting).all() known_indexes = [] for (uid,jid) in visits: X[ user_id_to_index_map[uid], job_id_to_index_map[jid] ] = per_view_score known_indexes.append( (user_id_to_index_map[uid], job_id_to_index_map[jid]) ) #generate ranom V,F matrices V = per_view_score * np.random.rand(user_count,K) F = per_view_score * np.random.rand(K,job_postings_count) (V,F) = MF_RS(X,V,F,known_indexes,heta,K,precision) X_ = V @ F #get recommendations for the user recomendations = [] user_index = user_id_to_index_map[user.id] #recommend the first number_of_recommendations jobs that have a positive rating for i in range(number_of_recommendations): job_index = np.argmax(X_[user_index,:]) if X_[user_index,job_index] > 0 : if job_postings[job_index].active is True and job_postings[job_index].creator_id != user.id: recomendations.append( job_postings[ job_index ] ) #don't pick that job posting again X_[user_index,job_index] = -1. else: break return recomendations