def calculateW(phi, y): w0 = ut.ls2Vec([1] * phi.shape[1]) w1 = ut.ls2Vec([0.5] * phi.shape[1]) delta = 1 while delta > 0.5: w0 = w1 pi = Pi(w0, phi) R = calcR(pi) tmp = reduce(np.dot, [phi.transpose(), R, phi]) z = np.dot(phi, w0) - np.dot(np.linalg.inv(R), (pi - y)) w1 = reduce(np.dot, [np.linalg.inv(tmp), phi.transpose(), R, z]) delta = np.linalg.norm(np.dot(phi.transpose(), (y - pi))) print delta return w1
def GaussianGM(trainData): """ given a set of training data return a function f(x) as a classifier f(x) will return the class of x trainData has the following form: { 1: list of feature vectors 2: list of feature vectors 3: ... } """ # num of classes K = len(trainData) # calculate p(C_k) p = [0] * (K) for i in range(K): p[i] = len(trainData[i + 1]) N = sum(p) p = (np.array(p) / (N + 0.0)).tolist() # calculate a_k (x)= W_k^T x + w_k0 a = [1] * K sigma = [1] * K mu = [1] * K W = [1] * K w0 = [1] * K for i in range(K): mu[i] = ut.getMean(trainData[i + 1], "array") f = lambda x: np.dot((ut.ls2Vec(x) - mu[i]), (ut.ls2Vec(x) - mu[i]).transpose()) sigma[i] = reduce(ut.add, map(f, trainData[i + 1])) sigma_inv = inv(reduce(ut.add, sigma) / (N + 0.0)) for k in range(K): W[k] = np.dot(sigma_inv, mu[k]) w0[k] = -0.5 * reduce(np.dot, [mu[k].transpose(), sigma_inv, mu[k]])[0, 0] + np.log(p[k]) a[k] = lambda x: np.dot(W[k].transpose(), ut.ls2Vec(x))[0, 0] + w0[k] # construct a classifier function def f(w, w0, x): tmp = [np.dot(ww.transpose(), ut.ls2Vec(x))[0, 0] + ww0 for (ww, ww0) in zip(w, w0)] tmp = ut.getMaxPos(tmp) return tmp[0] + 1 return lambda x: f(W, w0, x)
def training(self, trainData, mode = 'diag', subSpaceDim = 2): # num of classes K = len(trainData) # num of features D = len(trainData[1][0]) # mk = {} for k in range(1, K + 1): mk[k] = ut.getMean(trainData[k], 'array') # m = ut.getMean(reduce(ut.add, [trainData[k] for k in range(1, K + 1)])) # f = lambda k: len(trainData[k]) * np.dot( mk[k] - m, (mk[k] - m).transpose() ) S_B = reduce(ut.add, [f(k) for k in range(1, K - 1)]) # construct S_W S_W = [] Sk = range(K + 1) if mode == 'diag': S_W = np.diag([1] * D) else: g = lambda x, y: np.dot((x - y), (x - y).transpose()) for k in range(1, K + 1 ): Sk[k] = reduce(ut.add, [g(ut.ls2Vec(x), mk[k]) for x in trainData[k]]) S_W = reduce(ut.add, Sk[1:]) # [egs, vecs] = np.linalg.eigh(np.dot(np.linalg.inv(S_W), S_B)) pos = ut.getMaxPos(egs, subSpaceDim) W = ut.arrayExtract(vecs, pos) # now we will construct trainingData set in the # sub-feature space, then run Gaussian generative # method to train the new trainData set to get a # a classifier def projectFunc(v): tmp = np.dot(ut.ls2Vec(v).transpose(), W).tolist() return tmp[0] for k in range(K): trainData[k + 1] = map(projectFunc, trainData[k + 1]) self.projectFunc = projectFunc self.func = GaussianGM.GaussianGM(trainData)
def f(w, w0, x): tmp = [np.dot(ww.transpose(), ut.ls2Vec(x))[0, 0] + ww0 for (ww, ww0) in zip(w, w0)] tmp = ut.getMaxPos(tmp) return tmp[0] + 1
def projectFunc(v): tmp = np.dot(ut.ls2Vec(v).transpose(), W).tolist() return tmp[0]
def Pi(w, phi): tmp = np.dot(phi, w).transpose().tolist()[0] return ut.ls2Vec(map(sigma, tmp))
w1 = ut.ls2Vec([0.5] * phi.shape[1]) delta = 1 while delta > 0.5: w0 = w1 pi = Pi(w0, phi) R = calcR(pi) tmp = reduce(np.dot, [phi.transpose(), R, phi]) z = np.dot(phi, w0) - np.dot(np.linalg.inv(R), (pi - y)) w1 = reduce(np.dot, [np.linalg.inv(tmp), phi.transpose(), R, z]) delta = np.linalg.norm(np.dot(phi.transpose(), (y - pi))) print delta return w1 # import data data = ut.importRawData('Pima.csv') k = 100 y = ut.ls2Vec(map(lambda x: x[0] - 1 , data[1:k])) phi = np.array(map(lambda x: x[1:], data[1:k])) #print phi w = calculateW(phi, y) #print calcR(Pi(w, phi)) #pi = Pi(w, phi) #print np.linalg.det(np.dot(phi.transpose(), phi)) # w = reduce(np.dot, [np.linalg.inv(np.dot(phi.transpose(), phi)) , phi.transpose(), y]) print w for x in data[1:k]: print x[0] - 1, sigma( np.dot(w.transpose(), ut.ls2Vec(x[1:]))) #print np.dot(phi.transpose(), (y - pi))