forked from yang0110/graph-based-bandit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NMF_implement.py
99 lines (90 loc) · 3.99 KB
/
NMF_implement.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import numpy as np
from NMF_miss_value import NMF
import os
from sklearn.preprocessing import Normalizer
os.chdir('C:/Kaige_Research/Code/graph_bandit/code/')
input_path='../processed_data/movielens/'
rate_matrix=np.load(input_path+'rating_matrix_100_user_500_movies.npy')
true_payoffs=rate_matrix/np.max(rate_matrix)
mask=true_payoffs!=0
np.save(input_path+'normed_rating_matrix_100_user_500_movies.npy', true_payoffs)
np.save(input_path+'rating_matrix_mask_100_user_500_movies.npy', mask)
true_payoffs[true_payoffs==0]=np.nan
W, H, error=NMF(true_payoffs, 10)
print(W.shape)
print(H.shape)
W=Normalizer().fit_transform(W)
H=Normalizer().fit_transform(H.T)
np.save(input_path+'user_feature_matrix_100.npy', W)
np.save(input_path+'item_feature_matrix_500.npy', H)
# input_path='../processed_data/lastfm/'
# rate_matrix=np.load(input_path+'rating_matrix_100_user_500_artist.npy')
# true_payoffs=rate_matrix/np.max(rate_matrix)
# np.save(input_path+'normed_rating_matrix_100_user_500_artist.npy', true_payoffs)
# mask=true_payoffs!=0
# np.save(input_path+'rating_matrix_mask_100_user_500_artist.npy', mask)
# true_payoffs[true_payoffs==0]=np.nan
# W, H, error=NMF(true_payoffs, 10)
# print(W.shape)
# print(H.shape)
# W=Normalizer().fit_transform(W)
# H=Normalizer().fit_transform(H.T)
# np.save(input_path+'user_feature_matrix_100.npy', W)
# np.save(input_path+'item_feature_matrix_500.npy', H)
# input_path='../processed_data/delicious/'
# rate_matrix=np.load(input_path+'rating_matrix_100_user_500_bookmark.npy')
# true_payoffs=rate_matrix/np.max(rate_matrix)
# np.save(input_path+'normed_rating_matrix_100_user_500_bookmark.npy', true_payoffs)
# mask=true_payoffs!=0
# np.save(input_path+'rating_matrix_mask_100_user_500_bookmark.npy', mask)
# true_payoffs[true_payoffs==0]=np.nan
# W, H, error=NMF(true_payoffs, 10)
# print(W.shape)
# print(H.shape)
# W=Normalizer().fit_transform(W)
# H=Normalizer().fit_transform(H.T)
# np.save(input_path+'user_feature_matrix_100.npy', W)
# np.save(input_path+'item_feature_matrix_500.npy', H)
# input_path='../processed_data/netflix/'
# rate_matrix=np.load(input_path+'rating_matrix_100_user_500_movies.npy')
# true_payoffs=rate_matrix/np.max(rate_matrix)
# np.save(input_path+'normed_rating_matrix_100_user_500_movies.npy', true_payoffs)
# mask=true_payoffs!=0
# np.save(input_path+'rating_matrix_mask_100_user_500_movies.npy', mask)
# true_payoffs[true_payoffs==0]=np.nan
# W, H, error=NMF(true_payoffs, 10)
# print(W.shape)
# print(H.shape)
# W=Normalizer().fit_transform(W)
# H=Normalizer().fit_transform(H.T)
# np.save(input_path+'user_feature_matrix_100.npy', W)
# np.save(input_path+'item_feature_matrix_500.npy', H)
#
# input_path='../processed_data/lastfm/'
# rate_matrix=np.load(input_path+'rating_matrix_100_user_500_artist.npy')
# true_payoffs=rate_matrix/np.max(rate_matrix)
# true_payoffs[true_payoffs!=0]=1.0
# mask=true_payoffs!=0
# np.save(input_path+'binary_rating_matrix_100_user_500_artist.npy', true_payoffs)
# np.save(input_path+'binary_rating_mask_100_user_500_artist.npy', true_payoffs)
# W, H, error=NMF(true_payoffs, 10)
# print(W.shape)
# print(H.shape)
# W=Normalizer().fit_transform(W)
# H=Normalizer().fit_transform(H.T)
# np.save(input_path+'binary_payoff_user_feature_matrix_100.npy', W)
# np.save(input_path+'binary_payoff_item_feature_matrix_500.npy', H)
input_path='../processed_data/delicious/'
rate_matrix=np.load(input_path+'rating_matrix_100_user_500_bookmark.npy')
true_payoffs=rate_matrix/np.max(rate_matrix)
true_payoffs[true_payoffs!=0]=1.0
mask=true_payoffs!=0
np.save(input_path+'binary_rating_matrix_100_user_500_bookmark.npy', true_payoffs)
np.save(input_path+'binary_rating_mask_100_user_500_bookmark.npy', true_payoffs)
W, H, error=NMF(true_payoffs, 10)
print(W.shape)
print(H.shape)
W=Normalizer().fit_transform(W)
H=Normalizer().fit_transform(H.T)
np.save(input_path+'binary_payoff_user_feature_matrix_100.npy', W)
np.save(input_path+'binary_payoff_item_feature_matrix_500.npy', H)