Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Has to be done in Python 3. Only NumPy package is allowed. NO OTHER IMPORTS. TRAINING DATA: 5.0,3.0,1.6,0.2,Iris-setosa 5.0,3.4,1.6,0.4,Iris-setosa 5.2,3.5,1.5,0.2,Iris-setosa 5.2,3.4,1.4,0.2,Iris-setosa 4.7,3.2,1.6,0.2,Iris-setosa 4.8,3.1,1.6,0.2,Iris-setosa 5.4,3.4,1.5,0.4,Iris-setosa 5.2,4.1,1.5,0.1,Iris-setosa

Has to be done in Python 3. Only NumPy package is allowed. NO OTHER IMPORTS.

image text in transcribed

image text in transcribed

TRAINING DATA:

5.0,3.0,1.6,0.2,Iris-setosa 5.0,3.4,1.6,0.4,Iris-setosa 5.2,3.5,1.5,0.2,Iris-setosa 5.2,3.4,1.4,0.2,Iris-setosa 4.7,3.2,1.6,0.2,Iris-setosa 4.8,3.1,1.6,0.2,Iris-setosa 5.4,3.4,1.5,0.4,Iris-setosa 5.2,4.1,1.5,0.1,Iris-setosa 5.5,4.2,1.4,0.2,Iris-setosa 4.9,3.1,1.5,0.1,Iris-setosa 5.0,3.2,1.2,0.2,Iris-setosa 5.5,3.5,1.3,0.2,Iris-setosa 4.9,3.1,1.5,0.1,Iris-setosa 4.4,3.0,1.3,0.2,Iris-setosa 5.1,3.4,1.5,0.2,Iris-setosa 5.0,3.5,1.3,0.3,Iris-setosa 4.5,2.3,1.3,0.3,Iris-setosa 4.4,3.2,1.3,0.2,Iris-setosa 5.0,3.5,1.6,0.6,Iris-setosa 5.1,3.8,1.9,0.4,Iris-setosa 4.8,3.0,1.4,0.3,Iris-setosa 5.1,3.8,1.6,0.2,Iris-setosa 4.6,3.2,1.4,0.2,Iris-setosa 5.3,3.7,1.5,0.2,Iris-setosa 5.0,3.3,1.4,0.2,Iris-setosa 6.6,3.0,4.4,1.4,Iris-versicolor 6.8,2.8,4.8,1.4,Iris-versicolor 6.7,3.0,5.0,1.7,Iris-versicolor 6.0,2.9,4.5,1.5,Iris-versicolor 5.7,2.6,3.5,1.0,Iris-versicolor 5.5,2.4,3.8,1.1,Iris-versicolor 5.5,2.4,3.7,1.0,Iris-versicolor 5.8,2.7,3.9,1.2,Iris-versicolor 6.0,2.7,5.1,1.6,Iris-versicolor 5.4,3.0,4.5,1.5,Iris-versicolor 6.0,3.4,4.5,1.6,Iris-versicolor 6.7,3.1,4.7,1.5,Iris-versicolor 6.3,2.3,4.4,1.3,Iris-versicolor 5.6,3.0,4.1,1.3,Iris-versicolor 5.5,2.5,4.0,1.3,Iris-versicolor 5.5,2.6,4.4,1.2,Iris-versicolor 6.1,3.0,4.6,1.4,Iris-versicolor 5.8,2.6,4.0,1.2,Iris-versicolor 5.0,2.3,3.3,1.0,Iris-versicolor 5.6,2.7,4.2,1.3,Iris-versicolor 5.7,3.0,4.2,1.2,Iris-versicolor 5.7,2.9,4.2,1.3,Iris-versicolor 6.2,2.9,4.3,1.3,Iris-versicolor 5.1,2.5,3.0,1.1,Iris-versicolor 5.7,2.8,4.1,1.3,Iris-versicolor 7.2,3.2,6.0,1.8,Iris-virginica 6.2,2.8,4.8,1.8,Iris-virginica 6.1,3.0,4.9,1.8,Iris-virginica 6.4,2.8,5.6,2.1,Iris-virginica 7.2,3.0,5.8,1.6,Iris-virginica 7.4,2.8,6.1,1.9,Iris-virginica 7.9,3.8,6.4,2.0,Iris-virginica 6.4,2.8,5.6,2.2,Iris-virginica 6.3,2.8,5.1,1.5,Iris-virginica 6.1,2.6,5.6,1.4,Iris-virginica 7.7,3.0,6.1,2.3,Iris-virginica 6.3,3.4,5.6,2.4,Iris-virginica 6.4,3.1,5.5,1.8,Iris-virginica 6.0,3.0,4.8,1.8,Iris-virginica 6.9,3.1,5.4,2.1,Iris-virginica 6.7,3.1,5.6,2.4,Iris-virginica 6.9,3.1,5.1,2.3,Iris-virginica 5.8,2.7,5.1,1.9,Iris-virginica 6.8,3.2,5.9,2.3,Iris-virginica 6.7,3.3,5.7,2.5,Iris-virginica 6.7,3.0,5.2,2.3,Iris-virginica 6.3,2.5,5.0,1.9,Iris-virginica 6.5,3.0,5.2,2.0,Iris-virginica 6.2,3.4,5.4,2.3,Iris-virginica 5.9,3.0,5.1,1.8,Iris-virginica

