<commit_before>package edu.neu.ccs.pyramid.eval;

import edu.neu.ccs.pyramid.classification.Classifier;
import edu.neu.ccs.pyramid.dataset.ClfDataSet;
import edu.neu.ccs.pyramid.dataset.MultiLabel;
import edu.neu.ccs.pyramid.dataset.MultiLabelClfDataSet;
import edu.neu.ccs.pyramid.multilabel_classification.MultiLabelClassifier;
import edu.neu.ccs.pyramid.util.ArgSort;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.IntStream;

/**
 * Created by Rainicy on 8/21/15.
 */
public class AveragePrecision {


    /**
     * follows http://web.stanford.edu/class/cs276/handouts/EvaluationNew-handout-6-per.pdf
     *
     * @param relevance the actual relevance of each item in the sorted list; sorted by the estimated relevance,
     *                  from most relevant to least relevant
     *
     * @return
     */
    public static double averagePrecision(boolean[] relevance){
        double totalRelevant = 0;
        double relevantSoFar = 0;
        double sumPrecisionAtK = 0;
        for (int i=0;i<relevance.length;i++){
            if (relevance[i]){
                totalRelevant += 1;
                relevantSoFar += 1;
                sumPrecisionAtK += relevantSoFar/(i+1);
            }
        }
        return sumPrecisionAtK/totalRelevant;
    }

    public static double averagePrecision(int[] relevance){
        double totalRelevant = 0;
        double relevantSoFar = 0;
        double sumPrecisionAtK = 0;
        for (int i=0;i<relevance.length;i++){
            if (relevance[i]==1){
                totalRelevant += 1;
                relevantSoFar += 1;
                sumPrecisionAtK += relevantSoFar/(i+1);
            }
        }
        return sumPrecisionAtK/totalRelevant;
    }

    /**
     * sort by scores, and compare against binary labels; bigger score is interpreted as more relevant
     * @param binaryLabels
     * @param scores
     * @return
     */
    public static double averagePrecision(int[] binaryLabels, double[] scores){
        int[] sortedIndices = ArgSort.argSortDescending(scores);
        int[] relevance = new int[binaryLabels.length];
        for (int i=0;i<relevance.length;i++){
            relevance[i] = binaryLabels[sortedIndices[i]];
        }
        return averagePrecision(relevance);
    }

    /**
     * compute average precision for a binary classification task
     * @param classifier
     * @param dataSet
     * @return
     */
    public static double averagePrecision(Classifier.ProbabilityEstimator classifier, ClfDataSet dataSet){
        if (classifier.getNumClasses()!=2){
            throw new IllegalArgumentException("classifier.getNumClasses()!=2");
        }
        double[] probs = new double[dataSet.getNumDataPoints()];
        IntStream.range(0, dataSet.getNumDataPoints()).parallel()
                .forEach(i->probs[i]= classifier.predictClassProbs(dataSet.getRow(i))[1]);
        return averagePrecision(dataSet.getLabels(), probs);
    }

<commit_msg>more methods in average precision<commit_after>package edu.neu.ccs.pyramid.eval;

import edu.neu.ccs.pyramid.classification.Classifier;
import edu.neu.ccs.pyramid.dataset.ClfDataSet;
import edu.neu.ccs.pyramid.dataset.DataSet;
import edu.neu.ccs.pyramid.dataset.MultiLabel;
import edu.neu.ccs.pyramid.dataset.MultiLabelClfDataSet;
import edu.neu.ccs.pyramid.multilabel_classification.MultiLabelClassifier;
import edu.neu.ccs.pyramid.util.ArgSort;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.IntStream;

/**
 * Created by Rainicy on 8/21/15.
 */
public class AveragePrecision {


    /**
     * follows http://web.stanford.edu/class/cs276/handouts/EvaluationNew-handout-6-per.pdf
     *
     * @param relevance the actual relevance of each item in the sorted list; sorted by the estimated relevance,
     *                  from most relevant to least relevant
     *
     * @return
     */
    public static double averagePrecision(boolean[] relevance){
        double totalRelevant = 0;
        double relevantSoFar = 0;
        double sumPrecisionAtK = 0;
        for (int i=0;i<relevance.length;i++){
            if (relevance[i]){
                totalRelevant += 1;
                relevantSoFar += 1;
                sumPrecisionAtK += relevantSoFar/(i+1);
            }
        }
        return sumPrecisionAtK/totalRelevant;
    }

    public static double averagePrecision(int[] relevance){
        double totalRelevant = 0;
        double relevantSoFar = 0;
        double sumPrecisionAtK = 0;
        for (int i=0;i<relevance.length;i++){
            if (relevance[i]==1){
                totalRelevant += 1;
                relevantSoFar += 1;
                sumPrecisionAtK += relevantSoFar/(i+1);
            }
        }
        return sumPrecisionAtK/totalRelevant;
    }

    /**
     * sort by scores, and compare against binary labels; bigger score is interpreted as more relevant
     * @param binaryLabels
     * @param scores
     * @return
     */
    public static double averagePrecision(int[] binaryLabels, double[] scores){
        int[] sortedIndices = ArgSort.argSortDescending(scores);
        int[] relevance = new int[binaryLabels.length];
        for (int i=0;i<relevance.length;i++){
            relevance[i] = binaryLabels[sortedIndices[i]];
        }
        return averagePrecision(relevance);
    }

    /**
     * compute average precision for a binary classification task
     * @param classifier
     * @param dataSet
     * @return
     */
    public static double averagePrecision(Classifier.ProbabilityEstimator classifier, ClfDataSet dataSet){
        if (classifier.getNumClasses()!=2){
            throw new IllegalArgumentException("classifier.getNumClasses()!=2");
        }
        return averagePrecision(classifier, dataSet, dataSet.getLabels());
    }

    public static double averagePrecision(Classifier.ProbabilityEstimator classifier, DataSet dataSet, int[] labels){
        double[] probs = new double[dataSet.getNumDataPoints()];
        IntStream.range(0, dataSet.getNumDataPoints()).parallel()
                .forEach(i->probs[i]= classifier.predictClassProbs(dataSet.getRow(i))[1]);
        return averagePrecision(labels, probs);
    }


{'fraction_non_alphanumeric': 0.10661942341761958, 'fraction_numerical': 0.009341278788854887, 'mean_word_length': 2.6146682188591384, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6539383', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1812, 'n_words': 389}