Exemplo n.º 1
0
 /// <summary>
 /// 递归获取模型输出值
 /// </summary>
 /// <param name="point"></param>
 /// <param name="node"></param>
 /// <returns></returns>
 private static double Judge(CARTPoint point, CARTNode node)
 {
     if (node.left != null)
     {
         return(node.output);
     }
     else
     {
         if (point.vals[node.j] < node.splitVal)
         {
             return(Judge(point, node.left));
         }
         else
         {
             return(Judge(point, node.right));
         }
     }
 }
Exemplo n.º 2
0
        public void Init(string path)
        {
            var lines = File.ReadAllLines(path);
            int flag  = 0;  // 1: train-data; 1: verify-data

            foreach (var line in lines)
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                if (line.StartsWith("@ATTRIBUTE"))
                {
                    var segs = line.Split(new[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    attrNames.Add(segs[1]);

                    if (segs[2] != "cont")
                    {
                        var vals = segs[3].Split('/');
                        real2Disc.Add(J, vals);

                        var dict = new Dictionary <string, double>();
                        for (int i = 0; i < vals.Length; i++)
                        {
                            dict.Add(vals[i], i);
                        }
                        disc2Real.Add(J, dict);
                    }

                    J++;
                }
                else if (line.StartsWith("@train-data"))
                {
                    flag = 1;
                }
                else if (line.StartsWith("@verify-data"))
                {
                    flag = 2;
                }
                else
                {
                    var segs  = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    var point = new CARTPoint(segs.Length);
                    for (int i = 0; i < segs.Length; i++)
                    {
                        double d;
                        if (!double.TryParse(segs[i], out d))
                        {
                            // 离散值,获取对应的映射实数
                            d = disc2Real[i][segs[i]];
                        }
                        point.vals[i] = d;
                    }
                    if (flag == 1)
                    {
                        // 训练数据
                        trainSet.Add(point);
                    }
                    else
                    {
                        // 验证数据
                        verifySet.Add(point);
                    }
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 决策:根据输入计算模型输出值
 /// </summary>
 /// <param name="point">样本点</param>
 /// <param name="tree">决策树</param>
 /// <returns></returns>
 public static double Judge(CARTPoint point, CARTTree tree) => Judge(point, tree._root);