public static < T > double [ ] bootstrap ( int k , ClassifierTrainer < T > trainer , T [ ] x , int [ ] y ) { if ( k < 2 ) { throw new IllegalArgumentException ( "Invalid k for k-fold bootstrap: " + k ) ; } int n = x . length ; double [ ] results = new double [ k ] ; Accuracy measure = new Accuracy ( ) ; Bootstrap bootstrap = new Bootstrap ( n , k ) ; for ( int i = 0 ; i < k ; i ++ ) { T [ ] trainx = Math . slice ( x , bootstrap . train [ i ] ) ; int [ ] trainy = Math . slice ( y , bootstrap . train [ i ] ) ; Classifier < T > classifier = trainer . train ( trainx , trainy ) ; int nt = bootstrap . test [ i ] . length ; int [ ] truth = new int [ nt ] ; int [ ] predictions = new int [ nt ] ; for ( int j = 0 ; j < nt ; j ++ ) { int l = bootstrap . test [ i ] [ j ] ; truth [ j ] = y [ l ] ; predictions [ j ] = classifier . predict ( x [ l ] ) ; } results [ i ] = measure . measure ( truth , predictions ) ; } return results ; }
Bootstrap accuracy estimation of a classification model .