package model.supervised.bagging;

import data.DataSet;
import utils.random.RandomUtils;
import utils.sort.SortIntDoubleUtils;

/**
 * Created by hanxuan on 11/3/15 for machine_learning.
 */
public class BaggingClassification extends BootstrapAveraging{

    private int classCount = 0;

    @Override
    public double predict(double[] feature) {

        double[] votes = new double[classCount];
        for (int i = 0; i < predictables.length; i++) {
            votes[(int) predictables[i].predict(feature)] += 1;
        }
        int[] indexes = RandomUtils.getIndexes(classCount);
        SortIntDoubleUtils.sort(indexes, votes);
        return indexes[classCount - 1];
    }

    @Override

    public void initialize(DataSet d) {
        super.initialize(d);
        classCount = d.getLabels().getIndexClassMap().size();
    }
}

@@ -49,16 +49,49 @@
 ataSet;%0A
+import utils.array.ArraySumUtil;%0A
 import u
@@ -115,16 +115,16 @@
 mUtils;%0A
-
 import u
@@ -404,161 +404,23 @@
 s = 
-new double%5BclassCount%5D;%0A        for (int i = 0; i %3C predictables.length; i++) %7B%0A            votes%5B(int) predictables%5Bi%5D.predict(feature)%5D += 1;%0A        %7D
+probs(feature);
 %0A   
@@ -586,17 +586,16 @@
 verride%0A
-%0A
     publ
@@ -655,16 +655,16 @@
 ize(d);%0A
-
         
@@ -722,11 +722,289 @@
 );%0A    %7D
+%0A%0A    public double%5B%5D probs(double%5B%5D feature) %7B%0A        double%5B%5D votes = new double%5BclassCount%5D;%0A        for (int i = 0; i %3C predictables.length; i++) %7B%0A            votes%5B(int) predictables%5Bi%5D.predict(feature)%5D += 1;%0A        %7D%0A        return ArraySumUtil.normalize(votes);%0A    %7D
 %0A%7D%0A

add probs predict