static void GenerateAndExecuteExperiments(List <BreastCancerCase> data, double ratio) { int nChoices = 2; List <float> weightOptions = Enumerable.Range(1, nChoices) .Select(x => (float)x / nChoices) .ToList(); // List<double> ratioChoices = new List<double>() {0.1, 0.2, 0.3}; int k = TumourFeatures.FeatureCount; using (var writer = new StreamWriter("./experiments" + ratio + ".csv")) { Enumerable.Range(1, k * 3).ToList() .ForEach(x => writer.Write("w" + x + ",")); writer.WriteLine("ratio,acc"); //foreach (double ratio in ratioChoices) { var generator = GenerateWeights(k, weightOptions); //int replications = 20; int replications = 1; int combinations = (int)Math.Pow(weightOptions.Count, k) * replications; int current = 0; List <float> weights = new List <float>() { 1, 0.5f, 1, 1, 1, 0.5f, 1, 1, 0.5f, 0.5f }; //foreach (List<float> weights in generator) //{ ReplicateUntil(weights, k * 3); //List<double> values = new List<double>(); for (int i = 0; i < replications; i++) { ExperimentSetup setup = new ExperimentSetup(data, ratio); setup.MeanWeights = weights.GetRange(0, k); setup.ErrorWeights = weights.GetRange(k, k); setup.ExtremeWeights = weights.GetRange(k * 2, k); double accuracy = RunExperiment(setup, true); weights.ForEach(x => writer.Write(x + ",")); writer.WriteLine(ratio + "," + accuracy); Console.WriteLine((current++) + "/" + combinations + " => " + accuracy); } //} //} } }
static double RunExperiment(ExperimentSetup setup, bool print = false) { int n = setup.TestSet.Count; int[,] confusionMatrix = new int[n, n]; // Implicitly filled with zeros foreach (BreastCancerCase testCase in setup.TestSet) { List <BreastCancerCase> ordered = setup.CaseBase .OrderByDescending(value => value.CompareTo( testCase, setup.MeanWeights, setup.ErrorWeights, setup.ExtremeWeights )).ToList(); if (print) { Console.WriteLine("Case under evaluation: "); testCase.Print(); Console.WriteLine("\nMost similar cases in the database:\n"); } bool actual = testCase.Malignant; bool predicted = ordered[0].Malignant; int row = predicted ? 1 : 0; int column = actual ? 1 : 0; confusionMatrix[row, column]++; if (print) { for (int i = 0; i < 5; i++) { BreastCancerCase match = ordered[i]; double y = match.CompareTo(testCase, setup.MeanWeights, setup.ErrorWeights, setup.ExtremeWeights); Console.WriteLine("Match n. #" + i); match.Print(); bool valid = match.Malignant == actual; Console.WriteLine("\nSimilarity: " + y); Console.WriteLine("Valid diagnosis: " + (valid ? "YES" : "NO")); Console.WriteLine(); } } } // True Negatives + True Positives / Negatives + Positives double accuracy = (double)(confusionMatrix[0, 0] + confusionMatrix[1, 1]) / (double)n; if (print) { Console.WriteLine("Outcome:"); Console.WriteLine("\tB\tM"); Console.WriteLine("B\t" + confusionMatrix[0, 0] + "\t" + confusionMatrix[0, 1]); Console.WriteLine("M\t" + confusionMatrix[1, 0] + "\t" + confusionMatrix[1, 1]); Console.WriteLine("Accuracy: " + accuracy); } return(accuracy); }