private double impurity(int[] count, int n) {
        double impurity = 0.0;

        switch (rule) {
            case GINI:
                impurity = 1.0;
                for (int i = 0; i < count.length; i++) {
                    if (count[i] > 0) {
                        double p = (double) count[i] / n;
                        impurity -= p * p;
                    }
                }
                break;

            case ENTROPY:
                for (int i = 0; i < count.length; i++) {
                    if (count[i] > 0) {
                        double p = (double) count[i] / n;
                        impurity -= p * Math.log2(p);
                    }
                }
                break;
            case CLASSIFICATION_ERROR:
                impurity = 0;
                for (int i = 0; i < count.length; i++) {
                    if (count[i] > 0) {
                        impurity = Math.max(impurity, count[i] / (double)n);
                    }
                }
                impurity = Math.abs(1 - impurity);
                break;
        }

        return impurity;
    }
private double impurity ( int [ ] count , int n ) { double impurity = 0.0 ; switch ( rule ) { case GINI : impurity = 1.0 ; for ( int i = 0 ; i < count . length ; i ++ ) { if ( count [ i ] > 0 ) { double p = ( double ) count [ i ] / n ; impurity -= p * p ; } } break ; case ENTROPY : for ( int i = 0 ; i < count . length ; i ++ ) { if ( count [ i ] > 0 ) { double p = ( double ) count [ i ] / n ; impurity -= p * Math . log2 ( p ) ; } } break ; case CLASSIFICATION_ERROR : impurity = 0 ; for ( int i = 0 ; i < count . length ; i ++ ) { if ( count [ i ] > 0 ) { impurity = Math . max ( impurity , count [ i ] / ( double ) n ) ; } } impurity = Math . abs ( 1 - impurity ) ; break ; } return impurity ; }
Returns the impurity of a node.
@param count the sample count in each class.
@param n the number of samples in the node.
@return  the impurity of a node