package data;

import data.core.Label;
import data.core.AMatrix;
import gnu.trove.list.array.TDoubleArrayList;

import java.util.Set;

/**
 * Created by hanxuan on 9/17/15.
 */

public class DataSet {

    private AMatrix featureMatrix = null;

    private Label labels = null;

    public DataSet(AMatrix m, Label l) {

        this.featureMatrix = m;
        this.labels = l;
    }

    public DataSet subDataSetByRow(int[] rowIndexes) {
        AMatrix subMatrix = featureMatrix.subMatrixByRow(rowIndexes);
        Label subLabels = labels.subLabelByRow(rowIndexes);
        return new DataSet(subMatrix, subLabels);
    }

    public void shiftCompressNorm() {
        featureMatrix.shiftCompressNormalize();
    }

    public void meanVarianceNorm(float[] mean, float[] sd) {
        featureMatrix.meanVarianceNormalize(mean, sd);
    }

    public void meanVarianceNorm() {
        featureMatrix.meanVarianceNormalize();
    }

    public double getEntry(int i, int j){

        return featureMatrix.getEntry(i, j);
    }

    public double[] getInstance(int i) {

        return featureMatrix.getRow(i);
    }

    public double[] getFeatureCol(int featureIndex) {
        return featureMatrix.getCol(featureIndex);
    }

    public double[] getFeatureColFilteredByLabel(int col, Set<Integer> ls) {

        TDoubleArrayList filterFeature = new TDoubleArrayList(getInstanceLength());
        double[] feature = featureMatrix.getCol(col);
        for (int i = 0; i < feature.length; i++) {
            if (ls.contains((int) getLabel(i))) filterFeature.add(feature[i]);
        }
        return filterFeature.toArray();
    }

    public double getLabel(int i) {

        return labels.getRow(i);
    }

    public double getCategoryProportion(int category) {
        return labels.getLabelQuotient(category);
    }

    public AMatrix getFeatureMatrix() {

        return featureMatrix;
    }

    public int getInstanceLength() {

        return featureMatrix.getInstanceLength();
    }

    public int getFeatureLength() {

        return featureMatrix.getFeatureLength();
    }

    public Label getLabels() {

        return labels;
    }
}

@@ -108,29 +108,331 @@
 st;%0A
-%0Aimport java.util.Set
+import gnu.trove.list.array.TIntArrayList;%0Aimport gnu.trove.set.hash.TIntHashSet;%0Aimport org.apache.commons.math3.distribution.EnumeratedIntegerDistribution;%0Aimport org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger;%0A%0Aimport java.util.Arrays;%0Aimport java.util.Set;%0Aimport java.util.stream.IntStream
 ;%0A%0A/
@@ -493,24 +493,94 @@
  DataSet %7B%0A%0A
+    private static Logger log = LogManager.getLogger(DataSet.class);%0A%0A
     private 
@@ -984,32 +984,1028 @@
 Labels);%0A    %7D%0A%0A
+    public DataSet balancedDataSet(int sampleSizeMax, int sampleSizeMin) %7B%0A%0A        TIntArrayList ids = new TIntArrayList(getInstanceLength());%0A        for (int i = 0; i %3C labels.getClassIndexMap().size(); i++) %7B%0A            int classIndex = i;%0A            int%5B%5D indexes = IntStream.range(0, getInstanceLength()).filter(j -%3E (int) getLabel(j) == classIndex).toArray();%0A            double%5B%5D probs = new double%5Bindexes.length%5D;%0A            Arrays.fill(probs, 1 / (double) indexes.length);%0A            EnumeratedIntegerDistribution integerDistribution = new EnumeratedIntegerDistribution(indexes, probs);%0A            int sampleSize = Math.max(Math.min(indexes.length, sampleSizeMax), sampleSizeMin);%0A            ids.addAll(integerDistribution.sample(sampleSize));%0A            log.info(%22balance sampling class %7B%7D %7C instances %7B%7D %7C sampling %7B%7D%22, i, indexes.length, sampleSize);%0A        %7D%0A        log.info(%22balance sampling total %7B%7D%22, ids.size());%0A%0A        return subDataSetByRow(ids.toArray());%0A    %7D%0A%0A
     public void 

add sampling method to build new dataset