private List <ClassifierInfo> CreateEnsemble(Node current)
        {
            List <ClassifierInfo> ensemble = new List <ClassifierInfo>();

            foreach (Dataset currentTrainingSet in this._trainingSets)
            {
                Dataset local = GetLocalFlatDataset(current, currentTrainingSet);

                _algorithm.Dataset = local;
                IClassifier    currentClassifier = _algorithm.CreateClassifier();
                double         currentQyuality   = _evaluator.CalculateMeasure(ConfusionMatrix.GetConfusionMatrixes(currentClassifier, local));
                ClassifierInfo info = new ClassifierInfo()
                {
                    Classifier = currentClassifier, Desc = current.Name + ":" + currentTrainingSet.Metadata.DatasetName, Quality = currentQyuality
                };

                if (_fireEvents)
                {
                    ClassifierInfoEventArgs args = new ClassifierInfoEventArgs()
                    {
                        Info = info
                    };
                    this.onPostClassifierConstruction(current, args);
                }

                ensemble.Add(info);
            }

            return(ensemble);
        }
Ejemplo n.º 2
0
        public void PutClassifier(string label, ClassifierInfo info)
        {
            if (!this._modelMapping.Keys.Contains(label))
            {
                this._modelMapping.Add(label, new List <ClassifierInfo>());
            }

            this._modelMapping[label].Add(info);
        }
        public int Classify(List <ClassifierInfo> classifiersInfo, Data.Example example)
        {
            ClassifierInfo best = classifiersInfo[0];

            foreach (ClassifierInfo current in classifiersInfo)
            {
                if (current.Quality > best.Quality)
                {
                    best = current;
                }
            }

            return(best.Classifier.Classify(example).Label);
        }
        public int Classify(List <ClassifierInfo> classifiersInfo, List <Data.Example> examples)
        {
            ClassifierInfo best = classifiersInfo[0];

            foreach (ClassifierInfo current in classifiersInfo)
            {
                if (current.Quality > best.Quality)
                {
                    best = current;
                }
            }

            Data.Example bestExample = examples.Find(e => best.Desc.Contains(e.Metadata.DatasetName));
            return(best.Classifier.Classify(bestExample).Label);
        }
Ejemplo n.º 5
0
        public static ClassifierInfo DeserializeClassifier(string desc)
        {
            ClassifierInfo info = null;
            string         path = @"temp\" + desc;

            using (StreamReader reader = new StreamReader(path))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                info = formatter.Deserialize(reader.BaseStream) as ClassifierInfo;
                reader.Close();

                File.Delete(path);
            }

            return(info);
        }
        public static IClassifier CreateMetaClassifier(IClassificationAlgorithm algorithm, List <Dataset> trainingsets, List <ClassifierInfo> ensemble)
        {
            List <Data.Attribute> metaAttributes = new List <Data.Attribute>();

            for (int i = 0; i < ensemble.Count; i++)
            {
                metaAttributes.Add(new Data.Attribute(ensemble[i].Desc, i, new string[] { "Yes", "No" }));
            }

            Data.Attribute metaClass = new Data.Attribute("Class", ensemble.Count, new string[] { "Yes", "No" });

            Metadata metadata = new Metadata("metaClassifier", metaAttributes.ToArray(), metaClass);

            List <Example> metaExamples = new List <Example>();

            int counter = 0;

            foreach (Example example in trainingsets[0])
            {
                List <int> predictions = new List <int>();

                for (int infoIndex = 0; infoIndex < ensemble.Count; infoIndex++)
                {
                    ClassifierInfo info       = ensemble[infoIndex];
                    int            predictoin = info.Classifier.Classify(trainingsets[infoIndex][example.Index]).Label;
                    predictions.Add(predictoin);
                }


                Example metaExample = new Example(metadata, counter++, predictions.ToArray(), trainingsets[0][example.Index].Label);
                metaExamples.Add(metaExample);
            }



            Dataset metaDataset = new Dataset(metadata);

            metaDataset.SetExamples(metaExamples.ToArray());

            algorithm.Dataset = metaDataset;
            IClassifier classifier = algorithm.CreateClassifier();

            return(classifier);
        }
