在做机器学习算法和推荐系统时,往往都会遇到模型加载的问题,今天的例子就是,如何在django项目启动时将数据和模型加载到内存中,后续各个模块的调用就不需要重复的加载了,提高页面加载的速度,那么将数据或者模型加载至内存的方式如下,本章以一篇推荐系统的模型和数据的加载为例:
1、第一步:在应用模块下(我的引用名称叫做index,项目名称叫做music)app.py文件中编写加载数据或者模型的代码,代码如下:
class IndexConfig(AppConfig):
name = 'index'
def ready(self) -> None:
global data, user_songs, songID_titles, songs, knn_usercf
data = pd.read_csv(r'E:/Graduation-Porject/RecommendedSystem/MusicDataset/music_data.csv')
user_item_rating = pd.read_csv(r'E:/Graduation-Porject/RecommendedSystem/MusicDataset/music_user_item_rating.csv')
# 用户听过的歌曲集合
user_songs = {}
for user, group in user_item_rating.groupby('user'):
user_songs[user] = group['item'].values.tolist()
# 歌曲集合
songs = user_item_rating['item'].unique().tolist()
# 加载userCF模型
dump_file_path = 'E:/Graduation-Porject/GraduationCode/algorithm/knn_usercf.model'
_, knn_usercf = dump.load(dump_file_path)
# 歌曲id和歌曲名称对应关系
songID_titles = {}
for index in data.index:
songID_titles[data.loc[index, 'song']] = data.loc[index, 'title']
简单的介绍以下:基本的原理就是将变量名赋值成全局变量,整个项目都可以调用这个变量。
第二步:在其他模块下调用,这里我的案例是:调用我的推荐算法模型为登录的用户推荐他可能喜欢的歌曲。
# 加载启动内存中的全局变量到视图函数中,这里时views.py文件中
from index.apps import user_songs, songID_titles, songs, knn_usercf, data
def indexview(request):
# 热搜歌曲
search_song = Dynamic.objects.select_related('song').order_by('-dynamic_search').all()[:20]
# 音乐分类
label_list = Label.objects.all()
# 热门歌曲
play_hot_song = Dynamic.objects.select_related('song').order_by('-dynamic_plays').all()[:10]
down_ranking = Dynamic.objects.select_related('song').order_by('-dynamic_down').all()[:12]
current_user = str(request.user)
if not current_user.isnumeric():
user = 'reUser'
# 判定是否登录用户
if current_user == "AnonymousUser" or user == 'reUser':
# 基于榜单推荐
guess_like = recommendation_basedonPopularity(data, N=15)
guess_like = Song.objects.filter(song_name__in=guess_like)[:15]
search_ranking = search_song[:12]
daily_recommendation = guess_like[:3]
all_ranking = [guess_like[4:], search_ranking, down_ranking]
context = {
'daily_recommendation': daily_recommendation,
"all_ranking": all_ranking,
"play_hot_song": play_hot_song,
"label_list": label_list,
}
return render(request, 'index.html', context)
# 判断登录用户
elif current_user.isnumeric():
current_user = int(current_user)
current_user = int(current_user)
# 协同过滤算法,为当前用户推荐他可能喜欢的音乐歌曲
result = recommendation_basedonUserCF(current_user, user_songs,songID_titles, songs,knn_usercf,N=15)
# 查询数据
daily_recommendation = Song.objects.filter(song_name__in=result)
# 热门搜索、热门下载
guess_like = daily_recommendation[4:15]
search_ranking = search_song[:12]
all_ranking = [guess_like, down_ranking, search_ranking]
context = {
'daily_recommendation': daily_recommendation[:3],
"all_ranking": all_ranking,
"play_hot_song": play_hot_song,
"label_list": label_list,
}
return render(request, 'index.html', context)