compute Entropy - Java java.lang

Java examples for java.lang:Math Algorithm

Description

compute Entropy

Demo Code


//package com.java2s;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {


    /**//from  w ww. j  ava2s .  c  o  m
     * 
     * @param inputData
     *            : list of un-splitted string separated by separator
     * @param rowIds
     *            : contains row id's starting from zero column index
     * @param targetColumnIndex
     *            : based on 1 column index not 0 based
     * @param
     * @return
     */

    public static double computeEntropy(List<String> inputData,
            List<Integer> rowIds, int targetColumnIndex, String separator) {
        Map<String, Integer> classDistribution = new HashMap<>();
        double entropy = 0.0;
        String classLabel = null;
        // if we can avoid redundant data rows split operation somehow we can
        // improve performance.

        for (int rowid : rowIds) {
            classLabel = inputData.get(rowid).split(separator)[targetColumnIndex - 1];
            if (!classDistribution.containsKey(classLabel)) {
                classDistribution.put(classLabel, 1);
                continue;
            }
            classDistribution.put(classLabel,
                    classDistribution.get(classLabel) + 1);
        }

        int noOfDataPoints = rowIds.size();
        double probability = 0.0;
        for (int countOfClass : classDistribution.values()) {
            probability = ((double) countOfClass) / noOfDataPoints;
            entropy = entropy - probability * Math.log(probability);
        }
        return entropy;
    }
}

Related Tutorials