package edu.neu.ccs.pyramid.multilabel_classification.imlgb;

import edu.neu.ccs.pyramid.dataset.MultiLabel;
import edu.neu.ccs.pyramid.multilabel_classification.PluginPredictor;
import org.apache.mahout.math.Vector;

/**
 * optimal for Hamming Loss
 * Created by chengli on 4/6/16.
 */
public class HammingPredictor implements PluginPredictor<IMLGradientBoosting>{


    private static final long serialVersionUID = 1L;
    private IMLGradientBoosting imlGradientBoosting;


    public HammingPredictor(IMLGradientBoosting imlGradientBoosting) {
        this.imlGradientBoosting = imlGradientBoosting;
    }

    @Override
    public IMLGradientBoosting getModel() {
        return imlGradientBoosting;
    }

    @Override
    public MultiLabel predict(Vector vector) {
        MultiLabel prediction = new MultiLabel();
        for (int k=0;k<getNumClasses();k++){
            double score = imlGradientBoosting.predictClassScore(vector, k);
            if (score > 0){
                prediction.addLabel(k);
            }
        }
        return prediction;
    }

    public double predictAssignmentProb(Vector vector, MultiLabel assignment){
        if (assignment.outOfBound(imlGradientBoosting.getNumClasses())){
            return 0;
        }
        return imlGradientBoosting.predictAssignmentProbWithoutConstraint(vector,assignment);
    }

}

@@ -819,57 +819,8 @@
 ();%0A
-        for (int k=0;k%3CgetNumClasses();k++)%7B%0A    
     
@@ -833,14 +833,17 @@
 uble
+%5B%5D
  score
+s
  = i
@@ -882,20 +882,74 @@
 core
-(vector, k);
+sCachedInput(vector);%0A        for (int k=0;k%3CgetNumClasses();k++)%7B
 %0A   
@@ -966,16 +966,20 @@
 f (score
+s%5Bk%5D
  %3E 0)%7B%0A 

use cached input in Hamming predictor