<s> import numpy as np <EOL> import os <EOL> import sys <EOL> sys . path . append ( os . path . abspath ( os . path . dirname ( os . path . dirname ( __file__ ) ) ) ) <EOL> from utils . data_io import load_numpy_array_from_file <EOL> from utils . data_paths import DATA_DIR_PATH <EOL> from utils . constants import RATING_INDEX <EOL> PROBE_DATA_FILE_PATH = os . path . join ( DATA_DIR_PATH , '<STR_LIT>' ) <EOL> def main ( ) : <EOL> probe_predictions = get_probe_predictions ( ) <EOL> probe = get_probe ( ) <EOL> weights = get_weights ( probe_predictions , probe ) <EOL> blended_predictions = blend ( probe_predictions , weights ) <EOL> blend_file_path = get_blend_file_path ( ) <EOL> write ( blended_predictions , blend_file_path ) <EOL> def get_probe_predictions ( ) : <EOL> predictions = np . array ( [ ] ) <EOL> prediction_file_paths = sys . argv [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> for prediction_file_path in prediction_file_paths : <EOL> with open ( prediction_file_path , '<STR_LIT:r>' ) as prediction_file : <EOL> prediction = np . transpose ( np . array ( [ prediction_file . read ( ) . split ( ) ] , <EOL> dtype = np . float32 ) ) <EOL> if predictions . size == <NUM_LIT:0> : <EOL> predictions = prediction <EOL> else : <EOL> predictions = np . append ( predictions , prediction , axis = <NUM_LIT:1> ) <EOL> return np . matrix ( predictions ) <EOL> def get_probe ( ) : <EOL> probe_points = load_numpy_array_from_file ( PROBE_DATA_FILE_PATH ) <EOL> probe_ratings = np . array ( probe_points , dtype = np . int32 ) [ : , RATING_INDEX ] <EOL> return probe_ratings <EOL> def get_weights ( predictions , ratings , alpha = <NUM_LIT:1.0> ) : <EOL> assert predictions . shape [ <NUM_LIT:0> ] > predictions . shape [ <NUM_LIT:1> ] <EOL> number_of_models = predictions . shape [ <NUM_LIT:1> ] <EOL> gamma = np . eye ( number_of_models ) * alpha ** <NUM_LIT:2> <EOL> ptp_plus_gamma = predictions . T * predictions + gamma <EOL> weights = np . dot ( np . linalg . inv ( ptp_plus_gamma ) * predictions . T , ratings ) <EOL> return np . ravel ( weights ) <EOL> def blend ( predictions , weights ) : <EOL> return np . ravel ( weights * predictions . T ) <EOL> def get_blend_file_path ( ) : <EOL> return sys . argv [ - <NUM_LIT:1> ] <EOL> def write ( predictions , file_path ) : <EOL> assert not os . path . isfile ( file_path ) , '<STR_LIT>' . format ( file_path ) <EOL> with open ( file_path , '<STR_LIT>' ) as file : <EOL> file . write ( '<STR_LIT:\n>' . join ( [ str ( p ) for p in predictions ] ) + '<STR_LIT:\n>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
