public void AddAttribute(Attribute attribute) { _attributes.Add(attribute); }
private static double CalculateNormalizedValue(TestData current, Attribute pairAttribute) { return Math.Round(Double.Parse(current.Value.ToString()) / pairAttribute.Precision, 0) * pairAttribute.Precision; }
private double GetProbabilityForContinuousValues(int i, TestData current, Attribute pairAttribute) { var normalizedValue = CalculateNormalizedValue(current, pairAttribute); //continuous var searchedClasses = FindContinuousSearchedClasses(i, current); // formula!!! var values = searchedClasses.GetValues(); var probabilityValue = NormalDistribution(normalizedValue, values[0], values[1], false); return probabilityValue; }
private void CreateDiscreteModel(int targetAttribIndex, Attribute attribute, string currentAttributteName, int i) { foreach (var classes in _naiveBayesModel.TargetAttributeClasses) { var ocurrenceModelList = new List<IOcurrenceModel>(); // total ocurrences by class var selectedData = _naiveBayesModel.DataModel.Data.Cast<List<object>>().Where(item =>item[targetAttribIndex].ToString().Trim().Equals(classes.ClassName, StringComparison.InvariantCultureIgnoreCase)).ToList(); for (var j = 0; j < attribute.Definition.Count; j++) { var ocurrenceModel = new DiscreteAttributeOcurrenceModel(); ocurrenceModel.CurrentAttributeName = currentAttributteName; ocurrenceModel.CurrentAttributeValue = attribute.Definition[j]; ocurrenceModel.TargetAttributeClassName = classes.ClassName; // ocurrences of the current definition by class var value = ((double) selectedData.Cast<List<object>>().Where(item =>item[i].ToString().Equals(attribute.Definition[j], StringComparison.InvariantCultureIgnoreCase)) .Count() + 1.0)/((double) selectedData.Count + (double) attribute.Definition.Count); ocurrenceModel.Value = value; ocurrenceModelList.Add(ocurrenceModel); } //total of the current attribute in range of the class var totalOcurrenceModel = new DiscreteAttributeOcurrenceModel(); totalOcurrenceModel.CurrentAttributeName = currentAttributteName + " total"; totalOcurrenceModel.TargetAttributeClassName = classes.ClassName; totalOcurrenceModel.Value = ocurrenceModelList.Cast<DiscreteAttributeOcurrenceModel>().Sum(m => m.Value); ocurrenceModelList.Add(totalOcurrenceModel); _naiveBayesModel.AddOcurrenceMatrixRange(ocurrenceModelList); } }
private void CreateContinuousModel(int targetAttribIndex, Attribute attribute, int i) { foreach (var classes in _naiveBayesModel.TargetAttributeClasses) { var ocurrenceModelList = new List<IOcurrenceModel>(); var selectedData = _naiveBayesModel.DataModel.Data.Cast<List<object>>().Where(item =>item[targetAttribIndex].ToString().Trim().Equals(classes.ClassName, StringComparison.InvariantCultureIgnoreCase)).ToList(); for (var j = 0; j < attribute.Definition.Count; j++) { var ocurrenceModel = new ContinousAttributeOcurrenceModel(); ocurrenceModel.CurrentAttributeName = attribute.Name; ocurrenceModel.TargetAttributeClassName = classes.ClassName; var selectedItemsValues = selectedData.Select(s => (double) s[i]).ToList(); var currentDistribution = new StandardDeviation(selectedItemsValues); ocurrenceModel.Mean = currentDistribution.GetMean(); ocurrenceModel.StdDev = currentDistribution.GetStandardDeviation(); ocurrenceModel.WeightSum = currentDistribution.GetWeightSum(); ocurrenceModel.Precision = _naiveBayesModel.DataModel.Attributes[i].Precision; ocurrenceModelList.Add(ocurrenceModel); } _naiveBayesModel.AddOcurrenceMatrixRange(ocurrenceModelList); } }
private void CalculateProbabilityForAttribute(int i, Attribute pairAttribute, TestData current, ref List<double> probabilityList) { if (pairAttribute.Definition.Count == 1 && pairAttribute.Definition[0].Equals("Real", StringComparison.InvariantCultureIgnoreCase)) { var probabilityValue = GetProbabilityForContinuousValues(i, current, pairAttribute); probabilityList.Add(probabilityValue); } else { //discrete var searchedClasses = FindDiscreteSearchedClasses(i, current); var value = searchedClasses.GetValues().First(); probabilityList.Add(value); } }