Search in sources :

Example 11 with Feature

use of edu.neu.ccs.pyramid.feature.Feature in project pyramid by cheng-li.

the class MLLogisticRegressionInspector method decisionProcess.

public static ClassScoreCalculation decisionProcess(MLLogisticRegression logisticRegression, LabelTranslator labelTranslator, Vector vector, int classIndex, int limit) {
    ClassScoreCalculation classScoreCalculation = new ClassScoreCalculation(classIndex, labelTranslator.toExtLabel(classIndex), logisticRegression.predictClassScore(vector, classIndex));
    List<LinearRule> linearRules = new ArrayList<>();
    Rule bias = new ConstantRule(logisticRegression.getWeights().getBiasForClass(classIndex));
    classScoreCalculation.addRule(bias);
    for (int j = 0; j < logisticRegression.getNumFeatures(); j++) {
        Feature feature = logisticRegression.getFeatureList().get(j);
        double weight = logisticRegression.getWeights().getWeightsWithoutBiasForClass(classIndex).get(j);
        double featureValue = vector.get(j);
        double score = weight * featureValue;
        LinearRule rule = new LinearRule();
        rule.setFeature(feature);
        rule.setFeatureValue(featureValue);
        rule.setScore(score);
        rule.setWeight(weight);
        linearRules.add(rule);
    }
    Comparator<LinearRule> comparator = Comparator.comparing(decision -> Math.abs(decision.getScore()));
    List<LinearRule> sorted = linearRules.stream().sorted(comparator.reversed()).limit(limit).collect(Collectors.toList());
    for (LinearRule linearRule : sorted) {
        classScoreCalculation.addRule(linearRule);
    }
    return classScoreCalculation;
}
Also used : ConstantRule(edu.neu.ccs.pyramid.regression.ConstantRule) ClassScoreCalculation(edu.neu.ccs.pyramid.regression.ClassScoreCalculation) ArrayList(java.util.ArrayList) LinearRule(edu.neu.ccs.pyramid.regression.LinearRule) ConstantRule(edu.neu.ccs.pyramid.regression.ConstantRule) Rule(edu.neu.ccs.pyramid.regression.Rule) LinearRule(edu.neu.ccs.pyramid.regression.LinearRule) Feature(edu.neu.ccs.pyramid.feature.Feature)

Example 12 with Feature

use of edu.neu.ccs.pyramid.feature.Feature in project pyramid by cheng-li.

the class IMLGBInspector method topFeatures.

//todo: consider newton step and learning rate
/**
     * only trees are considered
     * @param boosting
     * @param classIndex
     * @return list of feature index and feature name pairs
     */
public static TopFeatures topFeatures(IMLGradientBoosting boosting, int classIndex, int limit) {
    Map<Feature, Double> totalContributions = new HashMap<>();
    List<Regressor> regressors = boosting.getRegressors(classIndex);
    List<RegressionTree> trees = regressors.stream().filter(regressor -> regressor instanceof RegressionTree).map(regressor -> (RegressionTree) regressor).collect(Collectors.toList());
    for (RegressionTree tree : trees) {
        Map<Feature, Double> contributions = RegTreeInspector.featureImportance(tree);
        for (Map.Entry<Feature, Double> entry : contributions.entrySet()) {
            Feature feature = entry.getKey();
            Double contribution = entry.getValue();
            double oldValue = totalContributions.getOrDefault(feature, 0.0);
            double newValue = oldValue + contribution;
            totalContributions.put(feature, newValue);
        }
    }
    Comparator<Map.Entry<Feature, Double>> comparator = Comparator.comparing(Map.Entry::getValue);
    List<Feature> list = totalContributions.entrySet().stream().sorted(comparator.reversed()).limit(limit).map(Map.Entry::getKey).collect(Collectors.toList());
    TopFeatures topFeatures = new TopFeatures();
    topFeatures.setTopFeatures(list);
    topFeatures.setClassIndex(classIndex);
    LabelTranslator labelTranslator = boosting.getLabelTranslator();
    topFeatures.setClassName(labelTranslator.toExtLabel(classIndex));
    return topFeatures;
}
Also used : MultiLabelPredictionAnalysis(edu.neu.ccs.pyramid.multilabel_classification.MultiLabelPredictionAnalysis) edu.neu.ccs.pyramid.regression(edu.neu.ccs.pyramid.regression) IntStream(java.util.stream.IntStream) java.util(java.util) DynamicProgramming(edu.neu.ccs.pyramid.multilabel_classification.DynamicProgramming) RegTreeInspector(edu.neu.ccs.pyramid.regression.regression_tree.RegTreeInspector) PluginPredictor(edu.neu.ccs.pyramid.multilabel_classification.PluginPredictor) Collectors(java.util.stream.Collectors) RegressionTree(edu.neu.ccs.pyramid.regression.regression_tree.RegressionTree) TreeRule(edu.neu.ccs.pyramid.regression.regression_tree.TreeRule) Feature(edu.neu.ccs.pyramid.feature.Feature) edu.neu.ccs.pyramid.dataset(edu.neu.ccs.pyramid.dataset) Vector(org.apache.mahout.math.Vector) TopFeatures(edu.neu.ccs.pyramid.feature.TopFeatures) Pair(edu.neu.ccs.pyramid.util.Pair) RegressionTree(edu.neu.ccs.pyramid.regression.regression_tree.RegressionTree) TopFeatures(edu.neu.ccs.pyramid.feature.TopFeatures) Feature(edu.neu.ccs.pyramid.feature.Feature)

Example 13 with Feature

use of edu.neu.ccs.pyramid.feature.Feature in project pyramid by cheng-li.

