Beispiel #1
0
        // Similarity comparison
        public double CompareTo(BreastCancerCase other, List <float> meanWeights,
                                List <float> errorWeights, List <float> extremeWeights)
        {
            List <double> values = new List <double>();

            values.Add(this.Mean.CalculateSimilarity(other.Mean, meanWeights));
            values.Add(this.StandardError.CalculateSimilarity(other.StandardError, errorWeights));
            values.Add(this.Extreme.CalculateSimilarity(other.Extreme, extremeWeights));
            return(values.Average());
        }
Beispiel #2
0
        // Creates a new randomized experiment setup, splitting the data in two parts
        public ExperimentSetup(List <BreastCancerCase> dataset, double testRatio)
        {
            int testCount = (int)Math.Round(dataset.Count * testRatio);

            Random rng = new Random();
            int    n   = dataset.Count;

            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                BreastCancerCase value = dataset[k];
                dataset[k] = dataset[n];
                dataset[n] = value;
            }

            CaseBase  = dataset.GetRange(0, dataset.Count - testCount);
            TestSet   = dataset.GetRange(dataset.Count - testCount, testCount);
            TestRatio = testRatio;
        }
Beispiel #3
0
        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);
        }