-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_svm_imdb.py
executable file
·403 lines (334 loc) · 12.4 KB
/
test_svm_imdb.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/python
'''
train-imdb.py
This script takes a text file containing a movieID on each line and trains
a Naive Bayes classifier by generating and pickling log probabilities in
the current directory.
Copyright 2013 Dan Cocuzzo <cocuzzo@cs.stanford.edu>
Stephen Wu <shw@stanford.edu>
'''
import sys, os
import imdb
import cPickle as pickle
from math import log
import ConfigParser
from imdbutils import hydrate, mpaa_to_label
import numpy as np
from sklearn.svm import SVC
#debugger
import pdb
MOVIE_FILE = sys.argv[1]
OUTPUT_DIR = 'results_svm/'
cfg = ConfigParser.ConfigParser()
cfg.read('params.cfg')
MAX_ACTORS = eval(cfg.get('Misc', 'MAX_ACTORS'))
BINS_RATING = eval(cfg.get('OutputLabels', 'BINS_RATING'))
BINS_BMULT = eval(cfg.get('OutputLabels', 'BINS_BMULT'))
NUM_RATING_BINS = len(BINS_RATING)
NUM_BMULT_BINS = len(BINS_BMULT)
# db.cfg should contain a uri of the form:
# sqlite:///absolute/path/to/imdb.db
cfg = ConfigParser.ConfigParser()
cfg.read('db.cfg')
db_uri = cfg.get('URI', 'IMDB_URI')
'''
guarantees on movies:
- must be a movie
- must have a rating
- must not be an adult movie
- must have a 'business' section, and a 'budget' section within that
- must have 'gross' within 'business'
- must have 'opening weekend'
'''
sys.stdout.write('Initializing... ')
sys.stdout.flush()
actual_rating = []
predicted_rating = []
actual_bmult = []
predicted_bmult = []
mids = []
sys.stdout.write('[done]\n')
sys.stdout.write('Loading model... ')
sys.stdout.flush()
fid = open('svmmodel/budget.dat', 'r')
MAX_BUDGET = int(fid.readline().strip())
fid.close()
fid = open('svmmodel/svm_bmult_model.pkl', 'rb')
bmult_model = pickle.load(fid)
fid.close()
fid = open('svmmodel/svm_rating_model.pkl', 'rb')
rating_model = pickle.load(fid)
fid.close()
'''
fid = open('staging/person_fvid.pkl', 'rb')
person_fvid = pickle.load(fid)
fid.close()
fid = open('staging/distro_fvid.pkl', 'rb')
distro_fvid = pickle.load(fid)
fid.close()
'''
fid = open('staging/genre_fvid.pkl', 'rb')
genre_fvid = pickle.load(fid)
fid.close()
# Feature vectoor is of the form:
# [ genres, mpaa, budget, pIDs? ] (extensible)
#NUM_PERSONS = len(person_fvid)
#NUM_DISTROS = len(distro_fvid)
NUM_GENRES = len(genre_fvid)
#PERSON_OFFSET = 0
#DISTRO_OFFSET = PERSON_OFFSET + NUM_PERSONS
#GENRE_OFFSET = DISTRO_OFFSET + NUM_DISTROS
GENRE_OFFSET = 0
MPAA_OFFSET = GENRE_OFFSET + NUM_GENRES
BUDGET_OFFSET = MPAA_OFFSET + 1
FV_LENGTH = NUM_GENRES + 2
sys.stdout.write('[done]\n')
sys.stdout.write('Loading imdb.db... ')
sys.stdout.flush()
ia = imdb.IMDb('sql', uri=db_uri)
sys.stdout.write('[done]\n')
# all pruning will be done in movielist
mlist = open(MOVIE_FILE, 'r')
mov_id = mlist.readline().strip()
max_budget = 0 # for normalization purposes
while mov_id != '':
sys.stdout.write('Predicting movie #' + mov_id + ': ')
sys.stdout.flush()
mids.append(mov_id)
movie = hydrate(mov_id, ia, MAX_ACTORS)
sys.stdout.write(movie['title'])
sys.stdout.flush()
# initialize feature vector
current_fv = [0]*FV_LENGTH
# generate actual outputs
actual_rating.append(BINS_RATING.index(movie['rating']))
actual_bmult.append(BINS_BMULT.index(movie['bmult']))
# Populate feature vector
'''
for actor_id in iter(movie['actor']):
current_fv[PERSON_OFFSET + person_fvid[actor_id]] = 1
for director_id in iter(movie['director']):
current_fv[PERSON_OFFSET + person_fvid[director_id]] = 1
for producer_id in iter(movie['producer']):
current_fv[PERSON_OFFSET + person_fvid[producer_id]] = 1
for composer_id in iter(movie['composer']):
current_fv[PERSON_OFFSET + person_fvid[composer_id]] = 1
for cinetog_id in iter(movie['cinetog']):
current_fv[PERSON_OFFSET + person_fvid[cinetog_id]] = 1
for distro_id in iter(movie['distro']):
current_fv[DISTRO_OFFSET + distro_fvid[distro_id]] = 1
'''
for genre in iter(movie['genre']):
current_fv[GENRE_OFFSET + genre_fvid[genre]] = 1
for mpaa in iter(movie['mpaa']):
current_fv[MPAA_OFFSET] = float(mpaa_to_label(mpaa)) / 4 # to normalize
current_fv[BUDGET_OFFSET] = float(movie['budget']) / MAX_BUDGET
predicted_rating.append(rating_model.predict(current_fv)[0])
predicted_bmult.append(bmult_model.predict(current_fv)[0])
mov_id = mlist.readline().strip()
sys.stdout.write(' [done]\n')
rating_sq_err = 0
bmult_sq_err = 0
rating_abs_err = 0
bmult_abs_err = 0
rating_sum_err = 0
bmult_sum_err = 0
num_rating_errors = 0
num_bmult_errors = 0
test_size = len(actual_rating)
# error_rating = predicted_rating
# error_bmult = predicted_bmult
hist_rating_true = []
hist_rating_pred = []
hist_rating_dist = []
hist_bmult_true = []
hist_bmult_pred = []
hist_bmult_dist = []
# pdb.set_trace()
f_rating = open(OUTPUT_DIR+'rating.out', 'w')
f_bmult = open(OUTPUT_DIR+'bmult.out', 'w')
for i in xrange(test_size):
err_r = predicted_rating[i] - actual_rating[i]
err_b = predicted_bmult[i] - actual_bmult[i]
# error_rating[i] = err_r
# error_bmult[i] = err_b
if err_r != 0:
num_rating_errors += 1
if err_b != 0:
num_bmult_errors += 1
rating_sq_err += pow(err_r, 2)
bmult_sq_err += pow(err_b, 2)
rating_abs_err += abs(err_r)
bmult_abs_err += abs(err_b)
rating_sum_err += err_r
bmult_sum_err += err_b
result_rating = 'PASS' if (err_r == 0) else 'FAIL'
result_bmult = 'PASS' if (err_b == 0) else 'FAIL'
f_rating.write('%s|%s|%s|%s|%s\n' % (mids[i], result_rating, actual_rating[i], predicted_rating[i], abs(err_r)))
f_bmult.write('%s|%s|%s|%s|%s\n' % (mids[i], result_bmult, actual_bmult[i], predicted_bmult[i], abs(err_b)))
f_rating.close()
f_bmult.close()
# collect histogram stats
hist_rating_true = actual_rating
hist_rating_pred = predicted_rating
hist_rating_dist = [abs(x-y) for x,y in zip(hist_rating_true, hist_rating_pred)]
hist_bmult_true = actual_bmult
hist_bmult_pred = predicted_bmult
hist_bmult_dist = [abs(x-y) for x,y in zip(hist_bmult_true, hist_bmult_pred)]
avg_abs_rating_err = float(rating_abs_err) / test_size
avg_abs_bmult_err = float(bmult_abs_err) / test_size
avg_sq_rating_err = float(rating_sq_err) / test_size
avg_sq_bmult_err = float(bmult_sq_err) / test_size
abs_rating_err_variance = avg_sq_rating_err - pow(avg_abs_rating_err, 2)
abs_bmult_err_variance = avg_sq_bmult_err - pow(avg_abs_bmult_err, 2)
avg_rating_err = float(rating_sum_err) / test_size
avg_bmult_err = float(bmult_sum_err) / test_size
rating_err_variance = avg_sq_rating_err - pow(avg_rating_err, 2)
bmult_err_variance = avg_sq_bmult_err - pow(avg_bmult_err, 2)
stats_file = open(OUTPUT_DIR + 'stats.out', 'w')
stats_file.write('test_size=' + str(test_size) + '\n')
stats_file.write('error_rating=' + str(float(num_rating_errors) / test_size) + '\n')
stats_file.write('error_bmult=' + str(float(num_bmult_errors) / test_size) + '\n')
stats_file.write('diff_rating=' + str(avg_abs_rating_err) + '\n')
stats_file.write('diff_bmult=' + str(avg_abs_bmult_err) + '\n')
stats_file.write('sqdiff_rating=' + str(avg_sq_rating_err) + '\n')
stats_file.write('sqdiff_bmult=' + str(avg_sq_bmult_err) + '\n')
stats_file.write('abs_rating_err_variance=' + str(abs_rating_err_variance) + '\n')
stats_file.write('abs_bmult_err_variance=' + str(abs_bmult_err_variance) + '\n')
stats_file.write('rating_err_variance=' + str(rating_err_variance) + '\n')
stats_file.write('bmult_err_variance=' + str(bmult_err_variance) + '\n')
stats_file.close()
sys.stdout.write('test_size=' + str(test_size) + '\n')
sys.stdout.write('error_rating=' + str(float(num_rating_errors) / test_size) + '\n')
sys.stdout.write('error_bmult=' + str(float(num_bmult_errors) / test_size) + '\n')
sys.stdout.write('diff_rating=' + str(avg_abs_rating_err) + '\n')
sys.stdout.write('diff_bmult=' + str(avg_abs_bmult_err) + '\n')
sys.stdout.write('sqdiff_rating=' + str(avg_sq_rating_err) + '\n')
sys.stdout.write('sqdiff_bmult=' + str(avg_sq_bmult_err) + '\n')
sys.stdout.write('abs_rating_err_variance=' + str(abs_rating_err_variance) + '\n')
sys.stdout.write('abs_bmult_err_variance=' + str(abs_bmult_err_variance) + '\n')
sys.stdout.write('rating_err_variance=' + str(rating_err_variance) + '\n')
sys.stdout.write('bmult_err_variance=' + str(bmult_err_variance) + '\n')
################################################
################################################
################################################
################################################
# generate histograms for experimental clarity
import matplotlib.pyplot as plt
import numpy as np
FIGURES_DIR = OUTPUT_DIR + 'figures'
try:
os.mkdir(FIGURES_DIR)
except:
pass
# pdb.set_trace()
############################################
## RATING PLOTS
############################################
# rating_titles = ['hist_rating_true', 'hist_rating_pred', 'hist_rating_dist']
# rating_distributions = ['hist_rating_true', 'hist_rating_pred', 'hist_rating_dist']
# colors = ['g','b','r']
# visualize true ratings in a histogram
# x = map(int, hist_rating_true)
x = hist_rating_true
b = [b-0.5 for b in range(len(BINS_RATING)+1)]
n, bins, patches = plt.hist(x, bins=b, alpha=0.75, normed=True)
# pdb.set_trace()
plt.xlabel('Rating')
plt.ylabel('Frequency')
plt.title('Histogram of True Movie Ratings')
plt.xlim(-0.5,10.5)
a = plt.gca()
a.set_xticks(map(int,BINS_RATING))
plt.grid(True)
fig_path = os.path.join(FIGURES_DIR, 'hist_rating_true.png')
plt.savefig(fig_path, bbox_inches='tight')
plt.close()
# visualize true and predicted ratings in a histogram
# x1 = map(int, hist_rating_true)
# x2 = map(int, hist_rating_pred)
x1 = hist_rating_true
x2 = hist_rating_pred
x = [x1,x2]
b = [b-0.5 for b in range(len(BINS_RATING)+1)]
n, bins, patches = plt.hist(x, bins=b, alpha=0.75)
# pdb.set_trace()
plt.xlabel('Rating')
plt.ylabel('Frequency')
plt.title('Histogram of Movie Ratings')
plt.xlim(-0.5,10.5)
a = plt.gca()
a.set_xticks(map(int,BINS_RATING))
plt.grid(True)
fig_path = os.path.join(FIGURES_DIR, 'hist_rating_compare.png')
plt.savefig(fig_path, bbox_inches='tight')
plt.close()
# visualize the difference in predictions in a histogram
# x = map(int, hist_rating_dist)
x = hist_rating_dist
b = [b-0.5 for b in range(len(BINS_RATING)+1)]
n, bins, patches = plt.hist(x, bins=b, facecolor='r', alpha=0.75)
plt.xlabel('|Rating{true} - Rating{pred}|')
plt.ylabel('Frequency')
plt.title('Histogram of |Rating{true} - Rating{pred}|')
plt.xlim(-0.5,10.5)
a = plt.gca()
a.set_xticks(map(int,BINS_RATING))
plt.grid(True)
fig_path = os.path.join(FIGURES_DIR, 'hist_rating_dist.png')
plt.savefig(fig_path, bbox_inches='tight')
plt.close()
############################################
## BUDGET-MULT PLOTS
############################################
# visualize true bmults in a histogram
# x = [BINS_BMULT.index(v) for v in hist_bmult_true]
x = hist_bmult_true
b = [b-0.5 for b in range(len(BINS_BMULT)+1)]
n, bins, patches = plt.hist(x, bins=b, alpha=0.75, normed=True)
plt.xlabel('Budget-Multiplier')
plt.ylabel('Frequency')
plt.title('Histogram of True Budget-Multipliers')
plt.xlim(-0.5, len(BINS_BMULT)+0.5)
a = plt.gca()
a.set_xticks(range(len(BINS_BMULT)))
a.set_xticklabels(BINS_BMULT)
plt.grid(True)
fig_path = os.path.join(FIGURES_DIR, 'hist_bmult_true.png')
plt.savefig(fig_path, bbox_inches='tight')
plt.close()
# visualize true and predicted bmults in a histogram
# x1 = [BINS_BMULT.index(v) for v in hist_bmult_true]
# x2 = [BINS_BMULT.index(v) for v in hist_bmult_pred]
x1 = hist_bmult_true
x2 = hist_bmult_pred
x = [x1, x2]
b = [b-0.5 for b in range(len(BINS_BMULT)+1)]
n, bins, patches = plt.hist(x, bins=b, alpha=0.75)
plt.xlabel('Budget-Multiplier')
plt.ylabel('Frequency')
plt.title('Histogram of Budget Multipliers')
plt.xlim(-0.5, len(BINS_BMULT)+0.5)
a = plt.gca()
a.set_xticks(range(len(BINS_BMULT)))
a.set_xticklabels(BINS_BMULT)
plt.grid(True)
fig_path = os.path.join(FIGURES_DIR, 'hist_bmult_compare.png')
plt.savefig(fig_path, bbox_inches='tight')
plt.close()
# visualize the difference in predictions in a histogram
# x = map(int, hist_bmult_dist)
x = hist_bmult_dist
b = [b-0.5 for b in range(len(BINS_RATING)+1)]
n, bins, patches = plt.hist(x, bins=b, facecolor='r', alpha=0.75)
plt.xlabel('|BMult{true} - BMult{pred}|')
plt.ylabel('Frequency')
plt.title('Histogram of |BMult{true} - BMult{pred}|')
plt.xlim(-0.5,len(BINS_BMULT)-0.5)
a = plt.gca()
a.set_xticks(range(len(BINS_BMULT)))
# a.set_xticklabels(BINS_BMULT)
plt.grid(True)
fig_path = os.path.join(FIGURES_DIR, 'hist_bmult_dist.png')
plt.savefig(fig_path, bbox_inches='tight')
plt.close()