def measure(self, target: Labelling, predicted) -> float: avg_prec = 0.0 npos = target.positives() if (npos == 0): return 1.0 pos_idxs = target.where_positive() for i in pos_idxs: yi_rank = predicted[i] s = sum([1 for j in pos_idxs if predicted[j] <= yi_rank]) avg_prec += s / yi_rank return avg_prec / npos
def clr(P: ProbabilityDistribution) -> Labelling: n = P.getNumberOfLabels() Pm = P.marginal() Pmsum = n - sum(Pm) return Labelling([ 1 if (_rpc_labelscore(P, i) + Pm[i] >= Pmsum) else 0 for i in range(n) ])
def minrisk(self, Pdist: ProbabilityDistribution): min_risk = 128.0 for L in Labelling.iterAll(Pdist.getNumberOfLabels()): r = self.risk(L, Pdist) if (r < min_risk): min_risk = r min_risk_sol = L return (min_risk_sol, min_risk)
def maxrisk(self, Pdist: ProbabilityDistribution): max_risk = 0.0 for L in Labelling.iterAll(Pdist.getNumberOfLabels()): r = self.risk(L, Pdist) if (r > max_risk): max_risk = r max_risk_sol = L return (max_risk_sol, max_risk)
def risk(self, prediction: Labelling, Pdist: ProbabilityDistribution) -> float: n = Pdist.getNumberOfLabels() k = prediction.positives() if (k == 0): return 1 - Pdist.fulljoint([0] * n) p = 0.0 for i in range(n): if (prediction[i] == 1): p += sum([ Pdist.getProbSize(i, j) / (k + j) for j in range(1, n + 1) ]) return 1 - 2 * p
def dbr(P: ProbabilityDistribution) -> Labelling: n = P.getNumberOfLabels() M = P.marginal() ybr = np.where(M >= 1 / 2, 1, 0) L = Labelling.zeros(n) for i in range(n): vi = ybr[i] ybr[i] = 0 p0 = P.fulljoint(ybr) ybr[i] = 1 p1 = P.fulljoint(ybr) ybr[i] = vi L[i] = 1 if (p1 + 1e-20) / (p0 + p1 + 2e-20) >= 1 / 2 else 0 return L
def classifier_chain(P: ProbabilityDistribution): n = P.getNumberOfLabels() given = np.empty((n, 2), dtype=int) given[:, 0] = np.arange(n) ask = np.array([[0, 1]]) perm = np.random.permutation(range(n)) for i, li in enumerate(perm): ask[0, 0] = li p = P.jointCond(ask, given[perm[:i]]) if (p > 0.5): given[li, 1] = 1 else: given[li, 1] = 0 return Labelling(given[:, 1])
def measure(self, target: Labelling, predicted) -> float: s = 0 for ri, yi in zip(predicted, target): if (yi == 0): continue for rj, yj in zip(predicted, target): if (yj == 1): continue if (ri > rj): s += 1 num_pos = target.positives() n = len(target) if (self.normalized == 2): return s / (num_pos * (n - num_pos)) if (self.normalized == 1): return 4 * s / (n * n) return s
def minrisk(self, Pdist: ProbabilityDistribution): n = Pdist.getNumberOfLabels() def predict_k(k): D = [] R = range(1, n + 1) for i in range(n): s1 = sum([Pdist.getProbSize(i, s) / (s + k) for s in R]) D.append(s1) pos = np.argsort(D)[-k:] Y = [0] * n v = 0.0 for p in pos: Y[p] = 1 v += 2 * D[p] return (Y, v) bests_Y = [([0] * n, Pdist.fulljoint([0] * n))] bests_Y += [predict_k(i) for i in range(1, n + 1)] V = [Y[1] for Y in bests_Y] v = np.argmax(V) best = bests_Y[v] # assert(np.isclose(1-best[1], super().minrisk(Pdist)[1])) return Labelling(best[0]), 1 - best[1]
def br(P) -> Labelling: M = P.marginal() return Labelling(np.where(M >= 1 / 2, 1, 0))
def minrisk(self, Pdist: ProbabilityDistribution): L = Labelling([1 if (p >= 1 / 2) else 0 for p in Pdist.marginal()]) return (L, self.risk(L, Pdist))
def measure(self, target: Labelling, predicted) -> float: positives_idxs = target.where_positive() return sum([1 / predicted[pi] for pi in positives_idxs]) / sum( 1 / np.arange(1, target.positives() + 1))
def measure(self, target: Labelling, predicted) -> float: positives_idxs = target.where_positive() if (len(positives_idxs) == 0): return 0.0 return max([predicted[pi] for pi in positives_idxs ]) # / len(target) / target.positives()