for (M_train,M_test) in Ms_train_test: check_empty_rows_columns(M_train,fraction) ''' Run the method on each of the M's for each fraction ''' all_performances = {metric:[] for metric in metrics} average_performances = {metric:[] for metric in metrics} # averaged over repeats for (fraction,Ms_train_test) in zip(fractions_unknown,all_Ms_train_test): print "Trying fraction %s." % fraction # Run the algorithm <repeats> times and store all the performances for metric in metrics: all_performances[metric].append([]) for repeat,(M_train,M_test) in zip(range(0,repeats),Ms_train_test): print "Repeat %s of fraction %s." % (repeat+1, fraction) NMF = nmf_np(R,M_train,K) NMF.initialise(init_UV,expo_prior=expo_prior) NMF.run(iterations) # Measure the performances performances = NMF.predict(M_test) for metric in metrics: # Add this metric's performance to the list of <repeat> performances for this fraction all_performances[metric][-1].append(performances[metric]) # Compute the average across attempts for metric in metrics: average_performances[metric].append(sum(all_performances[metric][-1])/repeats) print "repeats=%s \nfractions_unknown = %s \nall_performances = %s \naverage_performances = %s" % \
location_data = location + "data_row_01/" location_features_drugs = location + "features_drugs/" location_features_cell_lines = location + "features_cell_lines/" location_kernels = location + "kernels_features/" R_gdsc, M_gdsc, _, _ = load_data_without_empty(location_data + "gdsc_ic50_row_01.txt") R_ctrp, M_ctrp, _, _ = load_data_without_empty(location_data + "ctrp_ec50_row_01.txt") R_ccle_ec, M_ccle_ec, _, _ = load_data_without_empty(location_data + "ccle_ec50_row_01.txt") R_ccle_ic, M_ccle_ic, _, _ = load_data_without_empty(location_data + "ccle_ic50_row_01.txt") R, M = R_ccle_ec, M_ccle_ec ''' Settings NMF ''' iterations = 1000 init_UV = 'random' K = 10 ''' Run the method and time it. ''' time_start = time.time() NMF = nmf_np(R, M, K) NMF.initialise(init_UV) NMF.run(iterations) time_end = time.time() time_taken = time_end - time_start time_average = time_taken / iterations print "Time taken: %s seconds. Average per iteration: %s." % (time_taken, time_average)
''' Settings NMF-NP. ''' iterations = 2000 K = 2 init_UV = 'exponential' expo_prior = 0.1 no_folds = 10 file_performance = 'results/nmf_np.txt' ''' Split the folds. For each, obtain a list for the test set of (i,j,real,pred) values. ''' i_j_real_pred = [] folds_test = mask.compute_folds_attempts(I=I, J=J, no_folds=no_folds, attempts=1000, M=M_gdsc) folds_training = mask.compute_Ms(folds_test) for i, (train, test) in enumerate(zip(folds_training, folds_test)): print "Fold %s." % (i + 1) ''' Predict values. ''' NMF_NP = nmf_np(R=R_gdsc, M=train, K=K) NMF_NP.train(iterations=iterations, init_UV=init_UV, expo_prior=expo_prior) R_pred = NMF_NP.return_R_predicted() ''' Add predictions to list. ''' indices_test = [(i, j) for (i, j) in itertools.product(range(I), range(J)) if test[i, j]] for i, j in indices_test: i_j_real_pred.append((i, j, R_gdsc[i, j], R_pred[i, j])) ''' Store the performances. ''' with open(file_performance, 'w') as fout: fout.write('%s' % i_j_real_pred)