private static double GetDistanceBetweenRecords(DataRecord LHS, DataRecord RHS, string IDAttributeName, string ClassificationAttributeName, double Power = 2d)
        {
            double distance = 0;

            foreach (string attributeName in LHS.GetAttributes())
            {
                if (attributeName != ClassificationAttributeName && attributeName != IDAttributeName)
                {
                    distance += Math.Pow(Math.Abs(double.Parse(LHS.GetAttributeValue(attributeName)) - double.Parse(RHS.GetAttributeValue(attributeName))), Power);
                }
            }
            return(Math.Pow(distance, (1 / Power)));
        }
        private static double GetDistanceFromZero(DataRecord Record, string IDAttributeName, string ClassificationAttributeName, double Power = 2d)
        {
            double     distance = 0;
            DataRecord zero     = new DataRecord();

            foreach (string attribute in Record.GetAttributes())
            {
                zero.AddOrUpdateValue(attribute, "0");
            }
            foreach (string attributeName in Record.GetAttributes())
            {
                if (attributeName != ClassificationAttributeName && attributeName != IDAttributeName)
                {
                    distance += Math.Pow(Math.Abs(double.Parse(Record.GetAttributeValue(attributeName)) - double.Parse(zero.GetAttributeValue(attributeName))), Power);
                }
            }
            return(Math.Pow(distance, (1 / Power)));
        }
Beispiel #3
0
        public override Classification Classify(DataRecord Record)
        {
            Classification classification = new Classification();

            foreach (Rule r in Rules)
            {
                bool satisfied = true;
                foreach (Rule.RuleComponent rc in r.Components)
                {
                    if (satisfied)
                    {
                        bool localSatisfied = false;
                        for (int rcIndex = 0; rcIndex < rc.Comparators.Length; ++rcIndex)
                        {
                            if (!localSatisfied)
                            {
                                switch (rc.Comparators[rcIndex])
                                {
                                case Rule.EComparator.Equal:
                                    decimal lhs, rhs;
                                    if (decimal.TryParse(Record.GetAttributeValue(rc.LHSAttribute), out lhs))
                                    {
                                        if (decimal.TryParse(rc.RHSValues[rcIndex], out rhs))
                                        {
                                            localSatisfied = lhs == rhs;
                                            break;
                                        }
                                    }
                                    localSatisfied = Record.GetAttributeValue(rc.LHSAttribute) == rc.RHSValues[rcIndex];
                                    break;

                                case Rule.EComparator.GreaterThan:
                                    localSatisfied = Decimal.Parse(Record.GetAttributeValue(rc.LHSAttribute)) > Decimal.Parse(rc.RHSValues[rcIndex]);
                                    break;

                                case Rule.EComparator.LessThan:
                                    localSatisfied = Decimal.Parse(Record.GetAttributeValue(rc.LHSAttribute)) < Decimal.Parse(rc.RHSValues[rcIndex]);
                                    break;

                                case Rule.EComparator.GreaterThanInclusive:
                                    localSatisfied = Decimal.Parse(Record.GetAttributeValue(rc.LHSAttribute)) >= Decimal.Parse(rc.RHSValues[rcIndex]);
                                    break;

                                case Rule.EComparator.LessThanInclusive:
                                    localSatisfied = Decimal.Parse(Record.GetAttributeValue(rc.LHSAttribute)) <= Decimal.Parse(rc.RHSValues[rcIndex]);
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        satisfied = localSatisfied;
                    }
                    else
                    {
                        break;
                    }
                }
                if (satisfied)
                {
                    classification.AddMatch(r.Classification, r.GetRule());
                    satisfied = false;
                }
            }
            if (classification.Classifications.Length == 0)
            {
                classification.AddMatch(DefaultClassification, "None");
            }
            return(classification);
        }
Beispiel #4
0
        /// <summary>
        /// Get the classification target of a record.
        /// </summary>
        /// <param name="Record"></param>
        /// <returns></returns>
        public Classification Classify(DataRecord Record)
        {
            //For every child of this node.
            for (int i = 0; i < Comparators.Count; ++i)
            {
                switch (Comparators[i])
                {
                case (">"):
                    if (Decimal.Parse(Record.GetAttributeValue(LHSAttributes[i])) > Decimal.Parse(RHSValues[i]))
                    {
                        if (Classifications.ContainsKey(i))
                        {
                            return(new Classification(new string[] { Classifications[i] },
                                                      new string[] { this.WriteThisBranch(null, "", i) }));
                        }
                        else
                        {
                            return(Nodes[i].Classify(Record));
                        }
                    }
                    break;

                case ("<"):
                    if (Decimal.Parse(Record.GetAttributeValue(LHSAttributes[i])) < Decimal.Parse(RHSValues[i]))
                    {
                        if (Classifications.ContainsKey(i))
                        {
                            return(new Classification(new string[] { Classifications[i] },
                                                      new string[] { this.WriteThisBranch(null, "", i) }));
                        }
                        else
                        {
                            return(Nodes[i].Classify(Record));
                        }
                    }
                    break;

                case (">="):
                    if (Decimal.Parse(Record.GetAttributeValue(LHSAttributes[i])) >= Decimal.Parse(RHSValues[i]))
                    {
                        if (Classifications.ContainsKey(i))
                        {
                            return(new Classification(new string[] { Classifications[i] },
                                                      new string[] { this.WriteThisBranch(null, "", i) }));
                        }
                        else
                        {
                            return(Nodes[i].Classify(Record));
                        }
                    }
                    break;

                case ("<="):
                    if (Decimal.Parse(Record.GetAttributeValue(LHSAttributes[i])) <= Decimal.Parse(RHSValues[i]))
                    {
                        if (Classifications.ContainsKey(i))
                        {
                            return(new Classification(new string[] { Classifications[i] },
                                                      new string[] { this.WriteThisBranch(null, "", i) }));
                        }
                        else
                        {
                            return(Nodes[i].Classify(Record));
                        }
                    }
                    break;

                case ("="):
                    if (Record.GetAttributeValue(LHSAttributes[i]) == RHSValues[i])
                    {
                        if (Classifications.ContainsKey(i))
                        {
                            return(new Classification(new string[] { Classifications[i] },
                                                      new string[] { this.WriteThisBranch(null, "", i) }));
                        }
                        else
                        {
                            return(Nodes[i].Classify(Record));
                        }
                    }
                    break;

                default:
                    throw new Exception("Invalid comparator: " + Comparators[i]);
                }
            }
            throw new Exception("Invalid data value or incomplete tree.");
        }