use of edu.neu.ccs.pyramid.regression.LinearRule 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;
}
Aggregations