-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_preprocess_movielens.py
164 lines (127 loc) · 7.47 KB
/
1_preprocess_movielens.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import itertools
import numpy as np
import pandas as pd
from surprise import Dataset, Reader, SVD, accuracy
from surprise.model_selection import train_test_split
from config.cofig import PROJECT_DIR
THRESHOLD = None
MOVIELENS_NUMBER_OF_ACTIONS = 1000
def movie_preprocessing(movie):
movie_col = list(movie.columns)
movie_tag = [doc.split('|') for doc in movie['tag']]
tag_table = {token: idx for idx, token in enumerate(set(itertools.chain.from_iterable(movie_tag)))}
movie_tag = pd.DataFrame(movie_tag)
tag_table = pd.DataFrame(tag_table.items())
tag_table.columns = ['Tag', 'Index']
# use one-hot encoding for movie genres (here called tag)
tag_dummy = np.zeros([len(movie), len(tag_table)])
for i in range(len(movie)):
for j in range(len(tag_table)):
if tag_table['Tag'][j] in list(movie_tag.iloc[i, :]):
tag_dummy[i, j] = 1
# combine the tag_dummy one-hot encoding table to original movie files
movie = pd.concat([movie, pd.DataFrame(tag_dummy)], axis=1)
movie_col.extend(['tag' + str(i) for i in range(len(tag_table))])
movie.columns = movie_col
movie = movie.drop('tag', axis='columns')
return movie
def feature_extraction(data):
# actions: we use top MOVIELENS_NUMBER_OF_ACTIONS movies as our actions for recommendations
actions = data.groupby('movie_id').size().sort_values(ascending=False)[:MOVIELENS_NUMBER_OF_ACTIONS]
actions = list(actions.index)
# user_feature: tags they've watched for non-top-1000 movies normalized per user
user_feature = data[~data['movie_id'].isin(actions)]
user_feature = user_feature.groupby('user_id').aggregate(np.sum)
user_feature = user_feature.drop(['movie_id', 'rating', 'timestamp', 'movie_name'], axis='columns')
user_feature = user_feature.div(user_feature.sum(axis=1), axis=0)
# user_stream: the result for testing bandit algrorithms
# Only consider users that have watched some movies from the considered actions.
top50_data = data[data['movie_id'].isin(actions)]
top50_data = top50_data.sort_values('timestamp', ascending=1)
user_stream = top50_data[["user_id", "timestamp"]]
# Only use users with features.
user_stream = user_stream[user_stream.user_id.isin(set(user_feature.index))]
users_all_exp = user_stream.user_id[:150000].unique()
print(f"---\nThere are {len(users_all_exp)} unique users in the experiment\n---")
# reward_list: if rating >=3, the user will watch the movie
if THRESHOLD is not None:
top50_data['reward'] = np.where(top50_data['rating'] >= THRESHOLD, 1, 0)
else:
top50_data['reward'] = np.where(top50_data['rating'] >= 0, 1, 0)
top50_data = top50_data.rename(columns={'movie_id': "item_id"})
reward_list = top50_data[['user_id', 'item_id', 'reward']]
reward_list = reward_list[reward_list['reward'] == 1]
# Ratings are computed from reward because we use implicit feedback. So NDCG is also computed with 0-1 reward values.
ratings_list = top50_data[["user_id", "item_id", "reward"]]
return user_stream, user_feature, actions, reward_list, ratings_list
def main_data():
print("reading and preprocessing the movie data")
movie = pd.read_table(f'{PROJECT_DIR}/dataset/movielens/movies.dat', sep='::', names=['movie_id', 'movie_name', 'tag'], engine='python')
movie = movie_preprocessing(movie)
print("reading the ratings data and merge it with movie data")
rating = pd.read_table(f"{PROJECT_DIR}/dataset/movielens/ratings.dat", sep="::",
names=["user_id", "movie_id", "rating", "timestamp"], engine='python')
data = pd.merge(rating, movie, on="movie_id")
print("extracting feature from our data set")
user_stream, true_user_features, actions, reward_list, ratings_list = feature_extraction(data)
true_user_features.to_csv(f"{PROJECT_DIR}/dataset/movielens/true_user_features.csv", sep='\t')
pd.DataFrame(actions, columns=['item_id']).to_csv(f"{PROJECT_DIR}/dataset/movielens/actions.csv", sep='\t', index=False)
if THRESHOLD is not None:
reward_list.to_csv(f"{PROJECT_DIR}/dataset/movielens/reward_list_{THRESHOLD}.csv", sep='\t', index=False)
else:
reward_list.to_csv(f"{PROJECT_DIR}/dataset/movielens/reward_list.csv", sep='\t', index=False)
ratings_list.to_csv(f"{PROJECT_DIR}/dataset/movielens/ratings_list.csv", sep='\t', index=False)
action_context = movie[movie['movie_id'].isin(actions)]
action_context.to_csv(f"{PROJECT_DIR}/dataset/movielens/action_context.csv", sep='\t', index = False)
movie.to_csv(f"{PROJECT_DIR}/dataset/movielens/movie.csv", sep='\t', index=False)
ratings_df = pd.read_table(f"{PROJECT_DIR}/dataset/movielens/ratings.dat", sep="::",
names=['UserID', 'MovieID', 'Rating', 'Timestamp'], engine='python')
ratings_df['Rating'].unique()
print("instantiating a reader and reading in our rating data")
reader = Reader(rating_scale=(0.5, 5))
data = Dataset.load_from_df(ratings_df[['UserID', 'MovieID', 'Rating']], reader)
# train SVD on 75% of known rates")
print(f"training SVD on {len(data.raw_ratings)} ratings")
trainset, testset = train_test_split(data, test_size=.25)
svd = SVD(n_factors=100)
svd.fit(trainset)
idx_item = []
for i in range(trainset.n_items):
idx_item.append(trainset.to_raw_iid(i))
# SVD returns memory-views (cython), hence asarray calls.
pu_all, qi_all = np.asarray(svd.pu), np.asarray(svd.qi)
bu_all, bi_all = np.asarray(svd.bu), np.asarray(svd.bi)
print("Saving the features and biases")
action_features = pd.DataFrame(data=qi_all)
action_features.insert(loc=0, column='item_id', value=idx_item) # action_features["MovieID"] = idx
action_features.to_csv(f"{PROJECT_DIR}/dataset/movielens/action_features.csv", index=False)
action_biases = pd.DataFrame(data=bi_all)
action_biases.insert(loc=0, column='item_id', value=idx_item)
action_biases.to_csv(f"{PROJECT_DIR}/dataset/movielens/action_biases.csv", index=False)
# Users
print("Saving user features and biases")
idx_user = []
for i in range(trainset.n_users):
idx_user.append(trainset.to_raw_uid(i))
idx_user_int = [int(item) for item in idx_user]
user_features = pd.DataFrame(data=pu_all)
user_features.insert(0, 'user_id', idx_user_int)
# Only save user features for those users that are present in the experiment.
user_features = user_features[user_features.user_id.isin(set(user_stream["user_id"]))]
print(f"#users in user_features: {len(user_features)}")
user_features.to_csv(f"{PROJECT_DIR}/dataset/movielens/user_features.csv", index=False)
users_that_have_features_set = set(idx_user)
# Only save users with features for the experiment.
user_stream = user_stream[user_stream.user_id.isin(users_that_have_features_set)]
print(f"#users in user_stream: {len(user_stream)}")
user_stream.to_csv(f"{PROJECT_DIR}/dataset/movielens/user_stream.csv", sep='\t', index=False)
user_biases = pd.DataFrame(data=bu_all)
user_biases.insert(loc=0, column='user_id', value=idx_user_int)
# Only save user biases for those users that are present in the experiment.
user_biases = user_biases[user_biases.user_id.isin(set(user_stream["user_id"]))]
print(f"#users in user_biases: {len(user_biases)}")
user_biases.to_csv(f"{PROJECT_DIR}/dataset/movielens/user_biases.csv", index=False)
if __name__ == '__main__':
# for t in list(map(lambda x: x / 2, range(1, 11))):
# THRESHOLD = t
main_data()