-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocess_ml_1m.py
155 lines (132 loc) · 4.02 KB
/
preprocess_ml_1m.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
import csv
import numpy as np
from random import shuffle
import random
import tensorflow as tf
line_id = {}
id_line = {}
movie_line = {}
id_movie = {}
id_genre = {}
u_mid_pos = {}
u_mid_pos_test = {}
u_mid_neg = {}
firstline = True
count = 0
mid_genre = {}
##Read movie id
with open("./ml-1m/movies.dat", encoding='latin-1') as f:
for line in f:
line = line.strip().split('::')
if line[1] in movie_line:
line[1] = line[1] + "--2"
print(line)
# if line[1] in movie_line:
# line[1] = line[1] + "--3"
#
# print(line)
movie_line.update({line[1]: count})
line_id.update({count: line[0]})
id_line.update({line[0]: count})
count = count + 1
##genre
mid_genre.update({line[0]: line[2].split("|")})
with open("./ml-1m/ratings.dat",encoding='latin-1') as f:
count = 0
for line in f:
line = line.strip().split("::")
mid_rating = set()
mid_rating = (id_line[line[1]], line[2])
if (float(line[2]) > 3):
u_mid_pos.setdefault(int(line[0]), []).append(mid_rating)
count += 1
elif (float(line[2]) < 3):
u_mid_neg.setdefault(int(line[0]), []).append(mid_rating)
'''
u_mid_pos has 6038 user, 575281 rates
test size is 0.1
'''
test_size = 0.1
filter_threshold = 20
count = 0
for key, value in u_mid_pos.copy().items():
if (len(value) < filter_threshold):
count += 1
del(u_mid_pos[key])
else:
shuffle(u_mid_pos[key])
print("number of delete", count)
'''
read other features
'''
user_gender = {} ## 0 is M, 1 is F
user_age = {} ## 0 - 1, 1 - 18, 2 - 25.....
user_occupation = {} ## 0-0
##### add user related information
with open("./ml-1m/users.dat",encoding='latin-1') as f:
for line in f:
line = line.strip().split("::")
if(line[1] == "M"):
user_gender.update({int(line[0]): 0})
else:
user_gender.update({int(line[0]): 1})
### update age
user_age.update({int(line[0]): int(line[2])/56})
###update ocupation
user_occupation.update({int(line[0]): int(line[3])})
user_genre = {}
for key, value in u_mid_pos.items():
genre_count = np.zeros(18)
for index in value:
id = line_id[index[0]]
genres = mid_genre[id]
for genre in genres:
if (genre == "Action"):
genre_count[0] += 1
elif (genre == "Adventure"):
genre_count[1] += 1
elif (genre == "Animation"):
genre_count[2] += 1
elif (genre == "Children's"):
genre_count[3] += 1
elif (genre == "Comedy"):
genre_count[4] += 1
elif (genre == "Crime"):
genre_count[5] += 1
elif (genre == "Documentary"):
genre_count[6] += 1
elif (genre == "Drama"):
genre_count[7] += 1
elif (genre == "Fantasy"):
genre_count[8] += 1
elif (genre == "Film-Noir"):
genre_count[9] += 1
elif (genre == "Horror"):
genre_count[10] += 1
elif (genre == "Musical"):
genre_count[11] += 1
elif (genre == "Mystery"):
genre_count[12] += 1
elif (genre == "Romance"):
genre_count[13] += 1
elif (genre == "Sci-Fi"):
genre_count[14] += 1
elif (genre == "Thriller"):
genre_count[15] += 1
elif (genre == "War"):
genre_count[16] += 1
elif (genre == "Western"):
genre_count[17] += 1
genre_count = np.divide(genre_count, np.sum(genre_count))
user_genre.update({key: genre_count})
'''
split test and training
'''
test_count = 0
for key, value in u_mid_pos.copy().items():
if(test_count<100):
del(u_mid_pos[key])
u_mid_pos_test.update({key:value})
test_count += 1
print("u_mid_pos", len(u_mid_pos))
print("end")