get accuracy and precision/recall
340908
def compare(stanzas, gold_schemes, found_schemes):
    """"""
    result = SuccessMeasure()
    total = float(len(gold_schemes))
    correct = 0.0
    for (g, f) in zip(gold_schemes, found_schemes):
        if g == f:
            correct += 1
    result.accuracy = correct / total

    # for each word, let rhymeset[word] = set of words in rest of stanza rhyming with the word
    # precision = # correct words in rhymeset[word]/# words in proposed rhymeset[word]
    # recall = # correct words in rhymeset[word]/# words in reference words in rhymeset[word]
    # total precision and recall = avg over all words over all stanzas

    tot_p = 0.0
    tot_r = 0.0
    tot_words = 0.0

    for (s, g, f) in zip(stanzas, gold_schemes, found_schemes):
        stanzasize = len(s)
        for wi, word in enumerate(s):
            grhymeset_word = set(
                map(lambda x: x[0], filter(lambda x: x[1] == g[wi], zip(range(wi + 1, stanzasize), g[wi + 1:]))))
            frhymeset_word = set(
                map(lambda x: x[0], filter(lambda x: x[1] == f[wi], zip(range(wi + 1, stanzasize), f[wi + 1:]))))

            if len(grhymeset_word) == 0:
                continue

            tot_words += 1

            if len(frhymeset_word) == 0:
                continue

            # find intersection
            correct = float(len(grhymeset_word.intersection(frhymeset_word)))
            precision = correct / len(frhymeset_word)
            recall = correct / len(grhymeset_word)
            tot_p += precision
            tot_r += recall

    precision = tot_p / tot_words
    recall = tot_r / tot_words
    result.precision = precision
    result.recall = recall
    if precision + recall > 0:
        result.f_score = 2 * precision * recall / (precision + recall)
    return result
218059
def get(self):
        
        self.log.info('starting the ``get`` method')

        # PRECISION TEST
        decprecision = len(repr(self.dec).split(".")[-1])
        raprecision = len(repr(self.ra).split(".")[-1])

        dec2 = self.dec + self.north

        ra2 = self.ra + \
            ((self.east) /
             (math.cos((self.dec + dec2) * self.DEG_TO_RAD_FACTOR / 2.)))

        # FIX VALUES THAT CROSS RA/DEC LIMITS
        while ra2 > 360. or ra2 < 0.:
            while ra2 > 360.:
                ra2 = ra2 - 360.
            while ra2 < 0.:
                ra2 = ra2 + 360.
        while dec2 > 90. or dec2 < -90.:
            while dec2 > 90.:
                dec2 = 180. - dec2
            while dec2 < -90.:
                dec2 = -180. - dec2

        ra2 = "%0.*f" % (raprecision, ra2)
        dec2 = "%0.*f" % (decprecision, dec2)

        self.log.info('completed the ``get`` method')
        return ra2, dec2
933139
def evaluate_emb(emb, labels):
    """"""
    d_mat = get_distance_matrix(emb)
    d_mat = d_mat.asnumpy()
    labels = labels.asnumpy()

    names = []
    accs = []
    for k in [1, 2, 4, 8, 16]:
        names.append('Recall@%d' % k)
        correct, cnt = 0.0, 0.0
        for i in range(emb.shape[0]):
            d_mat[i, i] = 1e10
            nns = argpartition(d_mat[i], k)[:k]
            if any(labels[i] == labels[nn] for nn in nns):
                correct += 1
            cnt += 1
        accs.append(correct/cnt)
    return names, accs
337088
def from_scale(cls, gold_number, precision, recall, title):
        
        tp_count = get_numerator(recall, gold_number)
        positive_count = get_denominator(precision, tp_count)
        fp_count = positive_count - tp_count
        fn_count = gold_number - tp_count
        scale_report = cls(['tp'] * tp_count,
                           ['fp'] * fp_count,
                           ['fn'] * fn_count,
                           title)
        return scale_report
949415
def computePreRec(cm, class_names):
    '''
    
    '''
    n_classes = cm.shape[0]
    if len(class_names) != n_classes:
        print("Error in computePreRec! Confusion matrix and class_names "
              "list must be of the same size!")
        return
    precision = []
    recall = []
    f1 = []    
    for i, c in enumerate(class_names):
        precision.append(cm[i,i] / numpy.sum(cm[:,i]))
        recall.append(cm[i,i] / numpy.sum(cm[i,:]))
        f1.append( 2 * precision[-1] * recall[-1] / (precision[-1] + recall[-1]))
    return recall, precision, f1
1054642
public Map<String, Double> getFMeasureForLabels()
    {
        Map<String, Double> fMeasure = new LinkedHashMap<>();

        Map<String, Double> precisionForLabels = getPrecisionForLabels();
        Map<String, Double> recallForLabels = getRecallForLabels();

        for (String label : allGoldLabels) {
            double p = precisionForLabels.get(label);
            double r = recallForLabels.get(label);

            double fm = 0;

            if ((p + r) > 0) {
                fm = (2 * p * r) / (p + r);
            }

            fMeasure.put(label, fm);
        }

        return fMeasure;
    }
