Skip to content

Commit ba352bf

Browse files
committed
open iris files with statement to avoid ResourceWarning
1 parent 6b91f6d commit ba352bf

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

sklearn/datasets/base.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -249,22 +249,25 @@ def load_iris():
249249
['setosa', 'versicolor', 'virginica']
250250
"""
251251
module_path = dirname(__file__)
252-
data_file = csv.reader(open(join(module_path, 'data', 'iris.csv')))
253-
fdescr = open(join(module_path, 'descr', 'iris.rst'))
254-
temp = next(data_file)
255-
n_samples = int(temp[0])
256-
n_features = int(temp[1])
257-
target_names = np.array(temp[2:])
258-
data = np.empty((n_samples, n_features))
259-
target = np.empty((n_samples,), dtype=np.int)
260-
261-
for i, ir in enumerate(data_file):
262-
data[i] = np.asarray(ir[:-1], dtype=np.float)
263-
target[i] = np.asarray(ir[-1], dtype=np.int)
252+
with open(join(module_path, 'data', 'iris.csv')) as csv_file:
253+
data_file = csv.reader(csv_file)
254+
temp = next(data_file)
255+
n_samples = int(temp[0])
256+
n_features = int(temp[1])
257+
target_names = np.array(temp[2:])
258+
data = np.empty((n_samples, n_features))
259+
target = np.empty((n_samples,), dtype=np.int)
260+
261+
for i, ir in enumerate(data_file):
262+
data[i] = np.asarray(ir[:-1], dtype=np.float)
263+
target[i] = np.asarray(ir[-1], dtype=np.int)
264+
265+
with open(join(module_path, 'descr', 'iris.rst')) as rst_file:
266+
fdescr = rst_file.read()
264267

265268
return Bunch(data=data, target=target,
266269
target_names=target_names,
267-
DESCR=fdescr.read(),
270+
DESCR=fdescr,
268271
feature_names=['sepal length (cm)', 'sepal width (cm)',
269272
'petal length (cm)', 'petal width (cm)'])
270273

0 commit comments

Comments
 (0)