import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; public class KNearestNeighbour { private int numOfFolds; private int kValue = 3; private String fileName; private String trainingFileName; private String testingFileName; public double[][] trainingMatrix; public double[][] testingMatrix; public double[][] dataMatrix; private int dataSampleCount; private List categoricalIndexStorer; private Map map; //constructor for KNN public KNearestNeighbour ( int numOfFolds, String fileName ) { this.numOfFolds = numOfFolds; this.fileName = fileName; this.categoricalIndexStorer = new ArrayList<>(); this.map = new HashMap(); } public KNearestNeighbour(String trainingFileName, String testingFileName) { this.trainingFileName = trainingFileName; this.testingFileName = testingFileName; this.categoricalIndexStorer = new ArrayList<>(); this.map = new HashMap(); } public double[][] readFeatureValues(String path, String fileName) { Path filePath = null; try { filePath = Paths.get(path, fileName); List dataSamples = Files.readAllLines(filePath, StandardCharsets.UTF_8); int rows = dataSamples.size(); this.dataSampleCount = rows; int columns = dataSamples.get(0).trim().split("\\s+").length; dataMatrix = new double[rows][columns + 2]; double count = 0; String[] singleRecord = dataSamples.get(0).trim().split("\\s+"); for (int k = 0; k < columns; k++) { try { Double.parseDouble(singleRecord[k]); } catch (Exception e) { categoricalIndexStorer.add(k); } } for (int i = 0; i < rows; i++) { String[] singleDataSampleValue = dataSamples.get(i).trim().split("\\s+"); dataMatrix[i][columns] = -1; dataMatrix[i][columns+1]= -1; for (int j = 0; j < columns; j++) { try { dataMatrix[i][j] = Double.parseDouble(singleDataSampleValue[j]); } catch (Exception e) { StringBuilder string = new StringBuilder(); string.append(singleDataSampleValue[j]).append(String.valueOf(j)); if (map.containsKey(string)) { dataMatrix[i][j] = map.get(singleDataSampleValue[j]); } else { map.put(string.toString(), count); dataMatrix[i][j] = count; count++; } } } } } catch (Exception e) { e.printStackTrace(); } List ignoreList = categoricalIndexStorer; ignoreList.add(dataMatrix[0].length-3); int flag = 1; CrossValidation CV = new CrossValidation(dataMatrix,numOfFolds); dataMatrix = CV.getNormalizedMatrix(dataMatrix,ignoreList, flag); return dataMatrix; } //calculation of the distance matrix for easy access later. public double[][] calculateDistanceMatrix () { double[][] distanceMatrix = null; try { distanceMatrix = new double[dataMatrix.length][dataMatrix.length]; for ( int i = 0; i < dataMatrix.length - 1; i++ ) { for ( int j = i + 1; j < dataMatrix.length; j++ ) { double[] object1 = dataMatrix[i]; double[] object2 = dataMatrix[j]; double squaredSum = 0; for ( int dim = 0; dim < object1.length - 3; dim++ ) { squaredSum += Math.pow(object1[dim] - object2[dim], 2); } double eucDistance = Math.sqrt(squaredSum); distanceMatrix[i][j] = eucDistance; distanceMatrix[j][i] = eucDistance; } } } catch ( Exception e ) { e.printStackTrace(); } return distanceMatrix; } //for the demo files public void demoKNNalgorithm(double[][] trainingMatrix, double[][] testingMatrix) { double truePositive = 0; double trueNegative = 0; double falsePositive = 0; double falseNegative = 0; double truePositiveWeight = 0; double trueNegativeWeight = 0; double falsePositiveWeight = 0; double falseNegativeWeight = 0; double totalAccuracy = 0; double totalPrecision = 0; double totalRecall = 0; double totalF1measure = 0; double totalAccuracyWeight = 0; double totalPrecisionWeight = 0; double totalRecallWeight = 0; double totalF1measureWeight = 0; for(int i=0; i< testingMatrix.length;i++) { //KnnAlgorithm(distMatrix,j,foldSize, testSetStart); //demoKNNSingleTestCasePrediction(i); //for every test case int predValue = -1; double predWeight = -1; List nearestIndices = new ArrayList(); List nearestDist = new ArrayList(); List tempList = new ArrayList(); //creating a templist of the testcase observation for(int x = 0;xclass1) { predValue = 0; } else { predValue = 1; } if(weight0>weight1) { predWeight = 0; } else { predWeight = 1; } //System.out.println("pred value =" + predValue); testingMatrix[i][testingMatrix[0].length-2] = predValue; testingMatrix[i][testingMatrix[0].length-1] = predWeight; //for count values if(testingMatrix[i][testingMatrix[0].length-3] == testingMatrix[i][testingMatrix[0].length-2]) { if(testingMatrix[i][testingMatrix[0].length-3]==0) { trueNegative++; } else { truePositive++; } } else { if(testingMatrix[i][testingMatrix[0].length-3]==0) { falsePositive++; } else { falseNegative++; } } //for weight values if(testingMatrix[i][testingMatrix[0].length-3] == testingMatrix[i][testingMatrix[0].length-1]) { if(testingMatrix[i][testingMatrix[0].length-3]==0) { trueNegativeWeight++; } else { truePositiveWeight++; } } else { if(testingMatrix[i][testingMatrix[0].length-3]==0) { falsePositiveWeight++; } else { falseNegativeWeight++; } } } /*System.out.println("TP = " + truePositive); System.out.println("TN = " + trueNegative); System.out.println("FP = " + falsePositive); System.out.println("FN = " + falseNegative);*/ totalAccuracy = ((truePositive + trueNegative)/(truePositive + trueNegative + falsePositive + falseNegative)); totalPrecision = ((truePositive) / (truePositive + falsePositive)); totalRecall = ((truePositive) / (truePositive + falseNegative)); totalF1measure = (((double)2*truePositive) / ((2*truePositive) + falseNegative + falsePositive)); totalAccuracyWeight = ((truePositiveWeight + trueNegativeWeight)/(truePositiveWeight + trueNegativeWeight + falsePositiveWeight + falseNegativeWeight)); totalPrecisionWeight = (truePositiveWeight / (truePositiveWeight + falsePositiveWeight)); totalRecallWeight = (truePositiveWeight / (truePositiveWeight + falseNegativeWeight)); totalF1measureWeight = ((double)2*truePositiveWeight / (2*truePositiveWeight + falseNegativeWeight + falsePositiveWeight)); System.out.println("Accuracy = " + totalAccuracy); System.out.println("Precision = " + totalPrecision); System.out.println("Recall = " + totalRecall); System.out.println("F1Measure = " + totalF1measure); /*System.out.println("AccuracyWeight = " + totalAccuracyWeight); System.out.println("PrecisionWeight = " + totalPrecisionWeight); System.out.println("RecallWeight = " + totalRecallWeight); System.out.println("F1MeasureWeight = " + totalF1measureWeight);*/ } /*//demo KNN algorithm for every test case prediction public void demoKNNSingleTestCasePrediction(double[][] trainingMatrix, double[][] testingMatrix) { }*/ //Cross validation starts public void startCrossValidation(double[][] dataMatrix) { double truePositive = 0; double trueNegative = 0; double falsePositive = 0; double falseNegative = 0; double truePositiveWeight = 0; double trueNegativeWeight = 0; double falsePositiveWeight = 0; double falseNegativeWeight = 0; double totalAccuracy = 0; double totalPrecision = 0; double totalRecall = 0; double totalF1measure = 0; double totalAccuracyWeight = 0; double totalPrecisionWeight = 0; double totalRecallWeight = 0; double totalF1measureWeight = 0; CrossValidation CVclass = new CrossValidation(dataMatrix,numOfFolds); List splitSetsList = CVclass.generateKFoldSplit(dataMatrix,numOfFolds); int foldSize = dataMatrix.length/numOfFolds; int testSetStart = 0; for(int i=0;iclass1) { predValue = 0; } else { predValue = 1; } if(weight0>weight1) { predWeight = 0; } else { predWeight = 1; } //System.out.println("pred value =" + predValue); dataMatrix[testSetIndex][dataMatrix[0].length-2] = predValue; dataMatrix[testSetIndex][dataMatrix[0].length-1] = predWeight; } }