Example #1
0
        // Create 10 records sets
        public void CreateRecordsSets()
        {
            // Calculate needed numbers
            int totalItems   = allRecords.Count;
            int numberOfSets = 10;
            int itemsPerStep = totalItems / numberOfSets;
            int parts        = totalItems / itemsPerStep;

            // Add records for each set
            for (int i = 0; i < parts; i++)
            {
                int from = i * itemsPerStep;
                int to   = i * itemsPerStep + itemsPerStep - 1;

                if (to > totalItems - 1)
                {
                    to = totalItems - 1;
                }

                // Create new set that will be added after that to the list which contains all sets
                RecordsSet newRecordSet = new RecordsSet();

                // Add records to the current set
                for (int j = from; j <= to; j++)
                {
                    newRecordSet.AddRecord(allRecords[j]);
                }

                // Add current set to the list with all sets
                allSets.Add(newRecordSet);
            }
        }
Example #2
0
        // Test 1 set but before that will train from other 9 sets
        public void TestSetByIdx(int testSetIdx, ref double allAccuracy)
        {
            // All probabilities
            List <Probability> probabilities = new List <Probability>();

            Type classType = typeof(Record);

            PropertyInfo[] properties = classType.GetProperties();

            // Calculate the probabilities from training data
            CalculateProbabilities(testSetIdx, probabilities, properties);

            // Test the test set
            RecordsSet testSet = allSets[testSetIdx];

            int correctAnswers = 0; // total answers that are guessed by the AI
            int wrongAnswers   = 0; // wrong answers which are not guessed

            // Loop all the records in the test set and try to guess them
            foreach (var testSetRecord in testSet.Records)
            {
                string correctClassName = testSetRecord.ClassName;

                double probabilityToBeDemocrat = CalculateProbabilityForTestSetRecord(testSetRecord, true, probabilities, properties);

                double probabilityToBeRepublican = CalculateProbabilityForTestSetRecord(testSetRecord, false, probabilities, properties);

                string guessedClassName = string.Empty;

                if (probabilityToBeDemocrat > probabilityToBeRepublican)
                {
                    guessedClassName = "democrat";
                }
                else
                {
                    guessedClassName = "republican";
                }

                if (correctClassName == guessedClassName)
                {
                    correctAnswers++;
                }
                else
                {
                    wrongAnswers++;
                }
            }

            double accuracy = (correctAnswers * 1.0) / (correctAnswers + wrongAnswers) * 1.0;

            accuracy    *= 100;
            allAccuracy += accuracy;

            // Print result for this test set
            Console.WriteLine(new string('_', 20));
            Console.WriteLine($"Test Set: {testSetIdx + 1}");
            Console.WriteLine($"- Correct answers: {correctAnswers}");
            Console.WriteLine($"- Wrong answers: {wrongAnswers}");
            Console.WriteLine($"--> Accuracy: {accuracy:f2} %");
            Console.WriteLine();
            Console.WriteLine();
        }