Exemplo n.º 1
0
def test_initialisation():
    I,J = 2,3
    R = numpy.ones((I,J))
    M = numpy.ones((I,J))
    K = 4
    L = 5
    
    # Init FG ones, S random
    init_FG = 'ones'
    init_S = 'random'
    nmtf = NMTF(R,M,K,L)
    nmtf.initialise(init_S,init_FG)
    
    assert numpy.array_equal(numpy.ones((I,K)),nmtf.F)
    assert numpy.array_equal(numpy.ones((J,L)),nmtf.G)
    for (k,l) in itertools.product(range(0,K),range(0,L)):
        assert nmtf.S[k,l] > 0 and nmtf.S[k,l] < 1
    
    # Init FG random, S ones
    init_FG = 'random'
    init_S = 'ones'
    nmtf = NMTF(R,M,K,L)
    nmtf.initialise(init_S,init_FG)
    
    assert numpy.array_equal(numpy.ones((K,L)),nmtf.S)
    for (i,k) in itertools.product(range(0,I),range(0,K)):
        assert nmtf.F[i,k] > 0 and nmtf.F[i,k] < 1
    for (j,l) in itertools.product(range(0,J),range(0,L)):
        assert nmtf.G[j,k] > 0 and nmtf.G[j,k] < 1    
        
    # Init FG kmeans, S exponential
    init_FG = 'kmeans'
    init_S = 'exponential'
    nmtf = NMTF(R,M,K,L)
    nmtf.initialise(init_S,init_FG)
    
    for (i,k) in itertools.product(range(0,I),range(0,K)):
        assert nmtf.F[i,k] == 0.2 or nmtf.F[i,k] == 1.2
    for (j,l) in itertools.product(range(0,J),range(0,L)):
        assert nmtf.G[j,k] == 0.2 or nmtf.G[j,k] == 1.2   
    for (k,l) in itertools.product(range(0,K),range(0,L)):
        assert nmtf.S[k,l] > 0
Exemplo n.º 2
0
# Load in data
R = numpy.loadtxt(input_folder + "R.txt")
M = numpy.ones((I, J))

# Run the VB algorithm, <repeats> times
times_repeats = []
performances_repeats = []
for i in range(0, repeats):
    # Set all the seeds
    numpy.random.seed(3)
    random.seed(4)
    scipy.random.seed(5)

    # Run the classifier
    nmtf = NMTF(R, M, K, L)
    nmtf.initialise(init_S, init_FG, expo_prior)
    nmtf.run(iterations)

    # Extract the performances and timestamps across all iterations
    times_repeats.append(nmtf.all_times)
    performances_repeats.append(nmtf.all_performances)

# Check whether seed worked: all performances should be the same
assert all([numpy.array_equal(performances, performances_repeats[0]) for performances in performances_repeats]), \
    "Seed went wrong - performances not the same across repeats!"

# Print out the performances, and the average times
all_times_average = list(numpy.average(times_repeats, axis=0))
all_performances = performances_repeats[0]
print "np_all_times_average = %s" % all_times_average
print "np_all_performances = %s" % all_performances
Exemplo n.º 3
0
# We now run the VB algorithm 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, 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" % \