# Buggy code
def _load_imgs(file_paths, slice_, color, resize):
    """Internally used to load images"""

    # Try to import imread and imresize from PIL. We do this here to prevent
    # the whole sklearn.datasets module from depending on PIL.
    try:
        try:
            from scipy.misc import imread
        except ImportError:
            from scipy.misc.pilutil import imread
        from scipy.misc import imresize
    except ImportError:
        raise ImportError(
            "The Python Imaging Library (PIL) is required to load data from jpeg files"
        )

    # compute the portion of the images to load to respect the slice_ parameter
    # given by the caller
    default_slice = (slice(0, 250), slice(0, 250))
    if slice_ is None:
        slice_ = default_slice
    else:
        slice_ = tuple(s or ds for s, ds in zip(slice_, default_slice))

    h_slice, w_slice = slice_
    h = (h_slice.stop - h_slice.start) // (h_slice.step or 1)
    w = (w_slice.stop - w_slice.start) // (w_slice.step or 1)

    if resize is not None:
        resize = float(resize)
        h = int(resize * h)
        w = int(resize * w)

    # allocate some contiguous memory to host the decoded image slices
    n_faces = len(file_paths)
    if not color:
        faces = np.zeros((n_faces, h, w), dtype=np.float32)
    else:
        faces = np.zeros((n_faces, h, w, 3), dtype=np.float32)

    # iterate over the collected file path to load the jpeg files as numpy
    # arrays
    for i, file_path in enumerate(file_paths):
        if i % 1000 == 0:
            logger.info("Loading face #%05d / %05d", i + 1, n_faces)
        face = np.asarray(imread(file_path)[slice_], dtype=np.float32)
        face /= 255.0  # scale uint8 coded colors to the [0.0, 1.0] floats
        if resize is not None:
            face = imresize(face, resize)
        if not color:
            # average the color channels to compute a gray levels
            # representaion
            face = face.mean(axis=2)

        faces[i, ...] = face

    return faces


# Traceback (IndexError)
$ python face_recognition.py

===================================================
Faces recognition example using eigenfaces and SVMs
===================================================

The dataset used in this example is a preprocessed excerpt of the
"Labeled Faces in the Wild", aka LFW_:

http://vis-www.cs.umass.edu/lfw/lfw-funneled.tgz (233MB)

.. _LFW: http://vis-www.cs.umass.edu/lfw/

Expected results for the top 5 most represented people in the dataset::

precision    recall  f1-score   support

Gerhard_Schroeder       0.91      0.75      0.82        28
Donald_Rumsfeld       0.84      0.82      0.83        33
Tony_Blair       0.65      0.82      0.73        34
Colin_Powell       0.78      0.88      0.83        58
George_W_Bush       0.93      0.86      0.90       129

avg / total       0.86      0.84      0.85       282




2014-08-26 13:30:49,451 Loading LFW people faces from /home/jim/scikit_learn_data/lfw_home
2014-08-26 13:30:49,454 Loading face #00001 / 01140
Traceback (most recent call last):
File "eigenface.py", line 52, in <module>
lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)
File "/usr/local/lib/python2.7/dist-packages/sklearn/datasets/lfw.py", line 277, in fetch_lfw_people
min_faces_per_person=min_faces_per_person, color=color, slice_=slice_)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/memory.py", line 481, in __call__
return self._cached_call(args, kwargs)[0]
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/memory.py", line 428, in _cached_call
out, metadata = self.call(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/memory.py", line 673, in call
output = self.func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/sklearn/datasets/lfw.py", line 202, in _fetch_lfw_people
faces = _load_imgs(file_paths, slice_, color, resize)
File "/usr/local/lib/python2.7/dist-packages/sklearn/datasets/lfw.py", line 156, in _load_imgs
face = np.asarray(imread(file_path)[slice_], dtype=np.float32)
IndexError: 0-d arrays can only use a single () or a list of newaxes (and a single ...) as an index

# Fixed code
def _load_imgs(file_paths, slice_, color, resize):
    """Internally used to load images"""

    # Try to import imread and imresize from PIL. We do this here to prevent
    # the whole sklearn.datasets module from depending on PIL.
    try:
        try:
            from scipy.misc import imread
        except ImportError:
            from scipy.misc.pilutil import imread
        from scipy.misc import imresize
    except ImportError:
        raise ImportError(
            "The Python Imaging Library (PIL) is required to load data from jpeg files"
        )

    # compute the portion of the images to load to respect the slice_ parameter
    # given by the caller
    default_slice = (slice(0, 250), slice(0, 250))
    if slice_ is None:
        slice_ = default_slice
    else:
        slice_ = tuple(s or ds for s, ds in zip(slice_, default_slice))

    h_slice, w_slice = slice_
    h = (h_slice.stop - h_slice.start) // (h_slice.step or 1)
    w = (w_slice.stop - w_slice.start) // (w_slice.step or 1)

    if resize is not None:
        resize = float(resize)
        h = int(resize * h)
        w = int(resize * w)

    # allocate some contiguous memory to host the decoded image slices
    n_faces = len(file_paths)
    if not color:
        faces = np.zeros((n_faces, h, w), dtype=np.float32)
    else:
        faces = np.zeros((n_faces, h, w, 3), dtype=np.float32)

    # iterate over the collected file path to load the jpeg files as numpy
    # arrays
    for i, file_path in enumerate(file_paths):
        if i % 1000 == 0:
            logger.info("Loading face #%05d / %05d", i + 1, n_faces)

        # Checks if jpeg reading worked. Refer to issue #3594 for more
        # details.
        img = imread(file_path)
        if img.ndim is 0:
            raise RuntimeError(
                "Failed to read the image file %s, "
                "Please make sure that libjpeg is installed" % file_path
            )

        face = np.asarray(img[slice_], dtype=np.float32)
        face /= 255.0  # scale uint8 coded colors to the [0.0, 1.0] floats
        if resize is not None:
            face = imresize(face, resize)
        if not color:
            # average the color channels to compute a gray levels
            # representaion
            face = face.mean(axis=2)

        faces[i, ...] = face

    return faces