-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
66 lines (52 loc) · 1.89 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import os
import math
# In[2]:
# root mean squared error
def RMSE(user_item, users, movies, regular_term):
total = 0
count = 0
for user in user_item:
for movie, score in user_item[user]:
movie = int(movie)
user = int(user)
square = pow(score - np.dot(users[user, :], np.transpose(movies[movie, :])), 2)
regular = np.sum(regular_term *(np.power(users[user, :], 2) + np.power(movies[movie, :], 2)))
total = total + square + regular
count = count + 1
return math.sqrt(total / count)
def load_files(user_count, movie_count, latent_dim):
files = []
if os.path.isfile('./model/latent_user.npy'):
fm_users = np.load('./model/latent_user.npy')
files.append(fm_users)
else:
files.append(np.random.rand(user_count, latent_dim))
if os.path.isfile('./model/latent_movie.npy'):
fm_movies = np.load('./model/latent_movie.npy')
files.append(fm_movies)
else:
files.append(np.random.rand(movie_count, latent_dim))
if os.path.isfile('./model/train_error.npy'):
train_error = np.load('./model/train_error.npy')
files.append(train_error)
else:
files.append([])
return files
def save_files(fm_users, fm_movies, train_error):
np.save('./model/latent_user.npy', fm_users)
np.save('./model/latent_movie.npy', fm_movies)
np.save('./model/train_error.npy', train_error)
# with open('./model/userMapper.json', 'w') as f:
# json.dump(userMapper, f)
# with open('./model/movieMapper.json', 'w') as f:
# json.dump(movieMapper, f)
# user_item_json = {}
# for key in user_item:
# user_item_json[key] = user_item[key].tolist()
# with open('./model/user_item.json', 'w') as f:
# json.dump(user_item_json, f)
# In[ ]: