Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USE PYTHON3; DO NOT IMPORT ANY PACKAGES Please do all of part 3 and make sure to follow all the requirements; do not add or

USE PYTHON3; DO NOT IMPORT ANY PACKAGES

Please do all of part 3 and make sure to follow all the requirements; do not add or remove any parameters because it will make the answer incorrect even if the code runs accurately

Part 3:

Classification is one of the major tasks in machine learning, which aims to predict the label of a piece of unknown data by learning a pattern from a visible collection of data. To train a classifier, you need to fit the classifier with training data, which is a collection of (data, label) pairs. The classifier will apply the training data to its own algorithm. After training, you can provide a piece of known or unknown data, and the classifier will try to predict a label. By training a classification algorithm, we can extract essential information from pieces of complicated data.

In this part, you will implement a K-nearest Neighbors (KNN) classifier for the RGB images. Given an image, this algorithm will predict the label by finding the most popular labels in a collection (with size k) of nearest training data.

But how could we evaluate how near two images are? We need a definition of distance between images. With this definition, we can find the nearest training data by finding the shortest distances. Here, we use the Euclidean distance as our definition of distance. Since images are represented as 3-dimensional matrices, we can first flatten them to 1-dimensional vectors, then apply the Euclidean distance formula. For image a and b, we define their Euclidean distance as:

image text in transcribed

Where ai and bi (1 to calculate the distance, two images must have the same size.

Once we have a notion of distance, we can start implementing a KNN classifier. In the fitting (training) stage, all you need to do is to store the training data. Then, in the prediction stage, the algorithm will find the distance between the provided image and all images in the training data, find k nearest training data, and predict the label by the most popular label among the k-nearest training data.

Our blueprint of the classifier will be defined in the ImageKNNClassifier class. In this class, you need to implement the following methods:

image text in transcribed

image text in transcribed

Here is the code format in the text editor for your convenience:

Note: All methods in this class have a decorator @staticmethod. This decorator indicates that these functions do not belong to any instances, and should be called by the class itself. For example, to use the function vote(), you should call it like ImageProcessing.vote(candidates), instead of initializing an ImageProcessing instance first.

# Part 3: Image KNN Classifier # class ImageKNNClassifier: """ TODO: add description """

def __init__(self, n_neighbors): """ TODO: add description """ # YOUR CODE GOES HERE #

def fit(self, data): """ TODO: add description """ # YOUR CODE GOES HERE #

@staticmethod def distance(image1, image2): """ TODO: add description """ # YOUR CODE GOES HERE #

@staticmethod def vote(candidates): """ TODO: add description """ # YOUR CODE GOES HERE #

def predict(self, image): """ TODO: add description """ # YOUR CODE GOES HERE #

Thank you so much!

d(a, b) = V (a bq)2 + (az b)2 + ... + (an - b- b)? __init__(self, n_neighbors) A constructor that initializes a ImageKNNClassifier instance and necessary instance variables. The argument n_neighbors defines the size of the nearest neighborhood (i.e. how many neighbors your model will find to make the prediction). When predicting the labels, this classifier will look for the majority between the n_neighbors closest images. fit (self, data) Fit the classifier by storing all training data in the classifier instance. You can assume data is a list of (image, label) tuples, where image is a RGBImage instance and label is a string. Requirements: (1) Assert that the length of data is greater than self.n_neighbors. (2) Assert that this classifier instance does not already have training data stored. (3) Make sure that the name of the instance attribute that you store the training data in is data @staticmethod distance (imagel, image2) A method to calculate the Euclidean distance between RGB image imagel and image2 To calculate the Euclidean distance, for the value at each position (channel, row, column) in the pixels of both images, calculate the squared difference between two values. Then, add all these values of squared difference together. The Euclidean distance between two images is the square root of this sum. You can refer to d(a, b) = V(; -6,)2 + (az b)2 + ... + (an - br)? if you prefer a more formal definition. Requirements: (1) No explicit for/while loops. You can use list comprehensions or map instead. (2) Assert that both arguments are RGBImage instances with the same size. @staticmethod vote (candidates) Find the most popular label from a list of candidates (nearest neighbors) labels. If there is a tie when determining the majority label, you can return any of them. predict (self, image) Predict the label of the given image using the KNN classification algorithm. You should use the vote() method to make the prediction from the nearest neighbors. Requirements: (1) No explicit for/while loops. You can use list comprehensions or map() instead. (2) Assert that the training data is present in the classifier instance. In other words, assert that fit() method has been called before calling this method

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago