<s> """<STR_LIT>""" <EOL> import sys <EOL> import csv <EOL> import time <EOL> import cPickle as pickle <EOL> from collections import OrderedDict , defaultdict <EOL> from matplotlib import pyplot as plt <EOL> from random import shuffle <EOL> from recommender import * <EOL> from metrics import get_ndcg_2 <EOL> from relevances import * <EOL> try : <EOL> input_file = sys . argv [ <NUM_LIT:1> ] <EOL> except IndexError : <EOL> input_file = '<STR_LIT>' <EOL> try : <EOL> output_file = sys . argv [ <NUM_LIT:2> ] <EOL> print "<STR_LIT>" , output_file <EOL> except IndexError : <EOL> output_file = '<STR_LIT>' <EOL> relevance_bias = <NUM_LIT> <EOL> ndcg_k = <NUM_LIT:50> <EOL> min_items_for_ndcg = <NUM_LIT:1> <EOL> min_users = <NUM_LIT:10> <EOL> max_train_size = <NUM_LIT:200> <EOL> def validate ( ratings ) : <EOL> global ndcgs <EOL> user_ndcgs = [ ] <EOL> max_split_i = min ( max_train_size , len ( ratings ) - min_items_for_ndcg + <NUM_LIT:1> ) <EOL> print "<STR_LIT:U+0020>" , max_split_i <EOL> tmp = ratings . items ( ) <EOL> shuffle ( tmp ) <EOL> ratings = OrderedDict ( tmp ) <EOL> for split_i in range ( <NUM_LIT:1> , max_split_i ) : <EOL> train = ratings . keys ( ) [ : split_i ] <EOL> test = ratings . keys ( ) [ split_i : ] <EOL> ranked_ids = rec . get_ranked_ids ( train , ndcg_k + len ( train ) ) <EOL> ranked_ids = [ x for x in ranked_ids if x not in train ] <EOL> ranked_ids = ranked_ids [ : ndcg_k ] <EOL> relevances = get_relevances ( ratings , ranked_ids ) <EOL> best_relevances = get_best_relevances ( ratings , test , ndcg_k ) <EOL> worst_relevances = None <EOL> ndcg = get_ndcg_2 ( relevances , best_relevances , worst_relevances , ndcg_k ) <EOL> if not np . isnan ( ndcg ) : <EOL> ndcgs [ split_i ] . append ( ndcg ) <EOL> ndcgs = defaultdict ( list ) <EOL> reader = csv . reader ( open ( input_file , '<STR_LIT:rb>' ) ) <EOL> rec = Rec ( ) <EOL> print "<STR_LIT>" , time . strftime ( "<STR_LIT>" , time . localtime ( ) ) <EOL> start_time = time . time ( ) <EOL> reader . next ( ) <EOL> line = reader . next ( ) <EOL> user = line [ <NUM_LIT:0> ] <EOL> movie = int ( line [ <NUM_LIT:1> ] ) <EOL> rating = float ( line [ <NUM_LIT:2> ] ) <EOL> current_user = user <EOL> current_ratings = OrderedDict ( ) <EOL> current_ratings [ movie ] = rating <EOL> counter = <NUM_LIT:0> <EOL> user_counter = <NUM_LIT:1> <EOL> for line in reader : <EOL> user = line [ <NUM_LIT:0> ] <EOL> movie = int ( line [ <NUM_LIT:1> ] ) <EOL> rating = float ( line [ <NUM_LIT:2> ] ) <EOL> if user == current_user : <EOL> current_ratings [ movie ] = rating <EOL> else : <EOL> if len ( current_ratings ) >= min_items_for_ndcg + <NUM_LIT:1> : <EOL> validate ( current_ratings ) <EOL> current_ratings = OrderedDict ( ) <EOL> current_ratings [ movie ] = rating <EOL> current_user = user <EOL> user_counter += <NUM_LIT:1> <EOL> counter += <NUM_LIT:1> <EOL> if counter % <NUM_LIT:1000> == <NUM_LIT:0> : <EOL> print counter , user_counter <EOL> if len ( current_ratings ) >= min_items_for_ndcg + <NUM_LIT:1> : <EOL> validate ( current_ratings ) <EOL> print "<STR_LIT>" , time . strftime ( "<STR_LIT>" , time . localtime ( ) ) <EOL> end_time = time . time ( ) <EOL> duration = end_time - start_time <EOL> duration_minutes = int ( duration ) / <NUM_LIT> <EOL> duration_seconds = int ( duration ) % <NUM_LIT> <EOL> print "<STR_LIT>" . format ( duration_minutes , duration_seconds ) <EOL> if output_file : <EOL> pickle . dump ( { '<STR_LIT>' : ndcgs } , open ( output_file , '<STR_LIT:wb>' ) ) <EOL> mean_ndcgs = OrderedDict ( { k : sum ( v ) / len ( v ) for k , v in ndcgs . items ( ) if len ( v ) >= min_users } ) <EOL> for k , v in mean_ndcgs . items ( ) [ : <NUM_LIT:20> ] : <EOL> print k , v <EOL> plt . plot ( mean_ndcgs . keys ( ) , mean_ndcgs . values ( ) ) <EOL> axes = plt . gca ( ) <EOL> axes . set_ylim ( [ <NUM_LIT:0> , <NUM_LIT:0.5> ] ) <EOL> plt . show ( ) </s>