the class Checks method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < featureIndices.size(); i++) {
        int featureIndex = featureIndices.get(i);
        Feature featureName = features.get(i);
        double threshold = thresholds.get(i);
        double featureValue = values.get(i);
        boolean direction = directions.get(i);
        sb.append("feature ").append(featureIndex).append("(").append(featureName).append(")").append(" ");
        if (direction) {
            sb.append(featureValue).append("<=").append(threshold).append(", ");
        } else {
            sb.append(featureValue).append(">").append(threshold).append(", ");
        }
    }
    return sb.toString();
}
Also used : Feature(edu.neu.ccs.pyramid.feature.Feature)

Example 14 with Feature

use of edu.neu.ccs.pyramid.feature.Feature in project pyramid by cheng-li.

the class RegTreeInspector method featureImportance.

/**
     * pair contains feature name and reduction
     * @param tree
     * @return
     */
//    public static Map<Integer, Pair<String,Double>> featureImportance(RegressionTree tree){
//        List<Feature> featureList = tree.getFeatureList().getAll();
//        Map<Integer, Pair<String,Double>> map = new HashMap<>();
//        List<Node> nodes = tree.traverse();
//        nodes.stream().filter(node -> !node.isLeaf())
//                .forEach(node -> {
//                    int featureIndex = node.getFeatureIndex();
//                    String featureName = featureList.get(node.getFeatureIndex()).getName();
//                    double reduction = node.getReduction();
//                    Pair<String,Double> oldPair = map.getOrDefault(featureIndex, new Pair<>(featureName,0.0));
//                    Pair<String, Double> newPair = new Pair<>(featureName,oldPair.getSecond()+reduction);
//                    map.put(featureIndex, newPair);
//                });
//        return map;
//    }
public static Map<Feature, Double> featureImportance(RegressionTree tree) {
    FeatureList featureList = tree.getFeatureList();
    Map<Feature, Double> map = new HashMap<>();
    List<Node> nodes = tree.traverse();
    nodes.stream().filter(node -> !node.isLeaf()).forEach(node -> {
        int featureIndex = node.getFeatureIndex();
        Feature feature = featureList.get(featureIndex);
        double reduction = node.getReduction();
        double oldValue = map.getOrDefault(feature, 0.0);
        double newValue = reduction + oldValue;
        map.put(feature, newValue);
    });
    return map;
}
Also used : java.util(java.util) Feature(edu.neu.ccs.pyramid.feature.Feature) Vector(org.apache.mahout.math.Vector) FeatureList(edu.neu.ccs.pyramid.feature.FeatureList) Collectors(java.util.stream.Collectors) Pair(edu.neu.ccs.pyramid.util.Pair) FeatureList(edu.neu.ccs.pyramid.feature.FeatureList) Feature(edu.neu.ccs.pyramid.feature.Feature)

Example 15 with Feature

use of edu.neu.ccs.pyramid.feature.Feature in project pyramid by cheng-li.

the class TreeRule method add.

//todo deal with probabilities
public void add(RegressionTree tree, Node node, Vector vector) {
    List<Feature> featureList = tree.getFeatureList().getAll();
    if (node.isLeaf()) {
        this.score = node.getValue();
    } else {
        int featureIndex = node.getFeatureIndex();
        Feature feature = featureList.get(node.getFeatureIndex());
        double threshold = node.getThreshold();
        double featureValue = vector.get(featureIndex);
        if (Double.isNaN(featureValue)) {
            // todo this is a poor man's solution
            featureValue = -9999;
        }
        boolean direction = featureValue <= threshold;
        this.checks.featureIndices.add(featureIndex);
        this.checks.features.add(feature);
        this.checks.thresholds.add(threshold);
        this.checks.directions.add(direction);
        this.checks.values.add(featureValue);
        Node child;
        if (direction) {
            child = node.getLeftChild();
        } else {
            child = node.getRightChild();
        }
        add(tree, child, vector);
    }
}
Also used : Feature(edu.neu.ccs.pyramid.feature.Feature)

Aggregations

Feature (edu.neu.ccs.pyramid.feature.Feature)23 Vector (org.apache.mahout.math.Vector)14 FeatureList (edu.neu.ccs.pyramid.feature.FeatureList)13 Collectors (java.util.stream.Collectors)9 TopFeatures (edu.neu.ccs.pyramid.feature.TopFeatures)8 RegTreeInspector (edu.neu.ccs.pyramid.regression.regression_tree.RegTreeInspector)6 RegressionTree (edu.neu.ccs.pyramid.regression.regression_tree.RegressionTree)6 java.util (java.util)6 MultiLabelPredictionAnalysis (edu.neu.ccs.pyramid.multilabel_classification.MultiLabelPredictionAnalysis)5 edu.neu.ccs.pyramid.regression (edu.neu.ccs.pyramid.regression)5 TreeRule (edu.neu.ccs.pyramid.regression.regression_tree.TreeRule)5 ClassProbability (edu.neu.ccs.pyramid.classification.ClassProbability)4 edu.neu.ccs.pyramid.dataset (edu.neu.ccs.pyramid.dataset)4 LabelTranslator (edu.neu.ccs.pyramid.dataset.LabelTranslator)4 Pair (edu.neu.ccs.pyramid.util.Pair)4 IntStream (java.util.stream.IntStream)4 PredictionAnalysis (edu.neu.ccs.pyramid.classification.PredictionAnalysis)3 IdTranslator (edu.neu.ccs.pyramid.dataset.IdTranslator)3 IMLGradientBoosting (edu.neu.ccs.pyramid.multilabel_classification.imlgb.IMLGradientBoosting)3 ArrayList (java.util.ArrayList)3