Esempio n. 1
0
        static void classify(TextWriter writer, char csv_seperator, char dec_seperator)
        {
            ModelLoader    loader         = new ModelLoader();
            DataController datacontroller = new DataController(csv_seperator, dec_seperator);


            // Check if the data is already classified, and thus we are in verification mode and will check how our model holds up.
            string verification_mode_string = writer.askFromConfig("Verification mode? (TRUE or FALSE) ", "CLASSIFICATION", "verification-mode");
            bool   verification_mode        = (verification_mode_string == "TRUE");


            // Import the classification data.
            string         data_location   = writer.askFromConfig("Enter the file path to import the data from. ", "CLASSIFICATION", "input-location");
            ObservationSet observations    = verification_mode ? datacontroller.importExamples(data_location) : datacontroller.importUnclassified(data_location);
            string         classifier_name = observations.target_attribute;

            // Import the model
            string       model_location = writer.askFromConfig("Enter the file path to import the model from. ", "CLASSIFICATION", "model-location");
            DecisionTree model          = loader.load_model(model_location, classifier_name);

            // Get ready for classification.
            string export_location = writer.askFromConfig("Enter the file path to export data to. ", "CLASSIFICATION", "output-location");

            Console.WriteLine($"READY for classification{(verification_mode ? " in verification mode" : "")}. Press a key to start classification process \n");
            Console.ReadKey(true);

            List <DataInstance> classified_instances = new List <DataInstance>();
            int correct_classifications = 0;

            foreach (DataInstance instance in observations.instances)
            {
                string prediction = model.classify(instance);
                if (verification_mode)
                {
                    if (instance.getProperty(classifier_name) == prediction)
                    {
                        correct_classifications++;
                    }
                }
                else
                {
                    instance.setProperty(classifier_name, model.classify(instance));
                }
                classified_instances.Add(instance);
            }
            ObservationSet export_set = new ObservationSet(classified_instances, classifier_name, observations.attributes);

            Console.WriteLine("Classification succesful, saving now.");

            if (verification_mode)
            {
                double succesPercentage = Math.Round(((double)correct_classifications / (double)classified_instances.Count) * 100, 2);
                Console.WriteLine($"Verification mode results: success rate of {succesPercentage}% ({correct_classifications} / {classified_instances.Count}).");
            }
            datacontroller.exportSet(export_location, export_set);
        }
        public void Make(string name, DecisionTree tree)
        {
            long beforeSnapShot = this.stopwatch.ElapsedMilliseconds;

            // Save model
            writer.set_location(location);
            writer.filesave_lines(name + "." + this.model_extension, ModelManager.generate_model(tree));

            // Save model as rule set
            writer.filesave_lines(name + "." + this.rules_extension, ModelManager.generate_rules(tree));

            // Save image
            writer.filesave_lines(name + "." + this.drawing_extension, drawer.lines(tree));

            // Lets see how well it classifies.
            List <DataInstance> total_set = new List <DataInstance>();

            foreach (Leaf leaf in tree.data_locations.Keys.ToList())
            {
                total_set.AddRange(tree.data_locations[leaf]);
            }
            int correct_classifications = 0;

            foreach (DataInstance instance in total_set)
            {
                if (tree.classify(instance) == instance.getProperty(tree.target_attribute))
                {
                    correct_classifications++;
                }
            }
            double succesPercentage = Math.Round(((double)correct_classifications / (double)total_set.Count) * 100, 2);

            Console.WriteLine($"[SNAPSHOT] : Snapshot {name} made with success rate of {succesPercentage}% ({correct_classifications} / {total_set.Count}).");
            this.secondsBySnapShot += (this.stopwatch.ElapsedMilliseconds - beforeSnapShot);
        }