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) NMTF = nmtf_np(R, M_train, K, L) NMTF.initialise(init_S, init_FG, expo_prior=expo_prior) NMTF.run(iterations) # Measure the performances performances = NMTF.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)
location = project_location+"HMF/drug_sensitivity/data/overlap/" 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 NMTF ''' iterations = 1000 init_FG, init_S = 'kmeans', 'random' K, L = 10, 10 ''' Run the method and time it. ''' time_start = time.time() NMTF = nmtf_np(R,M,K,L) NMTF.initialise(init_FG=init_FG, init_S=init_S) NMTF.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)
K,L = 4,4 init_FG, init_S = 'kmeans', 'exponential' expo_prior = 0.1 no_folds = 10 file_performance = 'results/nmtf_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. ''' NMTF_NP = nmtf_np(R=R_gdsc,M=train,K=K,L=L) NMTF_NP.train(iterations=iterations,init_S=init_S,init_FG=init_FG,expo_prior=expo_prior) R_pred = NMTF_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)