本文介绍了如何将推荐系统(python文件)集成到django项目中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我朋友制作的python脚本。如何将该文件整合到我的django项目中,该项目包含从movierulz数据集中获取的所有电影列表。
This is the python script made by my friend .How to integrate this file in my django project which contains all list of movies taken from the movierulz data set.Where should I integrate this code.
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)
推荐答案
你必须问问自己,这个文件做什么?如果它用作一个可以帮助您为用户获取电影建议的实用程序,那么它应该包含在主项目下的 utils.py
中。然后,您可以使用适当的相关导入,并在正确的视图中提供内容。
You have to ask yourself, what does this file do? If it functions as a utility that helps you get movie recommendations for a user, then it should be included in your utils.py
under the main project. You can then use appropriate relative import and serve the content in the right view.
这篇关于如何将推荐系统(python文件)集成到django项目中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!