Question: 1 : [ 5 , 4 , 3 , 2 ] 2 : [ 8 , 9 , 3 , 7 ] distance = sqrt
:
:
distance sqrt
which equals
Write a function to calculate this distance:
double distancedouble a double b double c double d double a double b double c double d;
With your distance function, you will be able to implement the nearest neighbor algorithm. It will accept the arrays containing the characteristics of the flowers in train.data and then the four characteristics of the unknown flower.
The method will find the known flower with the minimum distance to the unknown flower. It will then return the label of the known flower as the prediction for the unknown flower.
int nearestNeighbor
double sepallengths double sepalwidths double petallengths double petalwidths int labels int length,
double sepallength double sepalwidth double petallength double petalwidth
;
If there is a tie, the entry that comes first in the list should win the tie.
As a note, this data, like a lot of data in machine learning, is messy. It does not follow clean, wellseparated bell curves. So if you enter a value near the mean for a flower, but it classifies as something else, do not worry. That just means the nearest neighbor was a different flower than expected.
Testing Accuracy
Now that you have the nearestNeighbor method, which can predict the type of a flower, you can test how accurate it is Read in test.data and use nearestNeighbor to classify each flower looking only at its characteristics.
Write a method called accuracy which accepts the arrays of data for know flowers and arrays of data for the testing flowers. You will use the labels included in test.data to determine whether nearestNeighbor made the correct prediction. accuracy should return the percentage of correct predictions as a decimal.
double accuracy
double sepallengths double sepalwidths double petallengths double petalwidths int labels int length,
double sepallengthstest double sepalwidthstest double petallengthstest double petalwidthstest int labelstest int lengthtest
;
Main method
display should call the display function on the loaded data:
Example omits the entire output; there will be more than three values under each:
aout train.data test.data display
Training data:
Testing data:
Examples
Example
aout train.data test.data stats
Sepal length Sepal width Petal length Petal width
Example
aout train.data test.data accuracy
Test accuracy:
Example
examples through have the user entering flower characteristics as input
aout train.data test.data classify
Flower characteristics:
Prediction: Irissetosa
Example
aout train.data test.data classify
Flower characteristics:
Prediction: Irisversicolor
Example
aout train.data test.data classify
Flower characteristics:
Prediction: Irisvirginica we are also already given some code
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
