public void selectFeatures ( int numFeatures , double [ ] scores ) { List < ScoredObject < F >> scoredFeatures = new ArrayList < ScoredObject < F > > ( ) ; for ( int i = 0 ; i < scores . length ; i ++ ) { scoredFeatures . add ( new ScoredObject < F > ( featureIndex . get ( i ) , scores [ i ] ) ) ; } Collections . sort ( scoredFeatures , ScoredComparator . DESCENDING_COMPARATOR ) ; Index < F > newFeatureIndex = new HashIndex < F > ( ) ; for ( int i = 0 ; i < scoredFeatures . size ( ) && i < numFeatures ; i ++ ) { newFeatureIndex . add ( scoredFeatures . get ( i ) . object ( ) ) ; //System.err.println(scoredFeatures.get(i)); } for ( int i = 0 ; i < size ; i ++ ) { int [ ] newData = new int [ data [ i ] . length ] ; int curIndex = 0 ; for ( int j = 0 ; j < data [ i ] . length ; j ++ ) { int index ; if ( ( index = newFeatureIndex . indexOf ( featureIndex . get ( data [ i ] [ j ] ) ) ) != - 1 ) { newData [ curIndex ++ ] = index ; } } int [ ] newDataTrimmed = new int [ curIndex ] ; System . arraycopy ( newData , 0 , newDataTrimmed , 0 , curIndex ) ; data [ i ] = newDataTrimmed ; } featureIndex = newFeatureIndex ; }
Generic method to select features based on the feature scores vector provided as an argument .