...

/

Image Classification Using Python Programming

Image Classification Using Python Programming

Explore how to implement image classification using different techniques in Python programming.

After discussing three widely used ML algorithms (SVMs, KNNs, and decision trees), we will perform image classification using Python programming in this lesson.

Implementing the KNN algorithm using Python programming

KNN identifies the k-nearest neighbors in the training set for a given test data point (to be classified). KNN is a lazy learner approach because it stores the dataset and classifies it based on the majority classes of the neighboring data points when it receives new data. The five steps described below develop an image classifier using the KNN algorithm in Python programming.

Step 1: Importing the libraries

Sklearn, also known as scikit-learn, is a widely used Python package (library) that can implement the KNN algorithm for image classification. It’s an open-source Python library for ML, and it offers various in-built algorithms for classification, regression, clustering, and dimensionality reduction. Additionally, it contains in-built functions and modules for preprocessing data, choosing models, and evaluating results.

The following code imports the required libraries:

Press + to interact
# importing required libraries
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
  • Line 2: We import the datasets module from the sklearn library, which provides a collection of different datasets.

  • Line 3: We import the train_test_split function from the model_selection module to split the dataset into training and testing subsets.

  • Line 4: We import the KNN classifier from the neighbors module of the sklearn library.

Step 2: Loading the dataset

The MNIST dataset is used here for image classification. It’s a handwritten digit dataset. We import this dataset using the sklearn library, which consists of 1,797 images of different digits. Each image contains 8 × 8 pixels with grayscale images.

Press + to interact
MNIST handwritten digits dataset
MNIST handwritten digits dataset

The following code loads, prints the size, and displays the images of the MNIST handwritten digits dataset:

Press + to interact
#Importing libraries to load MNIST dataset and display images
from sklearn import datasets
import matplotlib.pyplot as plt
# loading the MNIST digits dataset
mnist = datasets.load_digits()
# Printing the size of the MNIST digits dataset
print("Number of images in MNIST dataset:", len(mnist.images))
# displaying MNIST images from 0-3
figure, axis = plt.subplots(1, 4)
for j in range(4):
axis[j].imshow(mnist.images[j], cmap='gray')
axis[j].set_title(mnist.target[j])
plt.show()
  • Line 2: We import the datasets module from the sklearn library, which provides a collection of different datasets.

  • Line 3: We import the pyplot module from the matplotlib library used for data visualization.

  • Line 6: We load the mnist ...