Example usage for java.util BitSet clear

List of usage examples for java.util BitSet clear

Introduction

In this page you can find the example usage for java.util BitSet clear.

Prototype

public void clear() 

Source Link

Document

Sets all of the bits in this BitSet to false .

Usage

From source file:hivemall.smile.classification.GradientTreeBoostingClassifierUDTF.java

/**
 * Train L-k tree boost.//from  w w w  . java2 s.c  o  m
 */
private void traink(final double[][] x, final int[] y, final int k) throws HiveException {
    final int numVars = SmileExtUtils.computeNumInputVars(_numVars, x);
    if (logger.isInfoEnabled()) {
        logger.info("k: " + k + ", numTrees: " + _numTrees + ", shirinkage: " + _eta + ", subsample: "
                + _subsample + ", numVars: " + numVars + ", minSamplesSplit: " + _minSamplesSplit
                + ", maxDepth: " + _maxDepth + ", maxLeafs: " + _maxLeafNodes + ", seed: " + _seed);
    }

    final int numInstances = x.length;
    final int numSamples = (int) Math.round(numInstances * _subsample);

    final double[][] h = new double[k][numInstances]; // boost tree output.
    final double[][] p = new double[k][numInstances]; // posteriori probabilities.
    final double[][] response = new double[k][numInstances]; // pseudo response.

    final int[][] order = SmileExtUtils.sort(_attributes, x);
    final RegressionTree.NodeOutput[] output = new LKNodeOutput[k];
    for (int i = 0; i < k; i++) {
        output[i] = new LKNodeOutput(response[i], k);
    }

    final BitSet sampled = new BitSet(numInstances);
    final int[] bag = new int[numSamples];
    final int[] perm = new int[numSamples];
    for (int i = 0; i < numSamples; i++) {
        perm[i] = i;
    }

    long s = (this._seed == -1L) ? SmileExtUtils.generateSeed() : new smile.math.Random(_seed).nextLong();
    final smile.math.Random rnd1 = new smile.math.Random(s);
    final smile.math.Random rnd2 = new smile.math.Random(rnd1.nextLong());

    // out-of-bag prediction
    final int[] prediction = new int[numInstances];

    for (int m = 0; m < _numTrees; m++) {
        for (int i = 0; i < numInstances; i++) {
            double max = Double.NEGATIVE_INFINITY;
            for (int j = 0; j < k; j++) {
                final double h_ji = h[j][i];
                if (max < h_ji) {
                    max = h_ji;
                }
            }
            double Z = 0.0d;
            for (int j = 0; j < k; j++) {
                double p_ji = Math.exp(h[j][i] - max);
                p[j][i] = p_ji;
                Z += p_ji;
            }
            for (int j = 0; j < k; j++) {
                p[j][i] /= Z;
            }
        }

        final RegressionTree[] trees = new RegressionTree[k];

        Arrays.fill(prediction, -1);
        double max_h = Double.NEGATIVE_INFINITY;
        int oobTests = 0, oobErrors = 0;

        for (int j = 0; j < k; j++) {
            reportProgress(_progressReporter);

            final double[] response_j = response[j];
            final double[] p_j = p[j];
            final double[] h_j = h[j];

            for (int i = 0; i < numInstances; i++) {
                if (y[i] == j) {
                    response_j[i] = 1.0d;
                } else {
                    response_j[i] = 0.0d;
                }
                response_j[i] -= p_j[i];
            }

            SmileExtUtils.shuffle(perm, rnd1);
            for (int i = 0; i < numSamples; i++) {
                int index = perm[i];
                bag[i] = index;
                sampled.set(i);
            }

            RegressionTree tree = new RegressionTree(_attributes, x, response[j], numVars, _maxDepth,
                    _maxLeafNodes, _minSamplesSplit, _minSamplesLeaf, order, bag, output[j], rnd2);
            trees[j] = tree;

            for (int i = 0; i < numInstances; i++) {
                double h_ji = h_j[i] + _eta * tree.predict(x[i]);
                h_j[i] += h_ji;
                if (h_ji > max_h) {
                    max_h = h_ji;
                    prediction[i] = j;
                }
            }

        } // for each k

        // out-of-bag error estimate
        for (int i = sampled.nextClearBit(0); i < numInstances; i = sampled.nextClearBit(i + 1)) {
            oobTests++;
            if (prediction[i] != y[i]) {
                oobErrors++;
            }
        }
        sampled.clear();
        float oobErrorRate = 0.f;
        if (oobTests > 0) {
            oobErrorRate = ((float) oobErrors) / oobTests;
        }

        // forward a row
        forward(m + 1, 0.d, _eta, oobErrorRate, trees);

    } // for each m
}