def build_LDF_model(num_class, x_train, y_train): """ LDF model First call QDF model to caculate the mean, cov_matirx """ prior, mean, cov_matrix = build_QDF_model(num_class, x_train, y_train) #print_cov_matrix(cov_matrix) # cacualte the shared covariance matirx avg_cov = np.matlib.zeros(cov_matrix[0].shape) for i in range(num_class): avg_cov += (prior[i] * cov_matrix[i]) inverse_cov = avg_cov.getI() # get the inverse covariance matrix num_feature = x_train.shape[1] # each column for weight[i] weight = np.matrix([0] * num_feature).T w0 = [] for i in range(num_class): wi = 2 * inverse_cov.T * mean[i] weight = np.hstack((weight, wi)) wi0 = 2 * math.log(prior[i]) - (mean[i].T * inverse_cov * mean[i])[0,0] w0.append(wi0) return inverse_cov, weight[:, 1:], w0
def build_RDA_model(num_class, x_train, y_train, beta, gamma): """ First call QDF model to caculate the mean, cov_matirx beta, gamma: hyper-parameters, range: 0~1 """ prior, mean, cov_matrix = build_QDF_model(num_class, x_train, y_train) # cacualte the shared covariance matirx avg_cov = np.matlib.zeros(cov_matrix[0].shape) for i in range(num_class): avg_cov += (prior[i] * cov_matrix[i]) num_feature = len(x_train[0]) for i in range(num_class): # the following formula is from PPT tmp = cov_matrix[i] dev = tmp.trace()[0,0] * 1.0 / num_feature cov_matrix[i] = (1-gamma) * ((1-beta)*tmp + beta*avg_cov) + gamma * dev * np.matlib.eye(num_feature) """ # refer to Statistical Pattern Recognition, page 43 tmp = cov_matrix[i] tmp = (1-beta) * prior[i] * tmp + beta * avg_cov dev = tmp.trace()[0,0] * 1.0 / num_feature cov_matrix[i] = (1-gamma) * tmp + gamma * dev * np.matlib.eye(num_feature) """ return prior, mean, cov_matrix
def build_MQDF_model(num_class, x_train, y_train, k, delta0): """ MQDF model @k and @delta are hyper-parameters Note: There are three possible ways to set @delta: 1) @delta is a hyper-parameter, set by cross validation 2) @delta can be estimated via ML estimation, in which case, this delta is no longer a hyper-parameter 3) @delta can be set close to $\sigma_{i,k}$ or $\sigma_{i,k+1}$, in which case, this delta is also not a hyper-parameter """ d = len(x_train[0]) # number of features assert(k<d and k>0) prior, mean, cov_matrix = build_QDF_model(num_class, x_train, y_train) eigenvalue = [] # store the first largest k eigenvalues of each class eigenvector = [] # the first largest k eigenvectors, column-wise of each class delta = [0] * num_class # deltas for each class for i in range(num_class): cov = cov_matrix[i] eig_values, eig_vectors = linalg.eigh(cov) # sort the eigvalues idx = eig_values.argsort() idx = idx[::-1] # reverse the array eig_values = eig_values[idx] eig_vectors = eig_vectors[:,idx] eigenvector.append(eig_vectors[:, 0:k]) eigenvalue.append(eig_values[:k]) # delta via ML estimation #delta[i] = (cov.trace() - sum(eigenvalue[i])) * 1.0 / (d-k) # delta close to $\sigma_{i,k-1}$ or $\sigma_{i,k}$ #delta[i] = (eig_values[k-1] + eig_values[k])/2 #print 'Suggestd delta[%d]: %f' % (i, (eig_values[k] + eig_values[k+1])/2) # delta as the mean of minor values delta[i] = sum(eig_values[k:]) / len(eig_values[k:]) #delta = [delta0] * num_class return prior, mean, eigenvalue, eigenvector, delta