Example #1
0
        public int?Predict(Passenger item, IEnumerable <string> fields)
        {
            if (CountNo == 0)
            {
                return(1);
            }

            if (fields.Count() == 0)
            {
                return(guessResult());
                //int result = P > 0.5 ? 1 : 0;
                //return result;
            }

            var field = fields.First();
            //string.Format("{0}.{1}", pFieldName, node1),
            var val       = UnitTest1.GetPropValue(item, field).ToString();
            var fieldName = string.Format("{0}.{1}", field, val);
            var ch1       = Children.Where(x => x.FieldName == fieldName).SingleOrDefault();

            if (ch1 == null)
            {
                return(guessResult());
            }
            if (ch1.CountNo == 0)
            {
                return(1);
            }
            return(ch1.Predict(item, fields.Skip(1)));
        }
Example #2
0
        public void BuildChildNode(string pFieldName, TitanicNode parent)
        {
            var listOfDistinData = parent.DataSet.Select(x => UnitTest1.GetPropValue(x, pFieldName)).Distinct();

            foreach (var node1 in listOfDistinData)
            {
                var node_1_list = parent.DataSet.Where(x => UnitTest1.GetPropValue(x, pFieldName).Equals(node1)).ToList();
                var n1          = new TitanicNode
                {
                    FieldName = string.Format("{0}.{1}", pFieldName, node1),
                    Count     = node_1_list.Count,
                    CountYes  = node_1_list.Where(x => x.Survived == 1).Count(),
                    DataSet   = node_1_list,
                };
                parent.Children.Add(n1);
            }
        }
        public bool Predict(Weather item, IEnumerable <string> fields)
        {
            if (CountNo == 0)
            {
                return(true);
            }
            if (fields.Count() == 0)
            {
                return(false);
            }
            var ch1 = Children.Where(x => x.FieldName == UnitTest1.GetPropValue(item, fields.First()).ToString()).Single();

            if (ch1.CountNo == 0)
            {
                return(true);
            }
            return(ch1.Predict(item, fields.Skip(1)));
        }
Example #4
0
        public double GI <E>(string pFieldName, List <Passenger> dataSet)
            where E : struct, IComparable, IFormattable, IConvertible
        {
            double gi = 0;

            double total = dataSet.Count;
            double cYes  = (from x in dataSet where x.Survived == 1 select x).Count();

            List <double> childsE = new List <double>();
            List <double> childsP = new List <double>();

            foreach (var item in Enum.GetValues(typeof(E)))
            {
                var en = (E)item;

                double t     = (from x in dataSet where UnitTest1.GetPropValue(x, pFieldName).Equals(en) select x).Count();
                double cCYes = (from x in dataSet where x.Survived == 1 && UnitTest1.GetPropValue(x, pFieldName).Equals(en) select x).Count();

                if (t == 0)
                {
                    continue;
                }

                var e = UnitTest1.Entropy(cCYes / t);
                var p = t / total;

                childsE.Add(e);
                childsP.Add(p);
            }

            var eP = UnitTest1.Entropy(cYes / total);

            double wEC = 0;

            for (int i = 0; i < childsE.Count; i++)
            {
                wEC += childsE[i] * childsP[i];
            }

            gi = eP - wEC;

            return(gi);
        }
Example #5
0
        public double GI(string pFieldName, List <Passenger> dataSet)
        {
            double gi = 0;
            var    listOfDistinData = dataSet.Select(x => UnitTest1.GetPropValue(x, pFieldName)).Distinct();
            double total            = dataSet.Count;
            double cYes             = (from x in dataSet where x.Survived == 1 select x).Count();

            List <double> childsE = new List <double>();
            List <double> childsP = new List <double>();

            foreach (var item in listOfDistinData)
            {
                //var en = (E)item;

                double t     = (from x in dataSet where UnitTest1.GetPropValue(x, pFieldName).Equals(item) select x).Count();
                double cCYes = (from x in dataSet where x.Survived == 1 && UnitTest1.GetPropValue(x, pFieldName).Equals(item) select x).Count();

                if (t == 0)
                {
                    continue;
                }

                var e = UnitTest1.Entropy(cCYes / t);
                var p = t / total;

                childsE.Add(e);
                childsP.Add(p);
            }

            var eP = UnitTest1.Entropy(cYes / total);

            double wEC = 0;

            for (int i = 0; i < childsE.Count; i++)
            {
                wEC += childsE[i] * childsP[i];
            }

            gi = eP - wEC;

            return(gi);
        }
Example #6
0
        public int?Predict(Passenger item, IEnumerable <string> fields)
        {
            if (CountNo == 0)
            {
                return(1);
            }
            if (fields.Count() == 0)
            {
                //int result = s_Generator.NextDouble() <= P ? 1 : 0;
                //return result;
                int result = P > 0.5 ? 1 : 0;
                return(result);
            }
            var ch1 = Children.Where(x => x.FieldName == UnitTest1.GetPropValue(item, fields.First()).ToString()).Single();

            if (ch1.CountNo == 0)
            {
                return(1);
            }
            return(ch1.Predict(item, fields.Skip(1)));
        }
Example #7
0
        public void BuildChildNode <E>(string pFieldName, TitanicNode parent)
            where E : struct, IComparable, IFormattable, IConvertible
        {
            foreach (var node1 in Enum.GetValues(typeof(E)))
            {
                var e           = (E)node1;
                var node_1_list = parent.DataSet.Where(x => UnitTest1.GetPropValue(x, pFieldName).Equals(e)).ToList();

                var n1 = new TitanicNode
                {
                    FieldName = e.ToString(),
                    Count     = node_1_list.Count,
                    CountYes  = node_1_list.Where(x => x.Survived == 1).Count(),
                    DataSet   = node_1_list,
                };
                parent.Children.Add(n1);
                //yield return n1;

                //if (n1.CountYes == 0) break;
                //if (pFieldName == "") break;
                //if (pFieldName == "") break;
                //n1.Children.AddRange(BuildChildNode<Humanity>("Humanity", node_1_list));
            }
        }