public static double CalculateEuclideanDistance(AdultTrain a, AdultTrain b) { List <double> dist = new List <double>(); //here I am adding the squared difference between the instance variables in a (the test instance) //and b (the training instance) to a list dist.Add(Square(a.age, b.age)); dist.Add(Square(1, Match(a.workclass, b.workclass))); dist.Add(Square(a.fnlwgt, b.fnlwgt)); dist.Add(Square(1, Match(a.education, b.education))); dist.Add(Square(a.educationNum, b.educationNum)); dist.Add(Square(1, Match(a.maritalStatus, b.maritalStatus))); dist.Add(Square(1, Match(a.occupation, b.occupation))); dist.Add(Square(1, Match(a.relationship, b.relationship))); dist.Add(Square(1, Match(a.race, b.race))); dist.Add(Square(1, Match(a.sex, b.sex))); dist.Add(Square(a.capitalGain, b.capitalGain)); dist.Add(Square(a.capitalLoss, b.capitalLoss)); dist.Add(Square(a.hoursPerWeek, b.hoursPerWeek)); dist.Add(Square(1, Match(a.nativeCountry, b.nativeCountry))); double sum = 0; //here I am summing all the squared distances together foreach (var dis in dist) { sum += dis; } //here I am calculating the square root of the sum and returning it which is the //euclidean distance return(Math.Sqrt(sum)); }
public static double CalculateEuclideanDistance(AdultTest a, AdultTrain b) { List <double> dist = new List <double>(); dist.Add(Square(a.age, b.age)); dist.Add(Square(1, Match(a.workclass, b.workclass))); dist.Add(Square(a.fnlwgt, b.fnlwgt)); dist.Add(Square(1, Match(a.education, b.education))); dist.Add(Square(a.educationNum, b.educationNum)); dist.Add(Square(1, Match(a.maritalStatus, b.maritalStatus))); dist.Add(Square(1, Match(a.occupation, b.occupation))); dist.Add(Square(1, Match(a.relationship, b.relationship))); dist.Add(Square(1, Match(a.race, b.race))); dist.Add(Square(1, Match(a.sex, b.sex))); dist.Add(Square(a.capitalGain, b.capitalGain)); dist.Add(Square(a.capitalLoss, b.capitalLoss)); dist.Add(Square(a.hoursPerWeek, b.hoursPerWeek)); dist.Add(Square(1, Match(a.nativeCountry, b.nativeCountry))); double sum = 0; foreach (var dis in dist) { sum += dis; } return(Math.Sqrt(sum)); }
public static void CountError(AdultTrain input, TupleList <double, int> distLabels, int inputLabel, int split, int k) { //in this class I loop through the sorted distances and add all distances labelled <=50K and >50K //to seperate lists, then when i equals a desired tested K, I classify the test instance as the class list //with more distances, I then increment the number of errors in the current fold for the K if the test instance was incorrectly classified int i = 1; List <double> lThan50 = new List <double>(); List <double> gThan50 = new List <double>(); foreach (var dl in distLabels) { if (dl.Item2 == 0) { lThan50.Add(dl.Item1); } else { gThan50.Add(dl.Item1); } if (i % 2 != 0 && i != 0) { int label = 1; if (lThan50.Count > gThan50.Count) { label = 0; } if (inputLabel != label) { kAccs[i][split - 1]++; } } if (i == 39) { break; } i++; } }