def init_link_labels(self, x0, x1, p):
pdfs = [multivariate_normal(mean=m, cov=c, allow_singular=True) for m, c in zip(self.means, self.covs)]
neglog_costs = np.zeros((2, self.means.shape[0]))
for i in xrange(len(self.means)):
neglog_costs[0, i] = -np.log(pdfs[i].pdf(x0))
neglog_costs[1, i] = -np.log(pdfs[i].pdf(x1))
neglog_link_costs = np.clip(neglog_costs, 0, 9999999999)
def _cost(k, l):
return (-np.log(self.weights[k]) * neglog_link_costs[0, k]) + (-np.log(self.weights[l]) * neglog_link_costs[1, l])
must = must_consistent(self.partition)
cannot = cannot_consistent(self.partition)
if p == 1: # must link
return must[np.argmin([_cost(k, l) for k, l in must])]
elif p == -1: # cannot link
return cannot[np.argmin([_cost(k, l) for k, l in cannot])]
else:
raise ValueError("At this point link is expected to be -1 or 1 only!")
评论列表
文章目录