Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
3 changes: 3 additions & 0 deletions doc/whats_new/v0.21.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ Support for Python 3.4 and below has been officially dropped.
:class:`datasets.svmlight_format` :issue:`10727` by
:user:`Bryan K Woods <bryan-woods>`.

- |Fix| :func:`datasets.load_sample_images` returns images with a deterministic
order. :issue:`13250` by :user:`Thomas Fan <thomasjpfan>`.

:mod:`sklearn.decomposition`
............................

Expand Down
2 changes: 1 addition & 1 deletion sklearn/datasets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ def load_sample_images():
with open(join(module_path, 'README.txt')) as f:
descr = f.read()
filenames = [join(module_path, filename)
for filename in os.listdir(module_path)
for filename in sorted(os.listdir(module_path))
if filename.endswith(".jpg")]
# Load image data for each image in the source folder.
images = [imread(filename) for filename in filenames]
Expand Down
9 changes: 9 additions & 0 deletions sklearn/datasets/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pytest

import numpy as np
from sklearn.datasets import get_data_home
from sklearn.datasets import clear_data_home
from sklearn.datasets import load_files
Expand Down Expand Up @@ -123,6 +124,14 @@ def test_load_sample_images():
res = load_sample_images()
assert_equal(len(res.images), 2)
assert_equal(len(res.filenames), 2)
images = res.images

# assert is china image
assert np.all(images[0][0, 0, :] ==
np.array([174, 201, 231], dtype=np.uint8))
# assert is flower image
assert np.all(images[1][0, 0, :] ==
np.array([2, 19, 13], dtype=np.uint8))
assert res.DESCR
except ImportError:
warnings.warn("Could not load sample images, PIL is not available.")
Expand Down