@Test public void testGetFeatureToPercentileRankMapping() { SafeMap<String,Double> featuresToValue = new SafeHashMap<>(); featuresToValue.putNewKey("a",0.0); featuresToValue.putNewKey("b",0.1); featuresToValue.putNewKey("c",-0.1); featuresToValue.putNewKey("d",0.0); SafeMap<String, Double> featureToPercentileRank = FeatureAndValue.getFeatureToPercentileRankMapping(featuresToValue); double epsilon = 1e-5; assertEquals(0.5,featureToPercentileRank.getExisting("a"), epsilon); assertEquals(1,featureToPercentileRank.getExisting("b"), epsilon); assertEquals(0,featureToPercentileRank.getExisting("c"), epsilon); assertEquals(0.5,featureToPercentileRank.getExisting("d"), epsilon); }
static SafeMap<String, Double> getFeatureToPercentileRankMapping(SafeMap<String,Double> featureToValue) { Verify.verify(!featureToValue.isEmpty()); ArrayList<FeatureAndValue> featureValueArray = featureToValue.entrySet().stream() .map(e-> new FeatureAndValue(e.getKey(), e.getValue())) .collect(Collectors.toCollection(ArrayList::new)); NaturalRanking naturalRanking = new NaturalRanking(TiesStrategy.AVERAGE); double[] weights = Doubles.toArray(featureValueArray.stream() .map(x->x.value) .collect(Collectors.toList())); double[] ranks = naturalRanking.rank(weights); SafeMap<String, Double> result = new SafeHashMap<>(); for(int i=0; i<ranks.length; i++){ double percentileRank = (ranks[i] - 1) / (ranks.length-1); result.putNewKey(featureValueArray.get(i).featureName, percentileRank); } return result; }
FeatureAndValue { static SafeMap<String, Double> getFeatureToPercentileRankMapping(SafeMap<String,Double> featureToValue) { Verify.verify(!featureToValue.isEmpty()); ArrayList<FeatureAndValue> featureValueArray = featureToValue.entrySet().stream() .map(e-> new FeatureAndValue(e.getKey(), e.getValue())) .collect(Collectors.toCollection(ArrayList::new)); NaturalRanking naturalRanking = new NaturalRanking(TiesStrategy.AVERAGE); double[] weights = Doubles.toArray(featureValueArray.stream() .map(x->x.value) .collect(Collectors.toList())); double[] ranks = naturalRanking.rank(weights); SafeMap<String, Double> result = new SafeHashMap<>(); for(int i=0; i<ranks.length; i++){ double percentileRank = (ranks[i] - 1) / (ranks.length-1); result.putNewKey(featureValueArray.get(i).featureName, percentileRank); } return result; } }
FeatureAndValue { static SafeMap<String, Double> getFeatureToPercentileRankMapping(SafeMap<String,Double> featureToValue) { Verify.verify(!featureToValue.isEmpty()); ArrayList<FeatureAndValue> featureValueArray = featureToValue.entrySet().stream() .map(e-> new FeatureAndValue(e.getKey(), e.getValue())) .collect(Collectors.toCollection(ArrayList::new)); NaturalRanking naturalRanking = new NaturalRanking(TiesStrategy.AVERAGE); double[] weights = Doubles.toArray(featureValueArray.stream() .map(x->x.value) .collect(Collectors.toList())); double[] ranks = naturalRanking.rank(weights); SafeMap<String, Double> result = new SafeHashMap<>(); for(int i=0; i<ranks.length; i++){ double percentileRank = (ranks[i] - 1) / (ranks.length-1); result.putNewKey(featureValueArray.get(i).featureName, percentileRank); } return result; } FeatureAndValue(String featureName, double value); }
FeatureAndValue { static SafeMap<String, Double> getFeatureToPercentileRankMapping(SafeMap<String,Double> featureToValue) { Verify.verify(!featureToValue.isEmpty()); ArrayList<FeatureAndValue> featureValueArray = featureToValue.entrySet().stream() .map(e-> new FeatureAndValue(e.getKey(), e.getValue())) .collect(Collectors.toCollection(ArrayList::new)); NaturalRanking naturalRanking = new NaturalRanking(TiesStrategy.AVERAGE); double[] weights = Doubles.toArray(featureValueArray.stream() .map(x->x.value) .collect(Collectors.toList())); double[] ranks = naturalRanking.rank(weights); SafeMap<String, Double> result = new SafeHashMap<>(); for(int i=0; i<ranks.length; i++){ double percentileRank = (ranks[i] - 1) / (ranks.length-1); result.putNewKey(featureValueArray.get(i).featureName, percentileRank); } return result; } FeatureAndValue(String featureName, double value); static List<FeatureAndValue> readFromSempreWeightFile(Path weightFile); static SafeMap<String,Double> readMapFromSempreWeightFile(Path weightFile); static void compareTwoSempreWeightFiles(Path weightFile1, Path weightFile2, Path outputCsv,
                                                   Path outputMoreInfo); }
FeatureAndValue { static SafeMap<String, Double> getFeatureToPercentileRankMapping(SafeMap<String,Double> featureToValue) { Verify.verify(!featureToValue.isEmpty()); ArrayList<FeatureAndValue> featureValueArray = featureToValue.entrySet().stream() .map(e-> new FeatureAndValue(e.getKey(), e.getValue())) .collect(Collectors.toCollection(ArrayList::new)); NaturalRanking naturalRanking = new NaturalRanking(TiesStrategy.AVERAGE); double[] weights = Doubles.toArray(featureValueArray.stream() .map(x->x.value) .collect(Collectors.toList())); double[] ranks = naturalRanking.rank(weights); SafeMap<String, Double> result = new SafeHashMap<>(); for(int i=0; i<ranks.length; i++){ double percentileRank = (ranks[i] - 1) / (ranks.length-1); result.putNewKey(featureValueArray.get(i).featureName, percentileRank); } return result; } FeatureAndValue(String featureName, double value); static List<FeatureAndValue> readFromSempreWeightFile(Path weightFile); static SafeMap<String,Double> readMapFromSempreWeightFile(Path weightFile); static void compareTwoSempreWeightFiles(Path weightFile1, Path weightFile2, Path outputCsv,
                                                   Path outputMoreInfo); final String featureName; final double value; }