def test_estep_test_solution(self):
        post, ll = em.estep(ts.X, ts.mixture_initial)

        self.assertEqual(np.allclose(post, ts.post_first_estep),
                         True, f'Post not as expected:\nGot{post}\nExpected {ts.post_first_estep}')
        self.assertAlmostEqual(ll, ts.ll_first_estep, 8,
                         f'Log likelihood: got {ll}, expected {ts.ll_first_estep}')
    def run_estep_test_input(self, test):
        X, K, mixture, expected_post, expected_ll = test.data()
        post, ll = em.estep(X, mixture)

        self.assertEqual(np.allclose(post, expected_post),
                         True, f'Post not as expected:\nGot {post}\nExpected {expected_post}')
        self.assertEqual(np.isclose(ll, expected_ll),
                         True, f'Log likelihood: got {ll}, expected {expected_ll}')
Exemplo n.º 3
0
K = 4
n, d = X.shape
seed = 0

mixture, _ = common.init(X, K, seed)

# print("Input:")
# print('X:\n' + str(X))
# print('K: ' + str(K))
# print('Mu:\n' + str(mixture.mu))
# print('Var: ' + str(mixture.var))
# print('P: ' + str(mixture.p))
# print()

# print("After first E-step:")
post, ll = em.estep(X, mixture)
# print('post:\n' + str(post))
# print('LL:' + str(ll))
# print()

# print("After first M-step:")
mu, var, p = em.mstep(X, post, mixture)
# print('Mu:\n' + str(mu))
# print('Var: ' + str(var))
# print('P: ' + str(p))
# print()

# print("After a run")
(mu, var, p), post, ll = em.run(X, mixture, post)
# print('Mu:\n' + str(mu))
# print('Var: ' + str(var))
X = np.loadtxt("test_incomplete.txt")
X_gold = np.loadtxt("test_complete.txt")

# X = np.loadtxt("netflix_incomplete.txt")
# X_gold = np.loadtxt("netflix_complete.txt")

K = 4
n, d = X.shape
seed = 0

# TODO: Your code here

[mixture, post] = common.init(X, K, seed)

# [m,n] = np.shape(X)

# for i in range(m):

#     Cu = np.where(X[i] != 0)[0] # return tuple so need [0]
#     Hu = np.where(X[i] == 0)[0]

#     print(Cu)
#     print(Hu)

print(X)
print(mixture)
print(em.estep(X, mixture))

# print(naive_em.estep(X, mixture))
Exemplo n.º 5
0
import numpy as np
import em
import common

X = np.loadtxt("test_incomplete.txt")
X_gold = np.loadtxt("test_complete.txt")

K = 4
n, d = X.shape
seed = 0

initial_mixture, post = common.init(X, K, seed)
print(X)
print(initial_mixture)
post, LL = em.estep(X, initial_mixture)
print(post)
print(LL)

new_mixture = em.mstep(X, post, initial_mixture, 0.25)
print(new_mixture)
Exemplo n.º 6
0
import common

# =============================================================================
# 7. Implementing EM for matrix completion
# =============================================================================

X = np.loadtxt("test_incomplete.txt")
X_gold = np.loadtxt("test_complete.txt")

K = 4
n, d = X.shape
seed = 0

# for incomplete case
mixture, post = common.init(X, K=K, seed=seed)
post, log_likelihood = em.estep(X, mixture)
mixture = em.mstep(X, post, mixture)

# =============================================================================
# 8. Using the mixture model for collaborative filtering
# Reporting log likelihood values on Netflix data
# =============================================================================

X = np.loadtxt("netflix_incomplete.txt")

mixture, post = common.init(X, K=1, seed=0)
post, log_likelihood = em.estep(X, mixture)
mixtured = em.mstep(X, post, mixture)

Ks = [1, 12]
seeds = [0, 1, 2, 3, 4]
Exemplo n.º 7
0
for K in Ks:
    for seed in seeds:
        mixture, post = common.init(X, K=K, seed=seed)  # Initialize K-means
        mixture, post, log_likelihood = naive_em.run(X, mixture, post)
        common.plot(X, mixture, post, [K, seed])
        print(K, seed, log_likelihood)

# =============================================================================
# 5. Bayesian Information Criterion
# Picking the best K
# =============================================================================

for K in Ks:
    mixture, post = common.init(X, K=K)  # Initialize K-means
    mixture, post, log_likelihood = naive_em.run(X, mixture, post)
    BIC = common.bic(X, mixture, log_likelihood)
    print(K, BIC)

# =============================================================================
# 7. Implementing EM for matrix completion
# Test for comlete case
# =============================================================================

X = np.loadtxt("toy_data.txt")
Ks = [1, 2, 3, 4]
seeds = [0, 1, 2, 3, 4]
mixture, post = common.init(X, 3, seed=0)
post1, log_likelihood1 = naive_em.estep(X, mixture)
post2, log_likelihood2 = em.estep(X, mixture)
print(log_likelihood1, log_likelihood2)
# print(BIC_best, K_best)

######################### 8. Using the mixture model for collaborative filtering

# X = np.loadtxt("netflix_incomplete.txt")
# X_gold = np.loadtxt('netflix_complete.txt')

X = np.loadtxt("test_incomplete.txt")
X_gold = np.loadtxt('test_complete.txt')

K = 4
seed = 0

[mixture, post] = common.init(X, K, seed)

[post, L] = em.estep(X, mixture)
mixture = em.mstep(X, post, mixture)
print(post)
print(L)
print(mixture)

[mixture, post, L] = em.run(X, mixture, post)
print(post)
print(L)
print(mixture)

X_prep = em.fill_matrix(X, mixture)
print(X_prep)

RMSE = common.rmse(X_gold, X_prep)
print(RMSE)
Exemplo n.º 9
0
import naive_em
import common


X = np.loadtxt("test_incomplete.txt")
X_gold = np.loadtxt("test_complete.txt")

K = 4
n, d = X.shape
seed = 0

mixture, post = common.init(X, K, seed)


#post, LL = naive_em.estep(X, mixture)
post, LL = em.estep(X, mixture)

print("After first E-step:")
print("post:")
print(post[0:3,:])
print("LL:", LL)

#mixture = naive_em.mstep(X, post)
mixture = em.mstep(X, post, mixture)
print()
print("After first M-step:")
print("Mu:")
print(mixture.mu)
print("Var:", mixture.var)
print("P:", mixture.p)
 def test_estep_toy_dataset(self):
     mixture, post = common.init(self.X, 3, 0)
     post, ll = em.estep(self.X, mixture)
     expected_ll = -1388.0818
     self.assertEqual(np.isclose(ll, expected_ll),
                      True, f'Log likelihood: got {ll}, expected {expected_ll}')