#NEWLINE# def convert_adult( directory , output_directory , output_filename = 'adult.hdf5' ) : #NEWLINE# #INDENT# "\n    Convert the Adult dataset to HDF5.\n\n    Converts the Adult dataset to an HDF5 dataset compatible with\n    :class:`fuel.datasets.Adult`. The converted dataset is saved as\n    'adult.hdf5'.\n    This method assumes the existence of the file `adult.data` and\n    `adult.test`.\n\n    Parameters\n    ----------\n    directory : str\n        Directory in which input files reside.\n    output_directory : str\n        Directory in which to save the converted dataset.\n    output_filename : str, optional\n        Name of the saved dataset. Defaults to `adult.hdf5`.\n\n    Returns\n    -------\n    output_paths : tuple of str\n        Single-element tuple containing the path to the converted dataset.\n\n    " #NEWLINE# train_path = os . path . join ( directory , 'adult.data' ) #NEWLINE# test_path = os . path . join ( directory , 'adult.test' ) #NEWLINE# output_path = os . path . join ( output_directory , output_filename ) #NEWLINE# train_content = open ( train_path , 'r' ) . readlines ( ) #NEWLINE# test_content = open ( test_path , 'r' ) . readlines ( ) #NEWLINE# train_content = train_content [ : ( - 1 ) ] #NEWLINE# test_content = test_content [ 1 : ( - 1 ) ] #NEWLINE# features_list = [ ] #NEWLINE# targets_list = [ ] #NEWLINE# for content in [ train_content , test_content ] : #NEWLINE# #INDENT# content = [ line for line in content if ( line . find ( '?' ) == ( - 1 ) ) ] #NEWLINE# content = list ( map ( ( lambda l : l [ : ( - 1 ) ] . split ( ', ' ) ) , content ) ) #NEWLINE# features = list ( map ( ( lambda l : l [ : ( - 1 ) ] ) , content ) ) #NEWLINE# targets = list ( map ( ( lambda l : l [ ( - 1 ) ] ) , content ) ) #NEWLINE# del content #NEWLINE# y = list ( map ( ( lambda l : [ ( l [ 0 ] == '>' ) ] ) , targets ) ) #NEWLINE# y = numpy . array ( y ) #NEWLINE# del targets #NEWLINE# variables = [ 'age' , 'workclass' , 'fnlwgt' , 'education' , 'education-num' , 'marital-status' , 'occupation' , 'relationship' , 'race' , 'sex' , 'capital-gain' , 'capital-loss' , 'hours-per-week' , 'native-country' ] #NEWLINE# continuous = set ( [ 'age' , 'fnlwgt' , 'education-num' , 'capital-gain' , 'capital-loss' , 'hours-per-week' ] ) #NEWLINE# pieces = [ ] #NEWLINE# for ( i , var ) in enumerate ( variables ) : #NEWLINE# #INDENT# data = list ( map ( ( lambda l : l [ i ] ) , features ) ) #NEWLINE# if ( var in continuous ) : #NEWLINE# #INDENT# data = list ( map ( ( lambda l : float ( l ) ) , data ) ) #NEWLINE# data = numpy . array ( data ) #NEWLINE# data = data . reshape ( data . shape [ 0 ] , 1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# unique_values = list ( set ( data ) ) #NEWLINE# data = list ( map ( ( lambda l : unique_values . index ( l ) ) , data ) ) #NEWLINE# data = convert_to_one_hot ( data ) #NEWLINE# #UNINDENT# pieces . append ( data ) #NEWLINE# #UNINDENT# X = numpy . concatenate ( pieces , axis = 1 ) #NEWLINE# features_list . append ( X ) #NEWLINE# targets_list . append ( line ) #NEWLINE# #UNINDENT# features_list [ 1 ] = numpy . concatenate ( ( features_list [ 1 ] , numpy . zeros ( ( features_list [ 1 ] . shape [ 0 ] , 1 ) , dtype = features_list [ 1 ] . dtype ) ) , axis = 1 ) #NEWLINE# h5file = h5py . File ( output_path , mode = 'w' ) #NEWLINE# data = ( ( 'train' , 'features' , features_list [ 0 ] ) , ( 'train' , 'targets' , targets_list [ 0 ] ) , ( 'test' , 'features' , features_list [ 1 ] ) , ( 'test' , 'targets' , targets_list [ 1 ] ) ) #NEWLINE# fill_hdf5_file ( h5file , data ) #NEWLINE# h5file [ 'features' ] . dims [ 0 ] . label = 'batch' #NEWLINE# h5file [ 'features' ] . dims [ 1 ] . label = 'feature' #NEWLINE# h5file [ 'targets' ] . dims [ 0 ] . label = 'batch' #NEWLINE# h5file [ 'targets' ] . dims [ 1 ] . label = 'index' #NEWLINE# h5file . flush ( ) #NEWLINE# h5file . close ( ) #NEWLINE# return ( output_path , )