TESTING DATA:

5.1,3.5,1.4,0.2,Iris-setosa 4.9,3.0,1.4,0.2,Iris-setosa 4.7,3.2,1.3,0.2,Iris-setosa 4.6,3.1,1.5,0.2,Iris-setosa 5.0,3.6,1.4,0.2,Iris-setosa 5.4,3.9,1.7,0.4,Iris-setosa 4.6,3.4,1.4,0.3,Iris-setosa 5.0,3.4,1.5,0.2,Iris-setosa 4.4,2.9,1.4,0.2,Iris-setosa 4.9,3.1,1.5,0.1,Iris-setosa 5.4,3.7,1.5,0.2,Iris-setosa 4.8,3.4,1.6,0.2,Iris-setosa 4.8,3.0,1.4,0.1,Iris-setosa 4.3,3.0,1.1,0.1,Iris-setosa 5.8,4.0,1.2,0.2,Iris-setosa 5.7,4.4,1.5,0.4,Iris-setosa 5.4,3.9,1.3,0.4,Iris-setosa 5.1,3.5,1.4,0.3,Iris-setosa 5.7,3.8,1.7,0.3,Iris-setosa 5.1,3.8,1.5,0.3,Iris-setosa 5.4,3.4,1.7,0.2,Iris-setosa 5.1,3.7,1.5,0.4,Iris-setosa 4.6,3.6,1.0,0.2,Iris-setosa 5.1,3.3,1.7,0.5,Iris-setosa 4.8,3.4,1.9,0.2,Iris-setosa 7.0,3.2,4.7,1.4,Iris-versicolor 6.4,3.2,4.5,1.5,Iris-versicolor 6.9,3.1,4.9,1.5,Iris-versicolor 5.5,2.3,4.0,1.3,Iris-versicolor 6.5,2.8,4.6,1.5,Iris-versicolor 5.7,2.8,4.5,1.3,Iris-versicolor 6.3,3.3,4.7,1.6,Iris-versicolor 4.9,2.4,3.3,1.0,Iris-versicolor 6.6,2.9,4.6,1.3,Iris-versicolor 5.2,2.7,3.9,1.4,Iris-versicolor 5.0,2.0,3.5,1.0,Iris-versicolor 5.9,3.0,4.2,1.5,Iris-versicolor 6.0,2.2,4.0,1.0,Iris-versicolor 6.1,2.9,4.7,1.4,Iris-versicolor 5.6,2.9,3.6,1.3,Iris-versicolor 6.7,3.1,4.4,1.4,Iris-versicolor 5.6,3.0,4.5,1.5,Iris-versicolor 5.8,2.7,4.1,1.0,Iris-versicolor 6.2,2.2,4.5,1.5,Iris-versicolor 5.6,2.5,3.9,1.1,Iris-versicolor 5.9,3.2,4.8,1.8,Iris-versicolor 6.1,2.8,4.0,1.3,Iris-versicolor 6.3,2.5,4.9,1.5,Iris-versicolor 6.1,2.8,4.7,1.2,Iris-versicolor 6.4,2.9,4.3,1.3,Iris-versicolor 6.3,3.3,6.0,2.5,Iris-virginica 5.8,2.7,5.1,1.9,Iris-virginica 7.1,3.0,5.9,2.1,Iris-virginica 6.3,2.9,5.6,1.8,Iris-virginica 6.5,3.0,5.8,2.2,Iris-virginica 7.6,3.0,6.6,2.1,Iris-virginica 4.9,2.5,4.5,1.7,Iris-virginica 7.3,2.9,6.3,1.8,Iris-virginica 6.7,2.5,5.8,1.8,Iris-virginica 7.2,3.6,6.1,2.5,Iris-virginica 6.5,3.2,5.1,2.0,Iris-virginica 6.4,2.7,5.3,1.9,Iris-virginica 6.8,3.0,5.5,2.1,Iris-virginica 5.7,2.5,5.0,2.0,Iris-virginica 5.8,2.8,5.1,2.4,Iris-virginica 6.4,3.2,5.3,2.3,Iris-virginica 6.5,3.0,5.5,1.8,Iris-virginica 7.7,3.8,6.7,2.2,Iris-virginica 7.7,2.6,6.9,2.3,Iris-virginica 6.0,2.2,5.0,1.5,Iris-virginica 6.9,3.2,5.7,2.3,Iris-virginica 5.6,2.8,4.9,2.0,Iris-virginica 7.7,2.8,6.7,2.0,Iris-virginica 6.3,2.7,4.9,1.8,Iris-virginica 6.7,3.3,5.7,2.1,Iris-virginica

