static void Main(string[] args) { Spacer[] spacerDecisions = new Spacer[] { new Spacer(WeatherKind.Sun, 23, WindKind.Normal, true), new Spacer(WeatherKind.Rain, 15, WindKind.Strong, false), new Spacer(WeatherKind.Clouds, 17, WindKind.Light, true), new Spacer(WeatherKind.Clouds, 21, WindKind.Normal, false), new Spacer(WeatherKind.Sun, 20, WindKind.Strong, true), new Spacer(WeatherKind.Sun, 25, WindKind.Light, true), new Spacer(WeatherKind.Rain, 22, WindKind.Light, true), new Spacer(WeatherKind.Sun, 14, WindKind.Strong, false), new Spacer(WeatherKind.Clouds, 19, WindKind.Strong, false), new Spacer(WeatherKind.Rain, 16, WindKind.Light, false), }; Spacer spacerToClassify = new Spacer(WeatherKind.Rain, 24, WindKind.Light, null); BayesClassificator bayesClassificator = new BayesClassificator(spacerDecisions, 1); (Boolean, Double[])result = bayesClassificator.GetSpacerClassificationAndProbability(spacerToClassify);
public (Boolean, Double[]) GetSpacerClassificationAndProbability(Spacer spacerToClassify) { Boolean decision = false; decisionClassTruePower = (Double)Decisions.Count(d => d.Decision.Value); Double decisionClassTrueProb = decisionClassTruePower / (Double)Decisions.Count(); decisionClassFalsePower = (Double)Decisions.Count(d => !d.Decision.Value); Double decisionClassFalseProb = decisionClassFalsePower / (Double)Decisions.Count(); Double param1ProbTrue, param2ProbTrue, param3ProbTrue = 0d; Double param1ProbFalse, param2ProbFalse, param3ProbFalse = 0d; if (Decisions.Count(d => d.Weather == spacerToClassify.Weather && d.Decision.Value) == 0) { param1ProbTrue = GetCorrectedProbability(typeof(WeatherKind), true); } else { param1ProbTrue = (((Double)Decisions.Count(d => d.Weather == spacerToClassify.Weather && d.Decision.Value) / 10d) / (decisionClassTrueProb)); } if (Decisions.Count(d => d.Wind == spacerToClassify.Wind && d.Decision.Value) == 0) { param2ProbTrue = GetCorrectedProbability(typeof(WindKind), true); } else { param2ProbTrue = (((Double)Decisions.Count(d => d.Wind == spacerToClassify.Wind && d.Decision.Value) / 10d) / (decisionClassTrueProb)); } if (Decisions.Count(d => d.Temperature == spacerToClassify.Temperature && d.Decision.Value) == 0) { param3ProbTrue = GetCorrectedProbability(typeof(TemperatureKind), true); } else { param3ProbTrue = (((Double)Decisions.Count(d => d.Temperature == spacerToClassify.Temperature && d.Decision.Value) / 10d) / (decisionClassTrueProb)); } if (Decisions.Count(d => d.Weather == spacerToClassify.Weather && !d.Decision.Value) == 0) { param1ProbFalse = GetCorrectedProbability(typeof(WeatherKind), false); } else { param1ProbFalse = (((Double)Decisions.Count(d => d.Weather == spacerToClassify.Weather && !d.Decision.Value) / 10d) / (decisionClassFalseProb)); } if (Decisions.Count(d => d.Wind == spacerToClassify.Wind && !d.Decision.Value) == 0) { param2ProbFalse = GetCorrectedProbability(typeof(WindKind), false); } else { param2ProbFalse = (((Double)Decisions.Count(d => d.Wind == spacerToClassify.Wind && !d.Decision.Value) / 10d) / (decisionClassFalseProb)); } if (Decisions.Count(d => d.Temperature == spacerToClassify.Temperature && !d.Decision.Value) == 0) { param3ProbFalse = GetCorrectedProbability(typeof(TemperatureKind), false); } else { param3ProbFalse = (((Double)Decisions.Count(d => d.Temperature == spacerToClassify.Temperature && !d.Decision.Value) / 10d) / (decisionClassFalseProb)); } Double finalTrueClassProb = decisionClassTrueProb * param1ProbTrue * param2ProbTrue * param3ProbTrue; Double finalFalseClassProb = decisionClassFalseProb * param1ProbFalse * param2ProbFalse * param3ProbFalse; decision = finalTrueClassProb > finalFalseClassProb; return(decision, new Double[] { finalTrueClassProb, finalFalseClassProb });