<s> import numpy as np <EOL> import scipy . sparse as sp <EOL> from sklearn . metrics import mean_squared_error , r2_score <EOL> from . validation import check_random_state <EOL> from ffm import ffm_predict <EOL> def make_user_item_regression ( random_state = <NUM_LIT> , n_user = <NUM_LIT:20> , n_item = <NUM_LIT:20> , <EOL> label_stdev = <NUM_LIT> , rank = <NUM_LIT:2> , bias = True , <EOL> first_order = True , stdev_w0 = <NUM_LIT> , stdev_w = <NUM_LIT> , <EOL> stdev_V = <NUM_LIT> , mean_w0 = <NUM_LIT:2> , mean_w = <NUM_LIT:5> , mean_V = <NUM_LIT:10> ) : <EOL> n_features = n_user + n_item <EOL> n_samples = n_user * n_item <EOL> user_cols = np . repeat ( range ( n_user ) , n_item ) <EOL> item_cols = np . array ( list ( range ( n_item ) ) * n_user ) + n_user <EOL> cols = np . hstack ( ( user_cols , item_cols ) ) <EOL> rows = np . hstack ( ( np . arange ( n_item * n_user ) , np . arange ( n_item * n_user ) ) ) <EOL> X = sp . coo_matrix ( ( np . ones_like ( cols , dtype = np . float64 ) , ( rows , cols ) ) ) <EOL> X = sp . csc_matrix ( X ) <EOL> assert X . shape [ <NUM_LIT:0> ] == n_samples <EOL> assert X . shape [ <NUM_LIT:1> ] == n_features <EOL> random_state = check_random_state ( random_state ) <EOL> w0 = random_state . normal ( mean_w0 , stdev_w0 ) <EOL> w = random_state . normal ( mean_w , stdev_w , n_features ) <EOL> V = random_state . normal ( mean_V , stdev_V , ( rank , n_features ) ) <EOL> y = ffm_predict ( w0 , w , V , X ) <EOL> if label_stdev > <NUM_LIT:0> : <EOL> y = random_state . normal ( y , label_stdev ) <EOL> return X , y , ( w0 , w , V ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> X , y , coef = make_user_item_regression ( n_user = <NUM_LIT:5> , n_item = <NUM_LIT:5> , rank = <NUM_LIT:2> , <EOL> label_stdev = <NUM_LIT:2> ) <EOL> from sklearn . cross_validation import train_test_split <EOL> X_train , X_test , y_train , y_test = train_test_split ( <EOL> X , y , test_size = <NUM_LIT> , random_state = <NUM_LIT> ) <EOL> from mcmc import FMRegression <EOL> fm = FMRegression ( rank = <NUM_LIT:2> ) <EOL> y_pred = fm . fit_predict ( sp . csc_matrix ( X_train ) , y_train , <EOL> sp . csc_matrix ( X_test ) ) <EOL> print ( '<STR_LIT>' , mean_squared_error ( y_pred , y_test ) ) <EOL> print ( '<STR_LIT>' , r2_score ( y_pred , y_test ) ) <EOL> np . random . shuffle ( y_pred ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' , mean_squared_error ( y_pred , y_test ) ) <EOL> print ( '<STR_LIT>' , r2_score ( y_pred , y_test ) ) </s>
