尝试使用手写数字数据集的光学识别在 python 中实现高斯混合模型实现,该数据集由每个大小 [100x64] 的 10 个训练折叠和每个大小 [100x1] 的 10 个训练标签组成。该数据集还有一个测试数据集和大小为 $\left[110x64\right]$ 和 [110x1] 的标签集。只有两个类 5 和 6。我在类条件密度方法上得到以下错误:
ValueError: operands could not be broadcast together with shapes (64,64) (100,64)
鉴于此数据集配置,不知道我是否正确估计了最佳参数。根据 Bishop(模式识别和机器学习 2006)。我首先必须通过 MLE 估计最佳参数。所以对于每一折,我都在估计最佳参数,但是我不知道如何计算后验概率。
我什至不知道我的方法是否正确。我已经在 GitHub 和 medium 上搜索过类似的示例。任何帮助或指导将不胜感激。
我的代码实现:
#----------------------------------------------------------
# Assigning parameters
#----------------------------------------------------------
#--------------------------------------------------------
# Initial guess
#----------------------------------------------------------
# Total Folds
folds = 10
#Define the number of classes
num_classes = 2
n, m = total_trainf[0].shape
means = np.zeros((num_classes , n))
phi = np.zeros((num_classes , 1))
shared_cov_matrix = np.cov(np.transpose(np.concatenate(total_trainf, axis=0)),bias=True)
#posterior_prob_est = pp(w0, w, np.concatenate(total_trainf, axis=1))
c= 1
f= 0
acc_arry = []
# calculate the maximum likelihood of each observation xi
likelihood = []
means_array = np.zeros([num_classes, total_trainf[0].shape[1]])
cond_class_prob = np.zeros([num_classes, 1])
# Expectation step
means = np.zeros([folds, total_trainf[0].shape[1]])
while f < folds:
uclasses = np.unique(total_trainl[f])
mu_arr, pi, sigma = get_params(total_trainf[f], total_trainl[f], means_array, phi, uclasses)
#means[f, :] = mu_arr
cov = get_cov(total_trainl[f], shared_cov_matrix, sigma)
class_prob = ccd(total_trainf[f], mu_arr, pi, shared_cov_matrix )
f+=1
means_array[c-1] = np.mean(means, axis=0)
def get_params(data, label, means, pi, class_type):
mean_array = np.zeros([1, data.shape[1]])
num_classes = len(class_type)
sigma = 0
col = 0
for i in range(num_classes):
ind = np.flatnonzero(label == class_type[i])
pi[i] = len(ind)/label.shape[0]
means[i] = np.mean(data[ind] , axis = 0)
sigma += np.cov(data[ind].T)*(len(ind) - 1)
sigma = sigma/label.shape[0]
return means, pi, sigma
#Covariance per fold
def get_cov(data, cov, S):
res = np.log(np.linalg.det(cov))+np.trace(np.linalg.inv(cov)*S)
return -(data.shape[0]/2)*res
#Class conditional density
def ccd(data, means: np.array, pi: np.array, cov_matrix: np.array):
inv_cov = np.linalg.inv(cov_matrix)
mu_1 = means[0]
mu_2 = means[1]
pi_1 = pi[0]
pi_2 = pi[1]
W = inv_cov*(mu_1 - mu_2)
ft = -0.5*(mu_1.T*inv_cov*mu_1)
st = 0.5*(mu_2.T*inv_cov*mu_2)
tt = np.log(pi_1/pi_2)
W_0 = ft + st + tt
return pp(W, W_0, data)
#--------------------------------------------------------
# Initial guess
#----------------------------------------------------------
# Total Folds
folds = 10
#Define the number of classes
num_classes = 2
n, m = total_trainf[0].shape
means = np.zeros((num_classes , n))
#Covariance matrix
cov_matrix = np.cov(np.transpose(np.concatenate(total_trainf, axis=0)),bias=True)
c= 1
f= 0
means_array = np.zeros([num_classes, total_trainf[0].shape[1]])
cond_class_prob = np.zeros([num_classes, 1])
# Expectation step
while c <= num_classes:
means = np.zeros([folds, total_trainf[0].shape[1]])
while f < folds:
uclasses = np.unique(total_trainl[f])
mu_arr, pi = get_params(total_trainf[f], total_trainl[f], uclasses[c-1])
means[f, :] = mu_arr
cond_class_prob[c-1] *= ccd(total_trainf[f], mu_arr, cov_matrix)
f+=1
means_array[c-1] = np.mean(means, axis=0)
c += 1
参数功能
def get_params(data , label, class_type):
accum = 0
accum_array = np.zeros([1, data.shape[1]])
class_index = np.flatnonzero(label == class_type)
phi = len(class_index)/data.shape[0]
row = 0
col = 0
while col < data.shape[1]:
accum_array[0,col] = np.mean(data[col] , axis = 0)
col += 1
return accum_array, phi
#Class conditional density
def ccd(data, mean: np.array, cov_matrix: np.array):
cond_prob = 0
for row in range(data.shape[0]):
s1 = 1/np.sqrt(((2*np.pi)**data.shape[1])*np.linalg.det(cov_matrix))
arr_dot_1 = np.dot(data[row,:] - mean, np.linalg.inv(cov_matrix))
arr_dot_2 = np.dot(arr_dot_1, (np.transpose(data[row,:] - mean)))
s2 = np.exp(-0.5*(data[row,:] - mean)*arr_dot_2)
cond_prob *= (s1 * s2[0,0])
return cond_prob