All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mulan.evaluation.MultipleEvaluation Maven / Gradle / Ivy

Go to download

Mulan is an open-source Java library for learning from multi-label datasets.

The newest version!
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 *    MultipleEvaluation.java
 *    Copyright (C) 2009-2012 Aristotle University of Thessaloniki, Greece
 */
package mulan.evaluation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import mulan.data.MultiLabelInstances;
import mulan.evaluation.measure.MacroAverageMeasure;
import mulan.evaluation.measure.Measure;

/**
 * Simple class that includes an array, whose elements are lists of evaluation
 * evaluations. Used to compute means and standard deviations of multiple
 * evaluations (e.g. cross-validation)
 * 
 * @author Grigorios Tsoumakas
 */
public class MultipleEvaluation {

    private MultiLabelInstances data;
    private ArrayList evaluations;
    private HashMap mean;
    private HashMap standardDeviation;
    private HashMap labelMean;
    private HashMap labelStandardDeviation;

    /**
     * Constructs a new object
     * @param data the evaluation data used for obtaining label names for per
     * outputting per label values of macro average measures
     */
    public MultipleEvaluation(MultiLabelInstances data) {
        evaluations = new ArrayList();
        this.data = data;
    }

    /**
     * Constructs a new object with given array of evaluations 
     * @param data the evaluation data used for obtaining label names for per
     * outputting per label values of macro average measures
     * @param someEvaluations
     */
    public MultipleEvaluation(Evaluation[] someEvaluations, MultiLabelInstances data) {
        evaluations = new ArrayList();
        evaluations.addAll(Arrays.asList(someEvaluations));
        this.data = data;
    }

    /**
     * Computes mean and standard deviation of all evaluation measures
     */
    public void calculateStatistics() {
        int size = evaluations.size();
        HashMap sums = new HashMap();
        HashMap labelSums = new HashMap();

        // calculate sums of measures
        for (int i = 0; i < evaluations.size(); i++) {
            for (Measure m : evaluations.get(i).getMeasures()) {
                double value = Double.NaN;
                try {
                    value = m.getValue();
                } catch (Exception ex) {
                }
                if (sums.containsKey(m.getName())) {
                    sums.put(m.getName(), sums.get(m.getName()) + value);
                } else {
                    sums.put(m.getName(), value);
                }
                if (m instanceof MacroAverageMeasure) {
                    Double[] v = new Double[data.getNumLabels()];
                    for (int j=0; j();
        for (String measureName : sums.keySet()) {
            mean.put(measureName, sums.get(measureName) / size);
        }
        
        labelMean = new HashMap();
        for (String measureName : labelSums.keySet()) {
            Double[] v = labelSums.get(measureName);
            for (int j=0; j();
        labelSums = new HashMap();

        for (int i = 0; i < evaluations.size(); i++) {
            for (Measure m : evaluations.get(i).getMeasures()) {
                double value = Double.NaN;
                try {
                    value = m.getValue();
                } catch (Exception ex) {
                }
                if (sums.containsKey(m.getName())) {
                    sums.put(m.getName(), sums.get(m.getName()) + Math.pow(value - mean.get(m.getName()), 2));
                } else {
                    sums.put(m.getName(), Math.pow(value - mean.get(m.getName()), 2));
                }
                if (m instanceof MacroAverageMeasure) {
                    Double[] mean = labelMean.get(m.getName());
                    Double[] v = new Double[data.getNumLabels()];
                    for (int j=0; j();
        for (String measureName : sums.keySet()) {            
            standardDeviation.put(measureName, Math.sqrt(sums.get(measureName) / size));
        }
        labelStandardDeviation = new HashMap();
        for (String measureName : labelSums.keySet()) {
            Double[] s = labelSums.get(measureName);
            for (int j=0; j




© 2015 - 2025 Weber Informatics LLC | Privacy Policy