use of edu.neu.ccs.pyramid.regression.regression_tree.RegressionTree in project pyramid by cheng-li.
the class HMLGBInspector 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(HMLGradientBoosting 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.regression.regression_tree.RegressionTree in project pyramid by cheng-li.
the class AdaBoostMHInspector method decisionProcess.
public static ClassScoreCalculation decisionProcess(AdaBoostMH boosting, MultiLabelClassifier.ClassProbEstimator scaling, LabelTranslator labelTranslator, Vector vector, int classIndex, int limit) {
ClassScoreCalculation classScoreCalculation = new ClassScoreCalculation(classIndex, labelTranslator.toExtLabel(classIndex), boosting.predictClassScore(vector, classIndex));
double prob = scaling.predictClassProb(vector, classIndex);
classScoreCalculation.setClassProbability(prob);
List<Regressor> regressors = boosting.getRegressors(classIndex);
List<TreeRule> treeRules = new ArrayList<>();
for (Regressor regressor : regressors) {
if (regressor instanceof ConstantRegressor) {
Rule rule = new ConstantRule(((ConstantRegressor) regressor).getScore());
classScoreCalculation.addRule(rule);
}
if (regressor instanceof RegressionTree) {
RegressionTree tree = (RegressionTree) regressor;
TreeRule treeRule = new TreeRule(tree, vector);
treeRules.add(treeRule);
}
}
Comparator<TreeRule> comparator = Comparator.comparing(decision -> Math.abs(decision.getScore()));
List<TreeRule> merged = TreeRule.merge(treeRules).stream().sorted(comparator.reversed()).limit(limit).collect(Collectors.toList());
for (TreeRule treeRule : merged) {
classScoreCalculation.addRule(treeRule);
}
return classScoreCalculation;
}
use of edu.neu.ccs.pyramid.regression.regression_tree.RegressionTree in project pyramid by cheng-li.
the class AdaBoostMHInspector method topFeatures.
public static TopFeatures topFeatures(AdaBoostMH 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.regression.regression_tree.RegressionTree in project pyramid by cheng-li.
the class StumpSelector method score.
private static double score(DataSet dataSet, double[] labels) {
RegTreeConfig regTreeConfig = new RegTreeConfig().setMaxNumLeaves(2);
RegressionTree tree = RegTreeTrainer.fit(regTreeConfig, dataSet, labels);
return tree.getRoot().getReduction();
}
Aggregations