What is the datasets.load_sample_images() method in Sklearn?

In Python, the Scikit-learn library (sklearn) is used for machine learning. This library contains different algorithms for classification, SVM, random forests, regression, k-neighbours, etc.

Classification is helpful to create a model which separates your data into multiple classes for prediction based on the dataset.

The Scikit-learn library contains a pair of sample images (china and flower) that are helpful to test different pipelines and algorithms on two-dimensional data.

  • The load_sample_images() method is used to load sample images for performing manipulations.
  • The load_sample_image(image_name) is used to load an array containing single image.

Syntax


sklearn.datasets.load_sample_images()

Parameters

It does not take anything as an argument.

Return value

The function returns a dictionary-like object with three attributes.

  1. images: two sample images, i.e. china and flower.
  2. filenames: a list of the names of the images.
  3. DESCR: a short description of the dataset.

Code

In the following code snippet, on line 3, we loaded two sample images from sklearn datasets. It then prints the shape (427, 640, 3) of sample images in the output.

# Demo code load_sample_images()
from sklearn.datasets import load_sample_images
mydataset = load_sample_images()
print("Dataset length:", len(mydataset.images))
first_img_data = mydataset.images[0]
print("1st image Shape:",first_img_data.shape)
second_img_data = mydataset.images[1]
print("2nd image Shape:", second_img_data.shape)

Free Resources