Ejemplo n.º 1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dtree"></param>
        private void InteractiveClassification(CvDTree dtree)
        {
            if (dtree == null)
            {
                return;
            }

            CvDTreeNode root = dtree.GetRoot();
            CvDTreeTrainData data = dtree.GetData();
            string input;

            for (; ; )
            {
                CvDTreeNode node;

                Console.Write("Start/Proceed with interactive mushroom classification (y/n): ");
                input = Console.ReadLine();
                if (input[0] != 'y' && input[0] != 'Y')
                {
                    break;
                }
                Console.WriteLine("Enter 1-letter answers, '?' for missing/unknown value...");

                // custom version of predict
                node = root;
                for (; ; )
                {
                    CvDTreeSplit split = node.Split;
                    int dir = 0;

                    if (node.Left == null || node.Tn <= dtree.GetPrunedTreeIdx() || node.Split == null)
                    {
                        break;
                    }

                    for (; split != null; )
                    {
                        int j;
                        int vi = split.VarIdx;
                        int count = data.CatCount.DataArrayInt32[vi];


                        Console.Write("{0}: ", VarDesc[vi]);
                        input = Console.ReadLine();


                        if (input[0] == '?')
                        {
                            split = split.Next;
                            continue;
                        }

                        // convert the input character to the normalized value of the variable
                        unsafe
                        {
                            int* map = data.CatMap.DataInt32 + data.CatOfs.DataInt32[vi];
                            for (j = 0; j < count; j++)
                            {
                                if (map[j] == input[0])
                                {
                                    break;
                                }
                            }
                        }

                        if (j < count)
                        {
                            dir = (split.Subset[j >> 5] & (1 << (j & 31))) != 0 ? -1 : 1;
                            if (split.Inversed)
                            {
                                dir = -dir;
                            }
                            break;
                        }
                        else
                        {
                            Console.WriteLine("Error: unrecognized value");
                        }
                    }

                    if (dir == 0)
                    {
                        Console.WriteLine("Impossible to classify the sample");
                        node = null;
                        break;
                    }
                    node = dir < 0 ? node.Left : node.Right;
                }

                if (node != null)
                {
                    Console.Write("Prediction result: the mushroom is {0}\n", node.ClassIdx == 0 ? "EDIBLE" : "POISONOUS");
                }
                Console.Write("\n-----------------------------\n");
            }
        }