def test_predict(): R = numpy.array([[1, 2], [3, 4]], dtype=float) M = numpy.array([[1, 1], [0, 1]]) (I, J, K, L) = (2, 2, 3, 1) F = numpy.array([[1, 2, 3], [4, 5, 6]]) S = numpy.array([[7], [8], [9]]) G = numpy.array([[10], [11]]) #R_predicted = numpy.array([[500,550],[1220,1342]],dtype=float) nmtf = NMTF(R, M, K, L) nmtf.F = F nmtf.S = S nmtf.G = G performances = nmtf.predict(M) MSE = (499 * 499 + 548 * 548 + 1338 * 1338) / 3.0 R2 = 1 - (499**2 + 548**2 + 1338**2) / ( (4.0 / 3.0)**2 + (1.0 / 3.0)**2 + (5.0 / 3.0)**2) #mean=7.0/3.0 #mean_real=7.0/3.0,mean_pred=2392.0/3.0 -> diff_real=[-4.0/3.0,-1.0/3.0,5.0/3.0],diff_pred=[-892.0/3.0,-742.0/3.0,1634.0/3.0] Rp = ((-4.0 / 3.0 * -892.0 / 3.0) + (-1.0 / 3.0 * -742.0 / 3.0) + (5.0 / 3.0 * 1634.0 / 3.0)) / (math.sqrt((-4.0 / 3.0)**2 + (-1.0 / 3.0)**2 + (5.0 / 3.0)**2) * math.sqrt((-892.0 / 3.0)**2 + (-742.0 / 3.0)**2 + (1634.0 / 3.0)**2)) assert performances['MSE'] == MSE assert abs(performances['R^2'] - R2) < 0.000000001 assert abs(performances['Rp'] - Rp) < 0.000000001
def test_predict(): R = numpy.array([[1,2],[3,4]],dtype=float) M = numpy.array([[1,1],[0,1]]) (I,J,K,L) = (2,2,3,1) F = numpy.array([[1,2,3],[4,5,6]]) S = numpy.array([[7],[8],[9]]) G = numpy.array([[10],[11]]) #R_predicted = numpy.array([[500,550],[1220,1342]],dtype=float) nmtf = NMTF(R,M,K,L) nmtf.F = F nmtf.S = S nmtf.G = G performances = nmtf.predict(M) MSE = ( 499*499 + 548*548 + 1338*1338 ) / 3.0 R2 = 1 - (499**2 + 548**2 + 1338**2)/((4.0/3.0)**2 + (1.0/3.0)**2 + (5.0/3.0)**2) #mean=7.0/3.0 #mean_real=7.0/3.0,mean_pred=2392.0/3.0 -> diff_real=[-4.0/3.0,-1.0/3.0,5.0/3.0],diff_pred=[-892.0/3.0,-742.0/3.0,1634.0/3.0] Rp = ((-4.0/3.0*-892.0/3.0)+(-1.0/3.0*-742.0/3.0)+(5.0/3.0*1634.0/3.0)) / (math.sqrt((-4.0/3.0)**2+(-1.0/3.0)**2+(5.0/3.0)**2) * math.sqrt((-892.0/3.0)**2+(-742.0/3.0)**2+(1634.0/3.0)**2)) assert performances['MSE'] == MSE assert abs(performances['R^2'] - R2) < 0.000000001 assert abs(performances['Rp'] - Rp) < 0.000000001
for (fraction,Ms,Ms_test) in zip(fractions_unknown,all_Ms,all_Ms_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,M_test) in zip(range(0,repeats),Ms,Ms_test): print "Repeat %s of fraction %s." % (repeat+1, fraction) # Run the VB algorithm nmtf = NMTF(R,M,K,L) nmtf.initialise(init_S,init_FG) 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) print "repeats=%s \nfractions_unknown = %s \nall_performances = %s \naverage_performances = %s" % \ (repeats,fractions_unknown,all_performances,average_performances) ''' repeats=10