Example #1
0
def test_initialisation():
    I, J = 2, 3
    R = numpy.ones((I, J))
    M = numpy.ones((I, J))
    K = 4

    # Init ones
    init_UV = 'ones'
    nmf = NMF(R, M, K)
    nmf.initialise(init_UV)

    assert numpy.array_equal(numpy.ones((2, 4)), nmf.U)
    assert numpy.array_equal(numpy.ones((3, 4)), nmf.V)

    # Init random
    init_UV = 'random'
    nmf = NMF(R, M, K)
    nmf.initialise(init_UV)

    for (i, k) in itertools.product(range(0, I), range(0, K)):
        assert nmf.U[i, k] > 0 and nmf.U[i, k] < 1
    for (j, k) in itertools.product(range(0, J), range(0, K)):
        assert nmf.V[j, k] > 0 and nmf.V[j, k] < 1
Example #2
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)

        nmf = NMF(R, M, K)
        nmf.initialise(init_UV, 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" % \