Ejemplo n.º 7
0
        public static void SerializeClassifier(ClassifierInfo info)
        {
            if (!Directory.Exists("temp"))
            {
                System.IO.Directory.CreateDirectory("temp");
            }

            string path = @"temp\" + info.Desc;

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            using (StreamWriter writer = new StreamWriter(@"temp\" + info.Desc))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(writer.BaseStream, info);
            }
        }
        public int Classify(List <ClassifierInfo> classifiersInfo, List <Data.Example> examples)
        {
            IClassifier metaClassifier   = classifiersInfo.Last().Classifier;
            List <int>  predictionValues = new List <int>();

            for (int i = 0; i < classifiersInfo.Count - 1; i++)
            {
                ClassifierInfo info    = classifiersInfo[i];
                Data.Example   example = examples.Find(e => info.Desc.Contains(e.Metadata.DatasetName));

                int prediction = info.Classifier.Classify(example).Label;
                predictionValues.Add(prediction);
            }

            Data.Example metaExample = new Data.Example(null, -1, predictionValues.ToArray(), -1);

            int finalPredction = metaClassifier.Classify(metaExample).Label;

            return(finalPredction);
        }
        public Model.Hierarchical.IHierarchicalClassifier CreateClassifier()
        {
            ClassHierarchy         hierarchy      = ((Data.HierarchicalAttribute) this._trainingSets[0].Metadata.Target).Hierarchy;
            LocalPerNodeClassifier localClassifer = new LocalPerNodeClassifier(hierarchy, _ensembleStrategy);

            Node         current = hierarchy.Root;
            Queue <Node> queue   = new Queue <Node>();

            foreach (Node node in hierarchy.GetNodes(current.Children))
            {
                queue.Enqueue(node);
            }

            int counter = 1;

            while (queue.Count != 0)
            {
                current = queue.Dequeue();
                if (localClassifer.Contains(current.Name))
                {
                    continue;
                }


                List <ClassifierInfo> ensemble = CreateEnsemble(current);

                if (_ensembleStrategy is EnsembleStrategy.EnsembleStackClassificationStrategy)
                {
                    List <Dataset> localDatasets = new List <Dataset>();
                    foreach (Dataset trainingSet in this._trainingSets)
                    {
                        localDatasets.Add(GetLocalFlatDataset(current, trainingSet));
                    }

                    IClassifier    metaClassifier = EnsembleStrategy.EnsembleStackClassificationStrategy.CreateMetaClassifier(this._algorithm, localDatasets, ensemble);
                    ClassifierInfo metaInfo       = new ClassifierInfo()
                    {
                        Desc = "metaClassifier", Classifier = metaClassifier, Quality = -1
                    };
                    ensemble.Add(metaInfo);
                }

                if (_serialize)
                {
                    IO.Serializer.SerializeEnsmeble(ensemble, current.Name);
                    localClassifer.PutEnsemble(current.Name, null);
                }
                else
                {
                    localClassifer.PutEnsemble(current.Name, ensemble);
                }


                if (_fireEvents)
                {
                    this.onPostNodeProcessing(current, new NodeCounterEventArgs()
                    {
                        Counter = counter
                    });
                }

                counter++;

                if (current.Children != null)
                {
                    foreach (string child in current.Children)
                    {
                        if (!queue.Contains(hierarchy[child]))
                        {
                            queue.Enqueue(hierarchy[child]);
                        }
                    }
                }
            }

            if (_serialize)
            {
                foreach (Node node in hierarchy.GetDescendants(hierarchy.Root))
                {
                    localClassifer.PutEnsemble(node.Name, IO.Serializer.DeserializeEnsemble(node.Name));
                }
            }


            return(localClassifer);
        }