786992
def accuracy(input:Tensor, targs:Tensor)->Rank0Tensor:
    ""
    n = targs.shape[0]
    input = input.argmax(dim=-1).view(n,-1)
    targs = targs.view(n,-1)
    return (input==targs).float().mean()
1371172
public int precision(int recall) {
    int optimum = 0;
    for (int right = 0; right <= recall; right++) {
      int candidate = numpositive[right] + numnegative[recall - right];
      if (candidate > optimum) {
        optimum = candidate;
      }
    }
    return optimum;
  }
1222912
def _average_precision(self, rec, prec):
        
        # append sentinel values at both ends
        mrec = np.concatenate(([0.], rec, [1.]))
        mpre = np.concatenate(([0.], prec, [0.]))

        # compute precision integration ladder
        for i in range(mpre.size - 1, 0, -1):
            mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])

        # look for recall value changes
        i = np.where(mrec[1:] != mrec[:-1])[0]

        # sum (\delta recall) * prec
        ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
        return ap
178497
def recall(truth, recommend, k=None):
    
    if len(truth) == 0:
        if len(recommend) == 0:
            return 1.
        return 0.

    if k is None:
        k = len(recommend)
    return count_true_positive(truth, recommend[:k]) / float(truth.size)
1136849
def _get_result(self) -> float:
        """"""
        try:
            data = self._bus.read_word_data(self._i2c_add, self._mode)
            self._ok = True
        except OSError as exc:
            self.log_error("Bad reading in bus: %s", exc)
            self._ok = False
            return -1

        count = data >> 8 | (data & 0xff) << 8
        mode2coeff = 2 if self._high_res else 1
        ratio = 1 / (1.2 * (self._mtreg / 69.0) * mode2coeff)
        return ratio * count
727760
def _check_success(self):
        
        t, d, cos = self._compute_orientation()

        return d < 0.06 and t >= -0.12 and t <= 0.14 and cos > 0.95
1107015
def accuracy(self, mode: OsuMode):
        
        if mode is OsuMode.osu:
            return (
                (6 * self.count300 + 2 * self.count100 + self.count50) /
                (6 * (self.count300 + self.count100 + self.count50 + self.countmiss)))
        if mode is OsuMode.taiko:
            return (
                (self.count300 + self.countgeki + (0.5*(self.count100 + self.countkatu))) /
                (self.count300 + self.countgeki + self.count100 + self.countkatu + self.countmiss))
        if mode is OsuMode.mania:
            return (
                (6 * (self.countgeki + self.count300) + 4 * self.countkatu + 2 * self.count100 + self.count50) /
                (6 * (self.countgeki + self.count300 + self.countkatu + self.count100 + self.count50 + self.countmiss)))
        if mode is OsuMode.ctb:
            return (
                (self.count50 + self.count100 + self.count300) /
                (self.count50 + self.count100 + self.count300 + self.countmiss + self.countkatu))
693558
def get_precision(self):
        
        assert self.classifier is not None, 'not calibrated'
        arr = np.zeros((self._som.nrows, self._som.ncols))
        for ij, (lbl, p) in self.classifier.items():
            arr[ij] = p
        return arr
515373
def overall_f_measure(self):
        
        if self.overall['Nsys'] == 0 and self.empty_system_output_handling == 'zero_score':
            precision = 0

        else:
            precision = metric.precision(
                Ntp=self.overall['Ntp'],
                Nsys=self.overall['Nsys']
            )

        recall = metric.recall(
            Ntp=self.overall['Ntp'],
            Nref=self.overall['Nref']
        )

        f_measure = metric.f_measure(
            precision=precision,
            recall=recall
        )

        return {
            'f_measure': f_measure,
            'precision': precision,
            'recall': recall
        }
538394
def precision_curve(self, delta_tau=0.01):
        
        # compute thresholds based on the sorted probabilities
        orig_thresh = self.threshold
        sorted_labels, sorted_probs = self.sorted_values

        scores = []
        taus = []
        tau = 0
        for k in range(len(sorted_labels)):
            # compute new accuracy
            self.threshold = tau
            scores.append(self.precision)
            taus.append(tau)

            # update threshold
            tau = sorted_probs[k]

        # add last datapoint
        tau = 1.0
        self.threshold = tau
        scores.append(1.0)
        taus.append(tau)

        self.threshold = orig_thresh
        return scores, taus
1264221
def calc_true_performance(self, printout = False):
        
        try:
            self.calc_confusion_matrix(printout = False)
        except DataError as e:
            print(e.msg)
            raise

        if self.TP + self.FP == 0:
            self.precision = np.nan
        else:
            self.precision = self.TP / (self.TP + self.FP)

        if self.TP + self.FN == 0:
            self.recall = np.nan
        else:
            self.recall = self.TP / (self.TP + self.FN)

        if self.precision + self.recall == 0:
            self.F1_measure = np.nan
        else:
            self.F1_measure = ( 2 * self.precision * self.recall /
                                 (self.precision + self.recall) )

        if printout:
            print("True performance is:")
            print("--------------------")
            print("Precision: {} \t Recall: {} \t F1 measure: {}".format(self.precision, self.recall, self.F1_measure))