import project3 as p3 import random as ra import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as pat from numpy import linalg as LA #------------------------------------------------------------------------------- # Section 1.a #------------------------------------------------------------------------------- # Test the provided kMeans method on the toy data for K=[1,2,3,4] with # several different random initializations. Provide plots of the solution # for each K that minimizes the total distortion cost. X = p3.readData('toy_data.txt') #for K in [1,2,3,4]: # (Mu,P,Var) = p3.init(X,K) # (Mu,P,Var,post) = p3.kMeans(X, K, Mu, P, Var) #p3.plot2D(X,K,Mu,P,Var,post, "Title") #------------------------------------------------------------------------------- #------------------------------------------------------------------------------- # Section 1.b #------------------------------------------------------------------------------- # Fill in the Estep, Mstep, and mixGauss methods. # Test your Estep using K=3, after initializing using # (Mu,P,Var) = p3.init(X,K,fixedmeans=True). The returned log-likelihood # should be -1331.67489.
import project3 as p3 import random as ra import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as pat from numpy import linalg as LA #------------------------------------------------------------------------------- # Section 1.a #------------------------------------------------------------------------------- # Test the provided kMeans method on the toy data for K=[1,2,3,4] with # several different random initializations. Provide plots of the solution # for each K that minimizes the total distortion cost. X = p3.readData('toy_data.txt') # for K in [1,2,3,4]: # print "Calculating for K=", K # (Mu, P, Var) = p3.init(X,K) # (Mu,P,Var,post) = p3.kMeans(X, K, Mu, P, Var) # p3.plot2D(X,K,Mu,P,Var,post,"plot for K = %d" % K) #------------------------------------------------------------------------------- #------------------------------------------------------------------------------- # Section 1.b #------------------------------------------------------------------------------- # Fill in the Estep, Mstep, and mixGauss methods. # Test your Estep using K=3, after initializing using # (Mu,P,Var) = p3.init(X,K,fixedmeans=True). The returned log-likelihood # should be -1331.67489.
############### Imports ############################# # TODO: insert your project 3 file name here: import project3 as p3 import numpy as np ############### Read data ############################ X = p3.readData('matrixCompletionTest_incomplete.txt') Xc = p3.readData('matrixCompletionTest_complete.txt') K = 4 n, d = np.shape(X) ############### Functions ############################# def initToyFixed(X, K): n, d = np.shape(X) P = np.ones((K, 1)) / float(K) Mu = np.array([[2.,3.,4.,0.,0.],\ [0.,0.,2.,3.,3.],\ [2.,5.,2.,3.,4.],\ [0.,5.,3.,4.,2.]]) Var = np.mean((X - np.tile(np.mean(X, axis=0), (n, 1)))**2) * np.ones( (K, 1)) return (Mu, P, Var) ############### Init ################################## Mu, P, Var = initToyFixed(X, K)
import project3 as p3 import random as ra import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as pat from numpy import linalg as LA X = p3.readData("toy_data.txt") #------------------------------------------------------------------------------- # Section 1.a #------------------------------------------------------------------------------- # Test the provided kMeans method on the toy data for K=[1,2,3,4] with # several different random initializations. Provide plots of the solution # for each K that minimizes the total distortion cost. print "----------------------SECTION 1.A----------------------" K_arr = [1, 2, 3, 4] tries = 4 print X for K in K_arr: for t in range(tries): (Mu, P, Var) = p3.init(X, K) (Mu, P, Var, post) = p3.kMeans(X, K, Mu, P, Var) title = 'Plot for K = ' + str(K) + ', Try ' + str(t) p3.plot2D(X, K, Mu, P, Var, post, title) #------------------------------------------------------------------------------- #------------------------------------------------------------------------------- # Section 1.b