Ejemplo n.º 1
0
        public static NearestClassClassifier CreateNCClassifier(Dataset trainingSet, double distanceThreshold)
        {
            DefaultDistanceMeasure distanceMeasure = new DefaultDistanceMeasure(2);
            NearestClassClassifier NCClassifier    = new NearestClassClassifier(distanceMeasure, trainingSet, distanceThreshold);

            return(NCClassifier);
        }
Ejemplo n.º 2
0
        public static EnsembleClassifier CreateNCCAntIBMinerClassifier_Ensemble(Dataset trainingSet)
        {
            int classCount      = trainingSet.Metadata.Target.Values.Length;
            int attributesCount = trainingSet.Metadata.Attributes.Length;

            int problemSize = attributesCount + 1;

            AccuracyMeasure                  measure         = new AccuracyMeasure();
            DefaultDistanceMeasure           distanceMeasure = new DefaultDistanceMeasure(2);
            NearestClassClassifier           ncc             = new NearestClassClassifier(distanceMeasure, trainingSet);
            IBClassificationQualityEvaluator evaluator       = new ContinuousACO.ProblemSpecifics.IBClassificationQualityEvaluator(ncc, measure);

            evaluator.LearningSet   = trainingSet;
            evaluator.ValidationSet = trainingSet;
            Problem <double> problem = new Problem <double>(null, null, evaluator, null);

            AntIBMiner         antminer = new AntIBMiner(maxIterations, colonySize, convergenceIterations, problem, problemSize, archive, q, segma, trainingSet);
            EnsembleClassifier aconcc   = antminer.CreateEnsembleClassifier();

            return(aconcc);
        }
