Computes the normalized confusion matrix for two vectors. - Java java.lang

Java examples for java.lang:Math Matrix

Description

Computes the normalized confusion matrix for two vectors.

Demo Code

/** A collection of mathematical utility functions.
 * <p>// ww  w. ja v a 2  s.  c  o m
 * Copyright (c) 2008 Eric Eaton
 * <p>
 * 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 3 of the License, or
 * (at your option) any later version.
 * <p>
 * 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.
 * <p>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/.
 * 
 * @author Eric Eaton (EricEaton@umbc.edu) <br>
 *         University of Maryland Baltimore County
 * 
 * @version 0.1
 *
 */
//package com.java2s;

import java.util.HashSet;

public class Main {
    /** Computes the normalized confusion matrix for two vectors.
     * @param p the first vector
     * @param q the second vector
     * @return the normalized confusion matrix for p and q
     */
    public static double[][] getConfusionMatrix(int[] p, int[] q) {

        if (p.length != q.length) {
            throw new IllegalArgumentException(
                    "p and q must be the same length.");
        }

        int[] classes = uniqueValues(append(p, q));
        int n = p.length;

        // compute the confusion matrix
        double[][] confusionMatrix = new double[classes.length][classes.length];
        for (int i = 0; i < n; i++) {
            // determine the classIdx of p[i]
            int piClassIdx;
            for (piClassIdx = 0; piClassIdx < classes.length; piClassIdx++) {
                if (p[i] == classes[piClassIdx])
                    break;
            }
            // determine the classIdx of q[i]
            int qiClassIdx;
            for (qiClassIdx = 0; qiClassIdx < classes.length; qiClassIdx++) {
                if (q[i] == classes[qiClassIdx])
                    break;
            }
            // increment the counter in the confusion matrix
            confusionMatrix[piClassIdx][qiClassIdx]++;
        }

        // normalize the confusion matrix
        for (int i = 0; i < confusionMatrix.length; i++) {
            for (int j = 0; j < confusionMatrix.length; j++) {
                confusionMatrix[i][j] /= n;
            }
        }

        return confusionMatrix;
    }

    /** Determines the unique values of v.  The values are returned in no particular order.
     * @param v
     * @return the unique values of v in no particular order.
     */
    public static int[] uniqueValues(int[] v) {
        // form the values into a set, which automatically removes duplicates
        HashSet<Integer> uniqueValues = new HashSet<Integer>();
        for (int i = 0; i < v.length; i++) {
            uniqueValues.add(v[i]);
        }
        // convert the set back into an array
        int[] vUnique = new int[uniqueValues.size()];
        int i = 0;
        for (Integer uniqueValue : uniqueValues) {
            vUnique[i++] = uniqueValue;
        }
        return vUnique;
    }

    /** Determines the unique values of v.  The values are returned in no particular order.
     * @param v
     * @return the unique values of v in no particular order.
     */
    public static double[] uniqueValues(double[] v) {
        // form the values into a set, which automatically removes duplicates
        HashSet<Double> uniqueValues = new HashSet<Double>();
        for (int i = 0; i < v.length; i++) {
            uniqueValues.add(v[i]);
        }
        // convert the set back into an array
        double[] vUnique = new double[uniqueValues.size()];
        int i = 0;
        for (Double uniqueValue : uniqueValues) {
            vUnique[i++] = uniqueValue;
        }
        return vUnique;
    }

    /** Appends an element to a vector.
     * @param v1 the vector.
     * @param d the element to append.
     * @return A vector containing all the elements of v1 followed
     * by d.
     */
    public static int[] append(int[] v1, int d) {
        int[] newVector = new int[v1.length + 1];
        System.arraycopy(v1, 0, newVector, 0, v1.length);
        newVector[v1.length] = d;
        return newVector;
    }

    /** Appends an element to a vector.
     * @param v1 the vector.
     * @param d the element to append.
     * @return A vector containing all the elements of v1 followed
     * by d.
     */
    public static double[] append(double[] v1, double d) {
        double[] newVector = new double[v1.length + 1];
        System.arraycopy(v1, 0, newVector, 0, v1.length);
        newVector[v1.length] = d;
        return newVector;
    }

    /** Appends two vectors.
     * @param v1 the first vector.
     * @param v2 the second vector.
     * @return A vector containing all the elements of v1 followed
     * by all the elements of v2.
     */
    public static double[] append(double[] v1, double[] v2) {
        double[] newVector = new double[v1.length + v2.length];
        System.arraycopy(v1, 0, newVector, 0, v1.length);
        System.arraycopy(v2, 0, newVector, v1.length, v2.length);
        return newVector;
    }

    /** Appends two vectors.
     * @param v1 the first vector.
     * @param v2 the second vector.
     * @return A vector containing all the elements of v1 followed
     * by all the elements of v2.
     */
    public static int[] append(int[] v1, int[] v2) {
        int[] newVector = new int[v1.length + v2.length];
        System.arraycopy(v1, 0, newVector, 0, v1.length);
        System.arraycopy(v2, 0, newVector, v1.length, v2.length);
        return newVector;
    }
}

Related Tutorials