package edu.neu.ccs.pyramid.classification.logistic_regression;

import edu.neu.ccs.pyramid.util.Pair;
import org.apache.mahout.math.Vector;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;


/**
 * Created by chengli on 12/7/14.
 */
public class LogisticRegressonInspector {
    public static List<Pair<Integer,String>> topFeatures(LogisticRegression logisticRegression,
                                                         int k){
        String[] featureNames = logisticRegression.getFeatureNames();
        Vector weights = logisticRegression.getWeights().getWeightsWithoutBiasForClass(k);
        Comparator<Pair<Integer,Double>> comparator = Comparator.comparing(Pair::getSecond);
        return IntStream.range(0,weights.size()).mapToObj(i -> new Pair<>(i,weights.get(i)))
                .filter(pair -> pair.getSecond()>0)
                .sorted(comparator.reversed())
                .map(Pair::getFirst)
                .map(i -> new Pair<>(i, featureNames[i]))
                .collect(Collectors.toList());
    }

    public static List<Pair<Integer,String>> topFeatures(LogisticRegression logisticRegression,
                                                         int k,
                                                         int limit){
        String[] featureNames = logisticRegression.getFeatureNames();
        Vector weights = logisticRegression.getWeights().getWeightsWithoutBiasForClass(k);
        Comparator<Pair<Integer,Double>> comparator = Comparator.comparing(Pair::getSecond);
        return IntStream.range(0,weights.size()).mapToObj(i -> new Pair<>(i,weights.get(i)))
                .filter(pair -> pair.getSecond()>0)
                .sorted(comparator.reversed())
                .map(Pair::getFirst)
                .map(i -> new Pair<>(i, featureNames[i]))
                .limit(limit)
                .collect(Collectors.toList());
    }
}

@@ -58,16 +58,67 @@
 ssion;%0A%0A
+import edu.neu.ccs.pyramid.feature.FeatureUtility;%0A
 import e
@@ -418,36 +418,30 @@
 ic List%3C
-Pair%3CInteger,String%3E
+FeatureUtility
 %3E topFea
@@ -730,36 +730,30 @@
 parator%3C
-Pair%3CInteger,Double%3E
+FeatureUtility
 %3E compar
@@ -784,40 +784,72 @@
 ing(
-Pair::getSecond);%0A        return
+FeatureUtility::getUtility);%0A        List%3CFeatureUtility%3E list =
  Int
@@ -870,32 +870,49 @@
 ,weights.size())
+%0A                
 .mapToObj(i -%3E n
@@ -918,227 +918,312 @@
 new 
-Pair%3C%3E(i,weights.get(i)))%0A                .filter(pair -%3E pair.getSecond()%3E0)%0A                .sorted(comparator.reversed())%0A                .map(Pair::getFirst)%0A                .map(i -%3E new Pair%3C%3E(i, featureNames%5Bi%5D
+FeatureUtility(i,featureNames%5Bi%5D).setUtility(weights.get(i)))%0A                .filter(featureUtility -%3E featureUtility.getUtility()%3E0)%0A                .sorted(comparator.reversed())%0A                .collect(Collectors.toList());%0A        IntStream.range(0,list.size()).forEach(i-%3E list.get(i).setRank(i
 ))
+;
 %0A   
@@ -1231,45 +1231,19 @@
     
-        .collect(Collectors.toList())
+return list
 ;%0A  
@@ -1274,28 +1274,22 @@
 ist%3C
-Pair%3CInteger,String%3E
+FeatureUtility
 %3E to
@@ -1482,587 +1482,70 @@
     
-String%5B%5D featureNames = logisticRegression.getFeatureNames();%0A        Vector weights = logisticRegression.getWeights().getWeightsWithoutBiasForClass(k);%0A        Comparator%3CPair%3CInteger,Double%3E%3E comparator = Comparator.comparing(Pair::getSecond);%0A        return IntStream.range(0,weights.size()).mapToObj(i -%3E new Pair%3C%3E(i,weights.get(i)))%0A                .filter(pair -%3E pair.getSecond()%3E0)%0A                .sorted(comparator.reversed())%0A                .map(Pair::getFirst)%0A                .map(i -%3E new Pair%3C%3E(i, featureNames%5Bi%5D))%0A                .limit(limit)%0A                
+return topFeatures(logisticRegression,k).stream().limit(limit)
 .col
@@ -1577,10 +1577,12 @@
 ;%0A    %7D%0A
+%0A%0A
 %7D%0A

change return type