Ejemplo n.º 3
0
        public static EnsembleClassifier CreateNCCPSOIBMinerClassifier_Ensemble(Dataset trainingSet)
        {
            int classCount      = trainingSet.Metadata.Target.Values.Length;
            int attributesCount = trainingSet.Metadata.Attributes.Length;

            int problemSize = attributesCount + 1;

            AccuracyMeasure                  measure         = new AccuracyMeasure();
            DefaultDistanceMeasure           distanceMeasure = new DefaultDistanceMeasure(2);
            NearestClassClassifier           ncc             = new NearestClassClassifier(distanceMeasure, trainingSet);
            IBClassificationQualityEvaluator evaluator       = new ContinuousACO.ProblemSpecifics.IBClassificationQualityEvaluator(ncc, measure);

            evaluator.LearningSet   = trainingSet;
            evaluator.ValidationSet = trainingSet;

            PSOIB psoIB = new PSOIB(problemSize, archive, maxIterations / archive, convergenceIterations, evaluator);

            psoIB.OnPostSwarmIteration += OnPostColonyIteration;

            EnsembleClassifier psoncc = psoIB.CreateEnsembleClassifier();

            return(psoncc);
        }
        //private void LoadSimilarityCache()
        //{
        //    if (this._similarityCache == null)
        //        this._similarityCache = new double[this._IBClassifier.Database.Size, this._IBClassifier.Database.Size];

        //    //for(int i=0; i< this._IBClassifier.Metadata.Size; i++)
        //    //    for(int j=0; j< this._IBClassifier.Metadata.Size; j++)
        //    //        this._similarityCache[i,j]=this._IBClassifier.distanceMeasure.CalculateSimilarity(this._IBClassifier.Database[i],this._IBClassifier.Database[j]);
        //}

        #endregion

        public override void EvaluateSolutionQuality(Solution <double> solution)
        {
            int classCount      = this.LearningSet.Metadata.Target.Values.Length;
            int attributesCount = this.LearningSet.Metadata.Attributes.Length;

            if (this._IBClassifier is GaussianKernelEstimator)
            {
                double[][] classBasedWeights = new double[classCount][];
                double     kernelParameter   = solution.Components[0].Element;
                int        counter           = 1;

                if (solution.Components.Count > attributesCount + 1)
                {
                    for (int i = 0; i < classCount; i++)
                    {
                        classBasedWeights[i] = new double[attributesCount];
                        for (int j = 0; j < attributesCount; j++)
                        {
                            classBasedWeights[i][j] = solution.Components[counter].Element;
                            counter++;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < classCount; i++)
                    {
                        classBasedWeights[i] = new double[attributesCount];
                        for (int j = 0; j < attributesCount; j++)
                        {
                            classBasedWeights[i][j] = solution.Components[counter].Element;
                            counter++;
                        }
                        counter = 1;
                    }
                }

                GaussianKernelEstimator GKClassfier = this._IBClassifier as GaussianKernelEstimator;
                GKClassfier.KernelParameter = kernelParameter / 10;
                //GKClassfier.KernelParameter = 0;
                GKClassfier.SetWeights(classBasedWeights);
            }
            else if (this._IBClassifier is NearestClassClassifier)
            {
                double[][] classBasedWeights  = new double[classCount][];
                double     similarityTheshold = solution.Components[0].Element;
                int        counter            = 1;

                if (solution.Components.Count > attributesCount + 1)
                {
                    for (int i = 0; i < classCount; i++)
                    {
                        classBasedWeights[i] = new double[attributesCount];
                        for (int j = 0; j < attributesCount; j++)
                        {
                            classBasedWeights[i][j] = solution.Components[counter].Element;
                            counter++;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < classCount; i++)
                    {
                        classBasedWeights[i] = new double[attributesCount];
                        for (int j = 0; j < attributesCount; j++)
                        {
                            classBasedWeights[i][j] = solution.Components[counter].Element;
                            counter++;
                        }
                        counter = 1;
                    }
                }

                NearestClassClassifier NNClassfier = this._IBClassifier as NearestClassClassifier;
                NNClassfier.SimilarityThreshold = similarityTheshold;
                NNClassfier.SetWeights(classBasedWeights);
            }
            else if (this._IBClassifier is KNearestNeighbours)
            {
                double[][] classBasedWeights = new double[classCount][];
                int        k = ((int)(Math.Round((solution.Components[0].Element) * 20, 0))) + 1;
                //int k = 1;
                int counter = 1;

                if (solution.Components.Count > attributesCount + 1)
                {
                    for (int i = 0; i < classCount; i++)
                    {
                        classBasedWeights[i] = new double[attributesCount];
                        for (int j = 0; j < attributesCount; j++)
                        {
                            classBasedWeights[i][j] = solution.Components[counter].Element;
                            counter++;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < classCount; i++)
                    {
                        classBasedWeights[i] = new double[attributesCount];
                        for (int j = 0; j < attributesCount; j++)
                        {
                            classBasedWeights[i][j] = solution.Components[counter].Element;
                            counter++;
                        }
                        counter = 0;
                    }
                }

                KNearestNeighbours KNNClassifier = this._IBClassifier as KNearestNeighbours;
                KNNClassifier.KNeighbours = k;
                this._IBClassifier.SetWeights(classBasedWeights);
            }


            this._IBClassifier.Database = this.LearningSet;
            double quality = _measure.CalculateMeasure(this._IBClassifier, ValidationSet);

            solution.Quality = quality;
        }
Ejemplo n.º 5
0
        public static void RunACOIBL()
        {
            int             k = 9;
            AccuracyMeasure accuracyMeasure = new AccuracyMeasure();

            foreach (string dataset in GetDatasetFolds("datasets.txt"))
            {
                //----------------------------------------
                Console.WriteLine("Data Table:" + dataset);
                //----------------------------------------

                for (_currentFold = 0; _currentFold < _folds; _currentFold++)
                {
                    //----------------------------------------
                    //Console.WriteLine("Fold:" + _currentFold.ToString());
                    //----------------------------------------

                    DataMining.Data.Dataset[] tables      = LoadTrainingAndTestingData(dataset, _currentFold);
                    DataMining.Data.Dataset   trainingSet = tables[0];
                    DataMining.Data.Dataset   testingSet  = tables[1];

                    Dataset datasetFull = Dataset.Merge(trainingSet, testingSet);

                    double quality = 0;

                    try
                    {
                        {
                            KNearestNeighbours knn = SingleTest.CreateKNNClassifier(k, datasetFull, false);
                            quality = SingleTest.TestClassifier(knn, datasetFull, accuracyMeasure);
                            quality = Math.Round(quality * 100, 2);
                            //------------------------------------------------------------------
                            Console.WriteLine("KNN: " + dataset + " - Accuracy=" + quality);
                            SaveResults(dataset, "KNN", k.ToString(), quality.ToString());
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                        }

                        {
                            KNearestNeighbours knnWV = SingleTest.CreateKNNClassifier(k, datasetFull, true);
                            quality = SingleTest.TestClassifier(knnWV, datasetFull, accuracyMeasure);
                            quality = Math.Round(quality * 100, 2);
                            //------------------------------------------------------------------
                            Console.WriteLine("KNN-WV: " + dataset + " - Accuracy=" + quality);
                            SaveResults(dataset, "KNN-WV", k.ToString(), quality.ToString());
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                        }

                        {
                            NearestClassClassifier ncc = SingleTest.CreateNCClassifier(datasetFull);
                            quality = SingleTest.TestClassifier(ncc, datasetFull, accuracyMeasure);
                            quality = Math.Round(quality * 100, 2);
                            //------------------------------------------------------------------
                            Console.WriteLine("NNC: " + dataset + " - Accuracy=" + quality);
                            SaveResults(dataset, "NNC", quality.ToString());
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                        }

                        {
                            KNearestNeighbours knn = SingleTest.CreateKNNAntIBMinerClassifier(k, datasetFull, false);
                            quality = SingleTest.TestClassifier(knn, datasetFull, accuracyMeasure);
                            quality = Math.Round(quality * 100, 2);
                            //------------------------------------------------------------------
                            Console.WriteLine("ACO-KNN: " + dataset + " - Accuracy=" + quality);
                            SaveResults(dataset, "ACO-KNN", k.ToString(), quality.ToString());
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                        }

                        {
                            KNearestNeighbours knn = SingleTest.CreateKNNAntIBMinerClassifier(k, datasetFull, true);
                            quality = SingleTest.TestClassifier(knn, datasetFull, accuracyMeasure);
                            quality = Math.Round(quality * 100, 2);
                            //------------------------------------------------------------------
                            Console.WriteLine("ACO-KNN-WV: " + dataset + " - Accuracy=" + quality);
                            SaveResults(dataset, "ACO-KNN-WV", k.ToString(), quality.ToString());
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                        }

                        {
                            KNearestNeighbours knn = SingleTest.CreateKNNAntIBMinerClassifier_ClassBasedWeights(k, datasetFull, false);
                            quality = SingleTest.TestClassifier(knn, datasetFull, accuracyMeasure);
                            quality = Math.Round(quality * 100, 2);
                            //------------------------------------------------------------------
                            Console.WriteLine("ACO-KNN-CB: " + dataset + " - Accuracy=" + quality);
                            SaveResults(dataset, "ACO-KNN-CB", k.ToString(), quality.ToString());
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                        }

                        {
                            KNearestNeighbours knn = SingleTest.CreateKNNAntIBMinerClassifier_ClassBasedWeights(k, datasetFull, true);
                            quality = SingleTest.TestClassifier(knn, datasetFull, accuracyMeasure);
                            quality = Math.Round(quality * 100, 2);
                            //------------------------------------------------------------------
                            Console.WriteLine("ACO-KNN-CB-WV: " + dataset + " - Accuracy=" + quality);
                            SaveResults(dataset, "ACO-KNN-CB-WV", k.ToString(), quality.ToString());
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                        }

                        {
                            NearestClassClassifier ncc = SingleTest.CreateNCCAntIBMinerClassifier(datasetFull);
                            quality = SingleTest.TestClassifier(ncc, datasetFull, accuracyMeasure);
                            quality = Math.Round(quality * 100, 2);
                            //------------------------------------------------------------------
                            Console.WriteLine("ACO-NCC: " + dataset + " - Accuracy=" + quality);
                            SaveResults(dataset, "ACO-NCC", quality.ToString());
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                        }

                        {
                            NearestClassClassifier ncc = SingleTest.CreateNCCAntIBMinerClassifier_ClassBasedWeights(datasetFull);
                            quality = SingleTest.TestClassifier(ncc, datasetFull, accuracyMeasure);
                            quality = Math.Round(quality * 100, 2);
                            //------------------------------------------------------------------
                            Console.WriteLine("ACO-NCC-CB: " + dataset + " - Accuracy=" + quality);
                            SaveResults(dataset, "ACO-NCC-CB", quality.ToString());
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                        }

                        {
                            GaussianKernelEstimator GKC = SingleTest.CreateGKAntIBMinerClassifier(datasetFull);
                            quality = SingleTest.TestClassifier(GKC, datasetFull, accuracyMeasure);
                            quality = Math.Round(quality * 100, 2);
                            //------------------------------------------------------------------
                            Console.WriteLine("ACO-GKC: " + dataset + " - Accuracy=" + quality);
                            SaveResults(dataset, "ACO-GKC", quality.ToString());
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                        }

                        {
                            GaussianKernelEstimator GKC = SingleTest.CreateGKAntIBMinerClassifier_ClassBaseWeights(datasetFull);
                            quality = SingleTest.TestClassifier(GKC, datasetFull, accuracyMeasure);
                            quality = Math.Round(quality * 100, 2);
                            //------------------------------------------------------------------
                            Console.WriteLine("ACO-GKC-CB: " + dataset + " - Accuracy=" + quality);
                            SaveResults(dataset, "ACO-GKC-CB", quality.ToString());
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("-------------------------------------------");
                        }
                    }
                    catch (Exception ex)
                    {
                        LogError(ex);
                        //  Console.WriteLine(ex.Message);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public static void RunConventional()
        {
            AccuracyMeasure accuracyMeasure = new AccuracyMeasure();

            foreach (string dataset in GetDatasetFolds(DatasetNamesFile))
            {
                //----------------------------------------
                Console.WriteLine("Data Table:" + dataset);
                //----------------------------------------

                //try
                {
                    double quality1 = 0;
                    double quality2 = 0;
                    double quality3 = 0;

                    double quality4 = 0;
                    double quality5 = 0;
                    double quality6 = 0;

                    double quality7 = 0;
                    double quality8 = 0;
                    double quality9 = 0;


                    for (_currentFold = 0; _currentFold < _folds; _currentFold++)
                    {
                        //----------------------------------------
                        //Console.WriteLine("Fold:" + _currentFold.ToString());
                        //----------------------------------------

                        DataMining.Data.Dataset[] tables      = LoadTrainingAndTestingData(dataset, _currentFold);
                        DataMining.Data.Dataset   trainingSet = tables[0];
                        DataMining.Data.Dataset   testingSet  = tables[1];



                        KNearestNeighbours knn1 = SingleTest.CreateKNNClassifier(1, trainingSet, false);
                        quality1 += SingleTest.TestClassifier(knn1, testingSet, accuracyMeasure);
                        //------------------------------------------------------------------

                        KNearestNeighbours knn11 = SingleTest.CreateKNNClassifier(11, trainingSet, false);
                        quality2 += SingleTest.TestClassifier(knn11, testingSet, accuracyMeasure);
                        //------------------------------------------------------------------

                        KNearestNeighbours knn21 = SingleTest.CreateKNNClassifier(21, trainingSet, false);
                        quality3 += SingleTest.TestClassifier(knn21, testingSet, accuracyMeasure);
                        //------------------------------------------------------------------

                        //------------------------------------------------------------------
                        //------------------------------------------------------------------

                        NearestClassClassifier ncc0 = SingleTest.CreateNCClassifier(trainingSet, 0);
                        quality4 += SingleTest.TestClassifier(ncc0, testingSet, accuracyMeasure);
                        //------------------------------------------------------------------

                        NearestClassClassifier ncc5 = SingleTest.CreateNCClassifier(trainingSet, 0.5);
                        quality5 += SingleTest.TestClassifier(ncc5, testingSet, accuracyMeasure);
                        //------------------------------------------------------------------

                        NearestClassClassifier ncc1 = SingleTest.CreateNCClassifier(trainingSet, 0.9);
                        quality6 += SingleTest.TestClassifier(ncc1, testingSet, accuracyMeasure);
                        ////------------------------------------------------------------------

                        ////------------------------------------------------------------------
                        ////------------------------------------------------------------------

                        GaussianKernelEstimator gcc0 = SingleTest.CreateGKClassifier(trainingSet, 0);
                        quality7 += SingleTest.TestClassifier(gcc0, testingSet, accuracyMeasure);
                        //------------------------------------------------------------------

                        GaussianKernelEstimator gcc5 = SingleTest.CreateGKClassifier(trainingSet, 0.25);
                        quality8 += SingleTest.TestClassifier(gcc5, testingSet, accuracyMeasure);
                        //------------------------------------------------------------------

                        GaussianKernelEstimator gcc1 = SingleTest.CreateGKClassifier(trainingSet, 0.5);
                        quality9 += SingleTest.TestClassifier(gcc1, testingSet, accuracyMeasure);
                    }

                    quality1 = Math.Round((quality1 / _folds) * 100, 2);
                    quality2 = Math.Round((quality2 / _folds) * 100, 2);
                    quality3 = Math.Round((quality3 / _folds) * 100, 2);

                    quality4 = Math.Round((quality4 / _folds) * 100, 2);
                    quality5 = Math.Round((quality5 / _folds) * 100, 2);
                    quality6 = Math.Round((quality6 / _folds) * 100, 2);

                    quality7 = Math.Round((quality7 / _folds) * 100, 2);
                    quality8 = Math.Round((quality8 / _folds) * 100, 2);
                    quality9 = Math.Round((quality9 / _folds) * 100, 2);

                    Console.WriteLine("1NN: " + dataset + " - Accuracy=" + quality1);
                    SaveResults(dataset, "1NN", quality1.ToString());

                    Console.WriteLine("11NN: " + dataset + " - Accuracy=" + quality2);
                    SaveResults(dataset, "11NN", quality2.ToString());

                    Console.WriteLine("21NN: " + dataset + " - Accuracy=" + quality3);
                    SaveResults(dataset, "21NN", quality3.ToString());


                    Console.WriteLine("NCC-0: " + dataset + " - Accuracy=" + quality4);
                    SaveResults(dataset, "NCC-0", quality4.ToString());

                    Console.WriteLine("NCC-0.5: " + dataset + " - Accuracy=" + quality5);
                    SaveResults(dataset, "NCC-0.5", quality5.ToString());

                    Console.WriteLine("NCC-1: " + dataset + " - Accuracy=" + quality6);
                    SaveResults(dataset, "NCC-1", quality6.ToString());


                    Console.WriteLine("GKE-0: " + dataset + " - Accuracy=" + quality7);
                    SaveResults(dataset, "GKE-0", quality7.ToString());

                    Console.WriteLine("GKE-0.25: " + dataset + " - Accuracy=" + quality8);
                    SaveResults(dataset, "GKE-0.25", quality8.ToString());

                    Console.WriteLine("GKE-0.5: " + dataset + " - Accuracy=" + quality9);
                    SaveResults(dataset, "GKE-0.5", quality9.ToString());

                    Console.WriteLine("-------------------------------------------");
                    Console.WriteLine("-------------------------------------------");
                    Console.WriteLine("-------------------------------------------");
                }
                //catch (Exception ex)
                {
                    //LogError(ex);
                    //  Console.WriteLine(ex.Message);
                }
            }
        }