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;
}
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;
}
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();
}
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;
}
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);
}
}
Aggregations