Search in sources :

Example 1 with ConstantRule

use of edu.neu.ccs.pyramid.regression.ConstantRule 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)

Aggregations

Feature (edu.neu.ccs.pyramid.feature.Feature)1 ClassScoreCalculation (edu.neu.ccs.pyramid.regression.ClassScoreCalculation)1 ConstantRule (edu.neu.ccs.pyramid.regression.ConstantRule)1 LinearRule (edu.neu.ccs.pyramid.regression.LinearRule)1 Rule (edu.neu.ccs.pyramid.regression.Rule)1 ArrayList (java.util.ArrayList)1