SAMPLE OUTPUT

image text in transcribed

image text in transcribed

Introduction Machine learning is an area of computer science whose aim is to create programs which improve their performance with experience. There are many applications for this, including: face recognition, recommendation systems, defect detection, robot navigation, and game playing. For this assignment, you will implement a simple machine learning algorithm called Nearest Neighbor which learns by remembering training examples. It then classifies test examples by choosing the class of the "closest" training example. The notion of "closeness" differs depending on applications. You will need to use the Nearest Neighbor algorithm to learn and classify types of Iris plants based on their sepal and petal length and width. There are three Iris types you will need to classify: Iris Setosa Iris Versicolour Iris Virginica The learning will be done by remembering training examples stored in a comma-separated file. The training examples include different measurements which collectively are called features or attributes, and a class label for different instances. These are: 1. sepal length in cm 2. sepal width in cm 3. petal length in cm 4. petal width in cm 5. class: -- Iris Setosa -- Iris Versicolour -- Iris Virginica To see how well the program "learned", you will then load a file containing testing examples, which will include the same type of information, but for different instances. For each test instance, you will apply the Nearest Neighbor algorithm to classify the instance. This algorithm works by choosing a class label of the "closest training example, where "closest" means shortest distance. The distance is computed using the following formula: dist(x,y) = (st: - sly)* + (swx - sw, )* + (pl. - pl,)* + (pw, pw;) where x,y are two instances (.e. a training or a testing example), sly,sly are their sepal lengths, sw, sw, are their sepal widths, pl.ply are their petal lengths, and pwx, pwy are their petal widths. After you finish classifying each testing instance, you will then need to compare it to the "true" label that is specified for each example and compute the accuracy. Accuracy is measured as the number of correctly classified instances divided by the number of total testing instances. Requirements You are to create a program in Python 3 that performs the following: 1. Loads and parses the training and testing dataset files into separate NumPy ndarrays. Given what you know, the easiest way to do this is to create four separate arrays: 2D array of floats for storing training example attribute values 2D array of floats for storing testing example attribute values 1D array of strings for storing training example class labels 1D array of strings for storing testing example class labels You can assume there are exactly 4 attribute values in the training and testing examples. 2. Classifies each testing example. You also need to output the true and predicted class label to the screen and save it into a new 1D array of strings. This is done by first computing the distance value for each pair of training and testing examples (their attribute values). Then, for each test example, find the training example with the closest distance. You can do all that easily with NumPy's vectorized functions - you shouldn't use loops for this. 3. Computes the accuracy. Go through the array of class labels for testing examples and compare the label stored in the array created in step (2). Count how many matches you get. Output the number of matches, divided by the number of testing examples as a percentage. Additional Requirements 1. The name of your source code file should be NearestNeighbor.py. All your code should be within a single file. 2. You cannot import any package except for NumPy. 3. Your code should follow good coding practices, including good use of whitespace and use of both inline and block comments. 4. You need to use meaningful identifier names that conform to standard naming conventions. 5. At the top of each file, you need to put in a block comment with the following information: your name, date, course name, semester, and assignment name. 6. The output of your program should exactly match the sample program output given at the end. What to Turn In You will turn in screenshot(s) for your output and the single NearestNeighbor.py file using BlackBoard. #, True, Predicted 1,Iris-setosa, Iris-setosa 2, Iris-setosa, Iris-setosa 3, Iris-setosa, Iris-setosa 4, Iris-setosa, Iris-setosa 5, Iris-setosa, Iris-setosa 6, Iris-setosa, Iris-setosa 7, Iris-setosa, Iris-setosa 8, Iris-setosa, Iris-setosa 9, Iris-setosa, Iris-setosa 10,Iris-setosa, Iris-setosa 11,Iris-setosa, Iris-setosa 12,Iris-setosa, Iris-setosa 13,Iris-setosa, Iris-setosa 14,Iris-setosa, Iris-setosa 15, Iris-setosa, Iris-setosa 16,Iris-setosa, Iris-setosa 17, Iris-setosa, Iris-setosa 18,Iris-setosa, Iris-setosa 19,Iris-setosa, Iris-setosa 20,Iris-setosa, Iris-setosa 21,Iris-setosa, Iris-setosa 22,Iris-setosa, Iris-setosa 23,Iris-setosa, Iris-setosa 24, Iris-setosa, Iris-setosa 25,Iris-setosa, Iris-setosa 26,Iris-versicolor, Iris-versicolor 27,Iris-versicolor, Iris-versicolor 28,Iris-versicolor, Iris-versicolor 29, Iris-versicolor, Iris-versicolor 30, Iris-versicolor, Iris-versicolor 31, Iris-versicolor, Iris-versicolor 32,Iris-versicolor, Iris-versicolor 33, Iris-versicolor, Iris-versicolor 34,Iris-versicolor, Iris-versicolor 35,Iris-versicolor, Iris-versicolor 36,Iris-versicolor, Iris-versicolor 37, Iris-versicolor, Iris-versicolor 38,Iris-versicolor, Iris-versicolor 39,Iris-versicolor, Iris-versicolor 40, Iris-versicolor, Iris-versicolor 41,Iris-versicolor, Iris-versicolor 42,Iris-versicolor, Iris-versicolor 43,Iris-versicolor, Iris-versicolor 44,Iris-versicolor, Iris-versicolor 45, Iris-versicolor, Iris-versicolor 46,Iris-versicolor, Iris-virginica 47,Iris-versicolor, Iris-versicolor 48,Iris-versicolor, Iris-virginica 49,Iris-versicolor, Iris-versicolor 50,Iris-versicolor, Iris-versicolor 51, Iris virginica, Iris virginica 52,Iris-virginica, Iris-virginica 53,Iris-virginica, Iris-virginica 54,Iris-virginica, Iris-virginica 55, Iris virginica, Iris virginica 56,Iris-virginica, Iris-virginica 57, Iris-virginica, Iris-versicolor 58,Iris-virginica, Iris-virginica 59,Iris-virginica, Iris-virginica 60,Iris-virginica, Iris-virginica 61, Iris-virginica, Iris-virginica 62,Iris-virginica, Iris-virginica 63,Iris-virginica, Iris-virginica 64, Iris-virginica, Iris-virginica 65,Iris-virginica, Iris-virginica 66, Iris-virginica, Iris-virginica 67, Iris-virginica, Iris-virginica 68, Iris-virginica, Iris-virginica 69,Iris-virginica, Iris-virginica 70,Iris-virginica, Iris-versicolor 71,Iris-virginica, Iris-virginica 72,Iris-virginica, Iris-virginica 73,Iris-virginica, Iris-virginica 74,Iris-virginica, Iris-virginica 75, Iris-virginica, Iris-virginica Accuracy: 94.67% Introduction Machine learning is an area of computer science whose aim is to create programs which improve their performance with experience. There are many applications for this, including: face recognition, recommendation systems, defect detection, robot navigation, and game playing. For this assignment, you will implement a simple machine learning algorithm called Nearest Neighbor which learns by remembering training examples. It then classifies test examples by choosing the class of the "closest" training example. The notion of "closeness" differs depending on applications. You will need to use the Nearest Neighbor algorithm to learn and classify types of Iris plants based on their sepal and petal length and width. There are three Iris types you will need to classify: Iris Setosa Iris Versicolour Iris Virginica The learning will be done by remembering training examples stored in a comma-separated file. The training examples include different measurements which collectively are called features or attributes, and a class label for different instances. These are: 1. sepal length in cm 2. sepal width in cm 3. petal length in cm 4. petal width in cm 5. class: -- Iris Setosa -- Iris Versicolour -- Iris Virginica To see how well the program "learned", you will then load a file containing testing examples, which will include the same type of information, but for different instances. For each test instance, you will apply the Nearest Neighbor algorithm to classify the instance. This algorithm works by choosing a class label of the "closest training example, where "closest" means shortest distance. The distance is computed using the following formula: dist(x,y) = (st: - sly)* + (swx - sw, )* + (pl. - pl,)* + (pw, pw;) where x,y are two instances (.e. a training or a testing example), sly,sly are their sepal lengths, sw, sw, are their sepal widths, pl.ply are their petal lengths, and pwx, pwy are their petal widths. After you finish classifying each testing instance, you will then need to compare it to the "true" label that is specified for each example and compute the accuracy. Accuracy is measured as the number of correctly classified instances divided by the number of total testing instances. Requirements You are to create a program in Python 3 that performs the following: 1. Loads and parses the training and testing dataset files into separate NumPy ndarrays. Given what you know, the easiest way to do this is to create four separate arrays: 2D array of floats for storing training example attribute values 2D array of floats for storing testing example attribute values 1D array of strings for storing training example class labels 1D array of strings for storing testing example class labels You can assume there are exactly 4 attribute values in the training and testing examples. 2. Classifies each testing example. You also need to output the true and predicted class label to the screen and save it into a new 1D array of strings. This is done by first computing the distance value for each pair of training and testing examples (their attribute values). Then, for each test example, find the training example with the closest distance. You can do all that easily with NumPy's vectorized functions - you shouldn't use loops for this. 3. Computes the accuracy. Go through the array of class labels for testing examples and compare the label stored in the array created in step (2). Count how many matches you get. Output the number of matches, divided by the number of testing examples as a percentage. Additional Requirements 1. The name of your source code file should be NearestNeighbor.py. All your code should be within a single file. 2. You cannot import any package except for NumPy. 3. Your code should follow good coding practices, including good use of whitespace and use of both inline and block comments. 4. You need to use meaningful identifier names that conform to standard naming conventions. 5. At the top of each file, you need to put in a block comment with the following information: your name, date, course name, semester, and assignment name. 6. The output of your program should exactly match the sample program output given at the end. What to Turn In You will turn in screenshot(s) for your output and the single NearestNeighbor.py file using BlackBoard. #, True, Predicted 1,Iris-setosa, Iris-setosa 2, Iris-setosa, Iris-setosa 3, Iris-setosa, Iris-setosa 4, Iris-setosa, Iris-setosa 5, Iris-setosa, Iris-setosa 6, Iris-setosa, Iris-setosa 7, Iris-setosa, Iris-setosa 8, Iris-setosa, Iris-setosa 9, Iris-setosa, Iris-setosa 10,Iris-setosa, Iris-setosa 11,Iris-setosa, Iris-setosa 12,Iris-setosa, Iris-setosa 13,Iris-setosa, Iris-setosa 14,Iris-setosa, Iris-setosa 15, Iris-setosa, Iris-setosa 16,Iris-setosa, Iris-setosa 17, Iris-setosa, Iris-setosa 18,Iris-setosa, Iris-setosa 19,Iris-setosa, Iris-setosa 20,Iris-setosa, Iris-setosa 21,Iris-setosa, Iris-setosa 22,Iris-setosa, Iris-setosa 23,Iris-setosa, Iris-setosa 24, Iris-setosa, Iris-setosa 25,Iris-setosa, Iris-setosa 26,Iris-versicolor, Iris-versicolor 27,Iris-versicolor, Iris-versicolor 28,Iris-versicolor, Iris-versicolor 29, Iris-versicolor, Iris-versicolor 30, Iris-versicolor, Iris-versicolor 31, Iris-versicolor, Iris-versicolor 32,Iris-versicolor, Iris-versicolor 33, Iris-versicolor, Iris-versicolor 34,Iris-versicolor, Iris-versicolor 35,Iris-versicolor, Iris-versicolor 36,Iris-versicolor, Iris-versicolor 37, Iris-versicolor, Iris-versicolor 38,Iris-versicolor, Iris-versicolor 39,Iris-versicolor, Iris-versicolor 40, Iris-versicolor, Iris-versicolor 41,Iris-versicolor, Iris-versicolor 42,Iris-versicolor, Iris-versicolor 43,Iris-versicolor, Iris-versicolor 44,Iris-versicolor, Iris-versicolor 45, Iris-versicolor, Iris-versicolor 46,Iris-versicolor, Iris-virginica 47,Iris-versicolor, Iris-versicolor 48,Iris-versicolor, Iris-virginica 49,Iris-versicolor, Iris-versicolor 50,Iris-versicolor, Iris-versicolor 51, Iris virginica, Iris virginica 52,Iris-virginica, Iris-virginica 53,Iris-virginica, Iris-virginica 54,Iris-virginica, Iris-virginica 55, Iris virginica, Iris virginica 56,Iris-virginica, Iris-virginica 57, Iris-virginica, Iris-versicolor 58,Iris-virginica, Iris-virginica 59,Iris-virginica, Iris-virginica 60,Iris-virginica, Iris-virginica 61, Iris-virginica, Iris-virginica 62,Iris-virginica, Iris-virginica 63,Iris-virginica, Iris-virginica 64, Iris-virginica, Iris-virginica 65,Iris-virginica, Iris-virginica 66, Iris-virginica, Iris-virginica 67, Iris-virginica, Iris-virginica 68, Iris-virginica, Iris-virginica 69,Iris-virginica, Iris-virginica 70,Iris-virginica, Iris-versicolor 71,Iris-virginica, Iris-virginica 72,Iris-virginica, Iris-virginica 73,Iris-virginica, Iris-virginica 74,Iris-virginica, Iris-virginica 75, Iris-virginica, Iris-virginica Accuracy: 94.67%

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

More Books

Students also viewed these Databases questions

Question

1-4 How will MIS help my career?

Answered: 1 week ago