Java List Create createTargetSorted(List labels, Comparator comp)

Here you can find the source of createTargetSorted(List labels, Comparator comp)

Description

create Target Sorted

License

Open Source License

Declaration

public static <T> List<Double> createTargetSorted(List<T> labels, Comparator<T> comp) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

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

public class Main {
    public static <T> List<Double> createTargetSorted(List<T> labels, Comparator<T> comp) {
        return createTargetSorted(labels, new HashMap<T, Double>(), comp);
    }/* w ww .  j a  v  a 2s.com*/

    /**
     * Convert a list of arbitary labels into al list of Doubles which is used as the training target in LibSVM/LINEAR
     * 
     * @param labels, this list needs to be sortable, i.e. we need a Comparator for T
     * @param labelMap
     * @return
     */
    public static <T> List<Double> createTargetSorted(List<T> labels, Map<T, Double> labelMap, Comparator<T> comp) {
        List<T> sorted = new ArrayList<T>(labels);
        Collections.sort(sorted, comp);

        double t = 0;

        for (T label : sorted) {
            if (!labelMap.containsKey(label)) {
                t += 1;
                labelMap.put(label, t);
            }
        }

        return createTarget(labels, labelMap);
    }

    public static <T> List<Double> createTarget(List<T> labels) {
        return createTarget(labels, new HashMap<T, Double>());
    }

    public static <T> List<Double> createTarget(List<T> labels, Map<T, Double> labelMap) {
        List<Double> target = new ArrayList<Double>();
        double t = 0;

        for (T label : labels) {
            if (!labelMap.containsKey(label)) {
                t += 1;
                labelMap.put(label, t);
            }
            target.add((double) labelMap.get(label));
        }
        return target;
    }
}

Related

  1. createSublist(final T[] elements, final Class listImplementation)
  2. createSymbolNameString_From_Parts(List descTagName_Parts)
  3. createTable(List datas, String[] headers, int numColumns, int cellPadding, int border)
  4. createTagList(List tags, String separator)
  5. createTarget(List labels)
  6. createTuple(List> tuples, List entries, List> lists)
  7. createUniqueProducts(List list1, List list2)
  8. createUnmodifiableList(Collection coll)
  9. createUpdateMeasurementItemList(final String itemList, final String updateItem)