@@ -199,7 +199,10 @@ def load_files(container_path, description=None, categories=None,
199199 target = target [indices ]
200200
201201 if load_content :
202- data = [open (filename , 'rb' ).read () for filename in filenames ]
202+ data = []
203+ for filename in filenames :
204+ with open (filename , 'rb' ) as f :
205+ data .append (f .read ())
203206 if encoding is not None :
204207 data = [d .decode (encoding , decode_error ) for d in data ]
205208 return Bunch (data = data ,
@@ -317,7 +320,8 @@ def load_digits(n_class=10):
317320 module_path = dirname (__file__ )
318321 data = np .loadtxt (join (module_path , 'data' , 'digits.csv.gz' ),
319322 delimiter = ',' )
320- descr = open (join (module_path , 'descr' , 'digits.rst' )).read ()
323+ with open (join (module_path , 'descr' , 'digits.rst' )) as f :
324+ descr = f .read ()
321325 target = data [:, - 1 ]
322326 flat_data = data [:, :- 1 ]
323327 images = flat_data .view ()
@@ -418,26 +422,31 @@ def load_boston():
418422 (506, 13)
419423 """
420424 module_path = dirname (__file__ )
421- data_file = csv .reader (open (join (module_path , 'data' ,
422- 'boston_house_prices.csv' )))
423- fdescr = open (join (module_path , 'descr' , 'boston_house_prices.rst' ))
424- temp = next (data_file )
425- n_samples = int (temp [0 ])
426- n_features = int (temp [1 ])
427- data = np .empty ((n_samples , n_features ))
428- target = np .empty ((n_samples ,))
429- temp = next (data_file ) # names of features
430- feature_names = np .array (temp )
431-
432- for i , d in enumerate (data_file ):
433- data [i ] = np .asarray (d [:- 1 ], dtype = np .float )
434- target [i ] = np .asarray (d [- 1 ], dtype = np .float )
425+
426+ fdescr_name = join (module_path , 'descr' , 'boston_house_prices.rst' )
427+ with open (fdescr_name ) as f :
428+ descr_text = f .read ()
429+
430+ data_file_name = join (module_path , 'data' , 'boston_house_prices.csv' )
431+ with open (data_file_name ) as f :
432+ data_file = csv .reader (f )
433+ temp = next (data_file )
434+ n_samples = int (temp [0 ])
435+ n_features = int (temp [1 ])
436+ data = np .empty ((n_samples , n_features ))
437+ target = np .empty ((n_samples ,))
438+ temp = next (data_file ) # names of features
439+ feature_names = np .array (temp )
440+
441+ for i , d in enumerate (data_file ):
442+ data [i ] = np .asarray (d [:- 1 ], dtype = np .float )
443+ target [i ] = np .asarray (d [- 1 ], dtype = np .float )
435444
436445 return Bunch (data = data ,
437446 target = target ,
438447 # last column is target value
439448 feature_names = feature_names [:- 1 ],
440- DESCR = fdescr . read () )
449+ DESCR = descr_text )
441450
442451
443452def load_sample_images ():
0 commit comments