public static DataframeMatrix newInstance ( Dataframe dataset , boolean addConstantColumn , Map < Integer , Integer > recordIdsReference , Map < Object , Integer > featureIdsReference ) { if ( ! featureIdsReference . isEmpty ( ) ) { throw new IllegalArgumentException ( "The featureIdsReference map should be empty." ) ; } setStorageEngine ( dataset ) ; int n = dataset . size ( ) ; int d = dataset . xColumnSize ( ) ; if ( addConstantColumn ) { ++ d ; } DataframeMatrix m = new DataframeMatrix ( new MapRealMatrix ( n , d ) , new MapRealVector ( n ) ) ; if ( dataset . isEmpty ( ) ) { return m ; } boolean extractY = ( dataset . getYDataType ( ) == TypeInference . DataType . NUMERICAL ) ; int featureId = 0 ; if ( addConstantColumn ) { for ( int row = 0 ; row < n ; ++ row ) { m . X . setEntry ( row , featureId , 1.0 ) ; //put the constant in evey row } featureIdsReference . put ( Dataframe . COLUMN_NAME_CONSTANT , featureId ) ; ++ featureId ; } int rowId = 0 ; for ( Map . Entry < Integer , Record > e : dataset . entries ( ) ) { Integer rId = e . getKey ( ) ; Record r = e . getValue ( ) ; if ( recordIdsReference != null ) { recordIdsReference . put ( rId , rowId ) ; } if ( extractY ) { m . Y . setEntry ( rowId , TypeInference . toDouble ( r . getY ( ) ) ) ; } for ( Map . Entry < Object , Object > entry : r . getX ( ) . entrySet ( ) ) { Object feature = entry . getKey ( ) ; Integer knownFeatureId = featureIdsReference . get ( feature ) ; if ( knownFeatureId == null ) { featureIdsReference . put ( feature , featureId ) ; knownFeatureId = featureId ; ++ featureId ; } Double value = TypeInference . toDouble ( entry . getValue ( ) ) ; if ( value != null ) { m . X . setEntry ( rowId , knownFeatureId , value ) ; } //else the X matrix maintains the 0.0 default value } ++ rowId ; } return m ; }
Method used to generate a training Dataframe to a DataframeMatrix and extracts its contents to Matrixes . It populates the featureIdsReference map with the mappings between the feature names and the column ids of the matrix . Typically used to convert the training dataset .