Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi , can you please assist me with my Artifical Intelligent homework.we suppose

ID: 3850491 • Letter: H

Question


Hi , can you please assist me with my Artifical Intelligent homework.we suppose to write a c++ or java codes Experiment 3 Recognizing Handwritten Digits Using KNN Objective The shapes of the same digit (0,1 9) can be distinct even when they are written by the same person. Hence a good recognition of handwritten digits is not easy. To recognize digits in the MINIST dataset, you are required to implement the KNN classifier to assign each digits in the testing data a label for the estimation of its true value. Methodology The MINIST dataset is split into two parts, one part (trData shrinked.txt) s for training and the other (tsData shrinked.txt) is for testing, see Fig. 1 for a screen shot of tsData shrinked.txt. The appended shrinked" within the data name indicates that the provided data are only subsets of the original MNIST data to reduce the processing time. KNN predicts each testing example a label in (0,1 by considering the labels of its K-nearest neighbor. In a nutshell, KNN finds each testing example K-nearest neighbors in training examples, and then use voting (that is, checking which class dominates the K-nearest neighbors to predict the label. To find the K-nearest neighbors, one can use Euclidean distance (or any other distance metric available). Since the value of K is very important to get a promising recognition accuracy, one typically performs n-folds cross validation to obtain the "best" K. However, for this experiment it is not compulsory to implement cross validation. Rather you can try several of different K values (e.g KE 7) and report the one that delivers the best (1,3,5,7 performance

Explanation / Answer

import knn
import random

def column(A, j):
return [row[j] for row in A]

def test(data, k):
random.shuffle(data)
pts, labels = column(data, 0), column(data, 1)

trainingData = pts[:800]
trainingLabels = labels[:800]
testData = pts[800:]
testLabels = labels[800:]

f = knn.makeKNNClassifier(trainingData, trainingLabels,
k, knn.euclideanDistance)
correct = 0
total = len(testLabels)

for (point, label) in zip(testData, testLabels):
if f(point) == label:
correct += 1

return float(correct) / total

A run with k=1 gives a surprisingly good 89% success rate. Varying k, we see this is about as good as it gets without any modifications to the algorithm or the metric. Indeed, the graph below shows that the handwritten digits data set agrees with the axiom of neighborliness to a fair approximation.