public static Pair < List < Tuple > , List < Tuple > > splitData ( final List < Tuple > trainingData , double proportion ) { if ( proportion < 0 || proportion > 1 ) { throw new RuntimeException ( "Proportion should between 0.0 - 1.0" ) ; } if ( proportion > 0.5 ) { proportion = 1 - proportion ; } List < Tuple > smallList = new ArrayList <> ( ) ; List < Tuple > largeList = new ArrayList <> ( ) ; int smallListSize = ( int ) Math . floor ( proportion * trainingData . size ( ) ) ; int ct = 0 ; Set < Integer > indices = new HashSet <> ( ) ; while ( ct < smallListSize && trainingData . size ( ) > indices . size ( ) ) { int index = ( int ) ( Math . random ( ) * ( trainingData . size ( ) - 1 ) ) ; while ( indices . contains ( index ) ) { index = ( int ) ( Math . random ( ) * ( trainingData . size ( ) - 1 ) ) ; } indices . add ( index ) ; ct ++ ; } smallList . addAll ( indices . stream ( ) . map ( trainingData :: get ) . collect ( Collectors . toList ( ) ) ) ; IntStream . range ( 0 , trainingData . size ( ) ) . filter ( x -> ! indices . contains ( x ) ) . forEach ( i -> largeList . add ( trainingData . get ( i ) ) ) ; return new ImmutablePair <> ( smallList , largeList ) ; }
Shuffle the data and split by proportion