eric-haibin-lin commented on a change in pull request #7390: adding ranking metrics (precision/recall) at position K. URL: https://github.com/apache/incubator-mxnet/pull/7390#discussion_r136199686
########## File path: python/mxnet/metric.py ########## @@ -568,6 +568,147 @@ def update(self, labels, preds): self.sum_metric += f1_score self.num_inst += 1 +@register +@alias('top_k_precision') +class TopKPrecision(EvalMetric): + """Computes top k precision metric. + top k differs from regular precision in that the score is only + computed for the top k predictions. "correct" or "wrong" entries + outside the top k are ignored + Parameters + ---------- + top_k : int + Whether targets are in top k predictions. + name : str + Name of this metric instance for display. + output_names : list of str, or None + Name of predictions that should be used when updating with update_dict. + By default include all predictions. + label_names : list of str, or None + Name of labels that should be used when updating with update_dict. + By default include all labels. + + Examples + -------- + >>>ytrue = [[1.,0.,1.,0.],[0.,1.,1.,0.]] + >>>ytrue = mx.nd.array(ytrue) + >>>yhat = [[0.4,0.8,0.1,0.1],[0.4,0.8,0.8,0.4]] + >>>yhat = mx.nd.array(yhat) + >>>pre = mx.metric.create('top_k_precision',top_k=2) + >>>pre.update(preds = [yhat], labels = [ytrue]) + >>>print pre.get()[1] + >>> 0.75 + + """ + + def __init__(self, top_k=1, name='top_k_precision', + output_names=None, label_names=None): + super(TopKPrecision, self).__init__( + name, top_k=top_k, + output_names=output_names, label_names=label_names) + self.top_k = top_k + + + def update(self, labels, preds): + """Updates the internal evaluation result. + Parameters + ---------- + labels : list of `NDArray` + The labels of the data. (binary) + preds : list of `NDArray` + Predicted values. (float) + + Returns: + -------- + The precision at K (float) + """ + check_label_shapes(labels, preds) + + for label, pred_label in zip(labels, preds): + assert(len(pred_label.shape) <= 2), 'Predictions should be no more than 2 dims' + pred_label = numpy.argsort(-pred_label.asnumpy().astype('float32'), axis=1) Review comment: does this work when `len(pred_label.shape) == 1` ?? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services