예제 #1
0
        public PlayPrediction Predict(Day data)
        {
            var playLikelihood    = predictLikelihood(avc => avc.Play,    data);
            var notPlayLikelihood = predictLikelihood(avc => avc.NotPlay, data);

            var combinedLiklihood = playLikelihood + notPlayLikelihood;

            return playLikelihood > notPlayLikelihood
                ? new PlayPrediction(true,     playLikelihood,    playLikelihood / combinedLiklihood)
                : new PlayPrediction(false, notPlayLikelihood, notPlayLikelihood / combinedLiklihood);
        }
예제 #2
0
 private double predictLikelihood(Func<AttributeValueCount, int> totalGetter, Day data)
 {
     BigInteger totalInCategory = totalGetter(_totalPerCategory);
     var probabilities = _attributes
         .Select(a =>
         {
             var value = a.GetValueFrom(data);
             var subTotal = totalGetter(a.ValueCounts[value]);
             return Rational.Get(subTotal, totalInCategory);
         })
         .Concat(new[]
         {
             Rational.Get(totalInCategory, _total)
         }).ToList().AsReadOnly();
     return probabilities
         .Aggregate((double)1, (a, b) => a*(b.ToDouble()));
 }
예제 #3
0
 /* Implementation of Naive Bayes for the Golf/Weather dataset.
  * Principles taken from https://www.youtube.com/watch?v=IlVINQDk4o8
  * NOTE: The fellow in the video mis-counts "Sunny" (he counts 2 Yes, 3 No instead of 3 Yes, 2 No),
  * leading to incorrect probabilities (he calculates 2/9 Yes, 3/5 No rather than 3/9 Yes, 2/5 No) and likelihoods.
  */
 public static void Main(string[] args)
 {
     var dataSet = getTrainingDataSet();
     Console.WriteLine("Raw DataSet:");
     Writer.DataSetTable(dataSet);
     Console.WriteLine();
     var summary = new GolfDayPredictor(dataSet);
     Console.WriteLine("Naive Bayes has been applied to the DataSet above, which links weather conditions to a decision to play golf or not.");
     Console.WriteLine();
     Console.WriteLine("Next, we are given a description of weather conditions on an arbitrary day: ");
     var novelGolfDay = new Day(Outlook.Sunny, Temp.Cool, Humidity.High, true);
     var prediction = summary.Predict(novelGolfDay);
     Writer.DataSetRow(novelGolfDay);
     Console.WriteLine();
     Console.WriteLine("The Naive Bayes learner predicts:");
     Console.WriteLine();
     Console.Write("\t");
     Writer.Prediction(prediction);
     Console.WriteLine();
     Console.WriteLine("Press any key to exit");
     Console.ReadKey();
 }