<s> from __future__ import print_function <EOL> import numpy as np <EOL> from load_ml100k import get_train_test <EOL> from scipy . spatial import distance <EOL> from sklearn import metrics <EOL> from norm import NormalizePositive <EOL> def predict ( otrain ) : <EOL> binary = ( otrain > <NUM_LIT:0> ) <EOL> norm = NormalizePositive ( axis = <NUM_LIT:1> ) <EOL> train = norm . fit_transform ( otrain ) <EOL> dists = distance . pdist ( binary , '<STR_LIT>' ) <EOL> dists = distance . squareform ( dists ) <EOL> neighbors = dists . argsort ( axis = <NUM_LIT:1> ) <EOL> filled = train . copy ( ) <EOL> for u in range ( filled . shape [ <NUM_LIT:0> ] ) : <EOL> n_u = neighbors [ u , <NUM_LIT:1> : ] <EOL> for m in range ( filled . shape [ <NUM_LIT:1> ] ) : <EOL> revs = [ train [ neigh , m ] <EOL> for neigh in n_u <EOL> if binary [ neigh , m ] ] <EOL> if len ( revs ) : <EOL> n = len ( revs ) <EOL> n //= <NUM_LIT:2> <EOL> n += <NUM_LIT:1> <EOL> revs = revs [ : n ] <EOL> filled [ u , m ] = np . mean ( revs ) <EOL> return norm . inverse_transform ( filled ) <EOL> def main ( transpose_inputs = False ) : <EOL> train , test = get_train_test ( random_state = <NUM_LIT:12> ) <EOL> if transpose_inputs : <EOL> train = train . T <EOL> test = test . T <EOL> predicted = predict ( train ) <EOL> r2 = metrics . r2_score ( test [ test > <NUM_LIT:0> ] , predicted [ test > <NUM_LIT:0> ] ) <EOL> print ( '<STR_LIT>' . format ( <EOL> ( '<STR_LIT>' if transpose_inputs else '<STR_LIT:user>' ) , <EOL> r2 ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) <EOL> main ( transpose_inputs = True ) </s>
