Question: 1 : [ 5 , 4 , 3 , 2 ] 2 : [ 8 , 9 , 3 , 7 ] distance = sqrt

1: [5,4,3,2]
2: [8,9,3,7]
distance = sqrt((5-8)^(2)+(4-9)^(2)+(3-3)^(2)+(2-7)^(2))
which equals 7.68
Write a function to calculate this distance:
double distance(double a1, double b1, double c1, double d1, double a2, double b2, double c2, double d2);
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 sepal_(l)engths[], double sepal_(w)idths[], double petal_(l)engths[], double petal_(w)idths[], int labels[], int length,
double sepal_(l)ength, double sepal_(w)idth, double petal_(l)ength, double petal_(w)idth
);
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, well-separated 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 sepal_(l)engths[], double sepal_(w)idths[], double petal_(l)engths[], double petal_(w)idths[], int labels[], int length,
double sepal_(l)engths_(t)est[], double sepal_(w)idths_(t)est[], double petal_(l)engths_(t)est[], double petal_(w)idths_(t)est[], int labels_(t)est[], int length_(t)est
);
Main method
display should call the display function on the loaded data:
Example 1(omits the entire output; there will be more than three values under each):
(.)/(a).out train.data test.data display
Training data:
(5.100000,3.500000,1.400000,0.200000)=>0
(4.900000,3.000000,1.400000,0.200000)=>0
(4.700000,3.200000,1.300000,0.200000)=>0
...
Testing data:
(5.000000,3.500000,1.300000,0.300000)=>0
(4.500000,2.300000,1.300000,0.300000)=>0
(4.400000,3.200000,1.300000,0.200000)=>0
...
Examples
Example 2
(.)/(a).out train.data test.data stats
| Sepal length | Sepal width | Petal length | Petal width
0|5.04(+)/(-)0.72|3.44(+)/(-)0.72|1.46(+)/(-)0.34|0.23(+)/(-)0.20
1|6.01(+)/(-)1.03|2.78(+)/(-)0.66|4.32(+)/(-)0.89|1.35(+)/(-)0.41
2|6.62(+)/(-)1.35|2.96(+)/(-)0.66|5.61(+)/(-)1.16|1.99(+)/(-)0.54
Example 3
(.)/(a).out train.data test.data accuracy
Test accuracy: 1.00
Example 4
(examples 4 through 6 have the user entering flower characteristics as input)
(.)/(a).out train.data test.data classify
Flower characteristics: 531.50.2
Prediction: 0(Iris-setosa)
Example 5
(.)/(a).out train.data test.data classify
Flower characteristics: 6351.5
Prediction: 1(Iris-versicolor)
Example 6
(.)/(a).out train.data test.data classify
Flower characteristics: 7362.5
Prediction: 2(Iris-virginica) we are also already given some code
1 : [ 5 , 4 , 3 , 2 ] 2 : [ 8 , 9 , 3 , 7 ]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!