Example usage for org.apache.commons.lang3.tuple MutablePair setValue

List of usage examples for org.apache.commons.lang3.tuple MutablePair setValue

Introduction

In this page you can find the example usage for org.apache.commons.lang3.tuple MutablePair setValue.

Prototype

@Override
public R setValue(final R value) 

Source Link

Document

Sets the Map.Entry value.

Usage

From source file:ml.shifu.shifu.core.TreeModel.java

/**
 * Get feature importance of current model.
 * /*from  w w  w  . j  a va2s  .  c  om*/
 * @return map of feature importance, key is column index.
 */
public Map<Integer, MutablePair<String, Double>> getFeatureImportances() {
    Map<Integer, MutablePair<String, Double>> importancesSum = new HashMap<Integer, MutablePair<String, Double>>();
    Map<Integer, String> nameMapping = this.getIndependentTreeModel().getNumNameMapping();
    int treeSize = this.getIndependentTreeModel().getTrees().size();

    // such case we only support treeModel is one element list
    if (this.getIndependentTreeModel().getTrees().size() != 1) {
        throw new RuntimeException(
                "Bagging model cannot be supported in Tree Model one element feature importance computing.");
    }

    for (TreeNode tree : this.getIndependentTreeModel().getTrees().get(0)) {
        // get current tree importance at first
        Map<Integer, Double> subImportances = tree.computeFeatureImportance();
        // merge feature importance from different trees
        for (Entry<Integer, Double> entry : subImportances.entrySet()) {
            String featureName = nameMapping.get(entry.getKey());
            MutablePair<String, Double> importance = MutablePair.of(featureName, entry.getValue());
            if (!importancesSum.containsKey(entry.getKey())) {
                importance.setValue(importance.getValue() / treeSize);
                importancesSum.put(entry.getKey(), importance);
            } else {
                MutablePair<String, Double> current = importancesSum.get(entry.getKey());
                current.setValue(current.getValue() + importance.getValue() / treeSize);
                importancesSum.put(entry.getKey(), current);
            }
        }
    }
    return importancesSum;
}

From source file:ml.shifu.shifu.util.CommonUtils.java

private static Map<Integer, MutablePair<String, Double>> mergeImportanceList(
        List<Map<Integer, MutablePair<String, Double>>> list) {
    Map<Integer, MutablePair<String, Double>> finalResult = new HashMap<Integer, MutablePair<String, Double>>();
    int modelSize = list.size();
    for (Map<Integer, MutablePair<String, Double>> item : list) {
        for (Entry<Integer, MutablePair<String, Double>> entry : item.entrySet()) {
            if (!finalResult.containsKey(entry.getKey())) {
                // do average on models by dividing modelSize
                MutablePair<String, Double> value = MutablePair.of(entry.getValue().getKey(),
                        entry.getValue().getValue() / modelSize);
                finalResult.put(entry.getKey(), value);
            } else {
                MutablePair<String, Double> current = finalResult.get(entry.getKey());
                double entryValue = entry.getValue().getValue();
                current.setValue(current.getValue() + (entryValue / modelSize));
                finalResult.put(entry.getKey(), current);
            }/* w w  w  . j a va  2 s  .  c om*/
        }
    }
    return TreeModel.sortByValue(finalResult, false);
}