Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can someone help me check if i meet all requirements and also help me compile my project pls! import java.io.FileReader; import java.util.ArrayList; import java.util.List; import
Can someone help me check if i meet all requirements and also help me compile my project pls!
import java.io.FileReader; import java.util.ArrayList; import java.util.List; import java.net.URL; import java.io.IOException; import java.net.MalformedURLException; import java.net.URLConnection; import java.util.Scanner; /** * * @author mohamed */ public class CNNAlgorithm { private Listwhere x and y are two instances (i.e. a training or a testingexample); sl and sly are their sepal lengths; swx are sw, their sepal widths; plx and ply are their petal lengths; pw and 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 Java that performs the following 1. 2. Prompts the user to enter filenames for the training and the testing dataset files. Loads and parses the training and testing dataset files into separate arrays. Given what you know, the easiest way to do this is to create four separate arrays: . 2D array of doubles for storing training example attribute values .2D array of doubles 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 75 training and 75 testing examples. 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 iterating over the array of testing examples and computing the index of the closest training example. Then copying over the class label of the found training example into the new 1D array for the corresponding index. Computes the accuracy. Go through the array of class labels for testing examples and compare the label stored in the array created in step 3. Count how many matches you get. Output the number of matches, divided by the number of testing examples. 3. 4. Additional Requirements The name of your Java Class that contains the main method should be NearestNeighbor. All your code should be within a single file You will need to decompose your code into separate methods that implement the various parts of the program. For example, you can have a method that computes the index of the nearest training example, given some attribute values. In general, whenever your methods have too many lines ofcode, you should think about separating some of the functionality into methods. A good rule of thumb is to start thinking about method decomposition with methods over 15 lines, but sometimes 20 or 30 lines is still OK. Your code should follow good coding practices, including good use of whitespace (indents and line breaks) and use of both inline and block comments. You need to use meaningful identifier names that conform to standard Java naming conventions. At the top of each file, you need to put in a block comment with the following 1. 2. 3. 4. 5irisDataSet; private List testDataSet; private List trainingDataSet; private List condensedDataSet; public CNNAlgorithm() { irisDataSet = new ArrayList (); testDataSet = new ArrayList (); trainingDataSet = new ArrayList (); condensedDataSet = new ArrayList (); } public static void main(String[] args) throws IOException{ DataReader reader = new DataReader(); CNNAlgorithm cnnImpl = new CNNAlgorithm(); cnnImpl.setIrisDataSet(reader.getIrisData()); cnnImpl.prepareTrainingData(); cnnImpl.prepareTestData(); cnnImpl.prepareCondensedSet(); System.out.println("The condensed set is"); for ( Iris i : cnnImpl.condensedDataSet){ System.out.println(i); } double classificationAcc = cnnImpl.calculateClassificationAcc(); System.out.println("Classification accuracy: "+classificationAcc); } private double calculateClassificationAcc(){ double minDist = 99999; double correctClassification = 0.0; Iris closestSample = null; for ( Iris testD : testDataSet ){ minDist = 99999; for ( Iris trainD : condensedDataSet){ double calcDistance = testD.distance(trainD); if ( calcDistance reducedSet = constructReducedSet(); int previousReducedSetSize = 0; while ( true ) { previousReducedSetSize = reducedSet.size(); int i = 0; while ( !reducedSet.isEmpty()){ Iris ir = reducedSet.get(0); //Finding the closest pattern in condensed set. double minDist = 9999; IrisType type = IrisType.SETOSA; Iris ic = null; for ( int j=0; j constructReducedSet(){ List reducedSet = new ArrayList (); for ( Iris iTrain : trainingDataSet ){ //If present in Condensed set, then dont add it to reducedSet. if ( condensedDataSet.contains(iTrain) ) { continue; } reducedSet.add(iTrain); } return reducedSet; } /** * @param irisDataSet the irisDataSet to set */ public void setIrisDataSet(List irisDataSet) { this.irisDataSet = irisDataSet; } } class DataReader{ public List getIrisData() throws IOException{ Scanner irisDataReader = new Scanner(new FileReader("dataset")); List irisDataSet = new ArrayList (); while(irisDataReader.hasNextLine()){ String input = irisDataReader.nextLine(); String[] attr = null; if (input != null ) { attr = input.split(","); } if (attr != null && attr.length == 5 ) { IrisType type = IrisType.getType(attr[4]); Iris sample = new Iris(Double.parseDouble(attr[0]), Double.parseDouble(attr[1]), Double.parseDouble(attr[2]), Double.parseDouble(attr[3]), type); irisDataSet.add(sample); } } return irisDataSet; } } class Iris { double sepalLength; double sepalWidth; double petalLength; double petalWidth; IrisType type; Iris(double sl,double sw, double pl, double pw, IrisType t) { sepalLength = sl; sepalWidth = sw; petalLength = pl; petalWidth = pw; type = t; } @Override public String toString(){ return "SL: "+sepalLength+" SW: "+sepalWidth+" PL: "+petalLength+" PW: "+petalWidth+" Type: "+type.getLabel(); } public boolean equals(Iris other){ return (( this.sepalLength == other.sepalLength) && ( this.petalLength == other.petalLength) && ( this.sepalWidth == other.sepalWidth) && ( this.petalWidth == other.petalWidth) && ( this.type == other.type)); } public double distance(Iris other){ double d1 = Math.pow((this.sepalLength - other.sepalLength),2); double d2 = Math.pow((this.sepalWidth - other.sepalWidth),2); double d3 = Math.pow((this.petalWidth - other.petalWidth),2); double d4 = Math.pow((this.petalLength - other.petalLength),2); return Math.sqrt(d1+d2+d3+d4); } } enum IrisType { SETOSA(1,"Setosa"), VERSICOLOR(2,"Versicolor"), VIRGINICA(3,"Virginica"); private int code; private String label; IrisType(int code, String label) { this.code = code; this.label = label; } public int getCode() { return this.code; } public String getLabel(){ return this.label; } public static IrisType getType(String type) { if ( "Iris-setosa".equals(type) ){ return SETOSA; } if ( "Iris-versicolor".equals(type)){ return VERSICOLOR; } if ( "Iris-virginica".equals(type)){ return VIRGINICA; } return SETOSA; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started