如何将推荐系统(python文件)集成到django项目中。

时间:2017-03-18 15:24:47

标签: django python-2.7 django-views mahout-recommender

这是我朋友制作的python脚本。如何在我的django项目中集成此文件,该项目包含从movierulz数据集中获取的所有电影列表。我应该在哪里集成此代码。

import numpy as np
import pandas as pd

# set some print options
np.set_printoptions(precision=4)
np.set_printoptions(threshold=5)
np.set_printoptions(suppress=True)
pd.set_option('precision', 3, 'notebook_repr_html', True, )
# init random gen
np.random.seed(2)



#users_file = "/media/sourabhkondapaka/Sourabh's/main_project/sandbox/ml-latest-small/ratings.csv"
#movies_file = "/media/sourabhkondapaka/Sourabh's/main_project/sandbox/ml-latest-small/movies.csv"
#users = pd.read_table(users_file,sep=',', header=None,names = ['user_id','movie_id','rating','timestamp'])
#movies = pd.read_table(movies_file, sep=',')



class popularity_based():
    def __init__(self,users,movies):
        self.users = users
        self.movies = movies
        self.user_id = None
        self.mean_ratings = None
        self.movielens= None
        self.c  = 0

    def create(self):
        self.movielens = pd.merge(users,movies)
        self.movie_ratings = self.movielens.ix[:,1:3]
        self.mean_ratings = self.movie_ratings.groupby('movie_id',as_index = True)['rating'].mean().sort_values(ascending = False)
        self.mean_ratings = pd.DataFrame(self.mean_ratings).reset_index()
        self.mean_ratings['title'] = self.mean_ratings['movie_id'].map(self.movies.set_index('movie_id')['title'])

    def recommend(self, user_id,topu): #no arguement required here, just for the sake of uniformness across other recommender implementations
        self.user_id = user_id
        #From = self.c
        #self.c += topu
        #To = self.c
        print(type(self.mean_ratings.as_matrix(columns=None)))
        return self.mean_ratings.ix[:topu,'title'].as_matrix(columns = None)

1 个答案:

答案 0 :(得分:0)

你必须问问自己,这个文件有什么作用?如果它作为一个实用程序,可以帮助您获得用户的电影推荐,那么它应该包含在主项目下的utils.py中。然后,您可以使用适当的相对导入并在右视图中提供内容。