Example #1
0
        private void doNearestKNeighboursAlgorithmTest()
        {
            var metric = new EuclideanMetric();
            var alg    = new NearestKNeighboursAlgorithm(Data.TrainingSample, metric, 1);

            // LOO
            alg.Train_LOO();
            var optK = alg.K;

            Console.WriteLine("Nearest K Neigbour: optimal k is {0}", optK);
            Console.WriteLine();

            // Margins
            Console.WriteLine("Margins:");
            calculateMargin(alg);
            Console.WriteLine();

            //Error distribution
            Console.WriteLine("Errors:");
            for (int k = 1; k < 5; k++)
            {
                alg.K = k;
                var errors = alg.GetErrors(Data.Data);
                var ec     = errors.Count();
                var dc     = Data.Data.Count;
                var pct    = Math.Round(100.0F * ec / dc, 2);
                Console.WriteLine("{0}:\t{1} of {2}\t({3}%) {4}", k, ec, dc, pct, k == optK ? "<-LOO optimal" : string.Empty);
            }
            Console.WriteLine();

            Visualizer.Run(alg);
        }
Example #2
0
        static void RunProgram()
        {
            //Config
            string codeName                = "Out-Cikade";
            bool   printKMeans             = true;
            bool   printKMeansMembers      = false;
            bool   distanceMatrix          = true;
            bool   multiDimensionalScaling = true;
            bool   localOutlierFactor      = true;

            IConfiguration configuration =
                (ConfigurationSectionHandler)ConfigurationManager.GetSection("csvDataImport");
            var reader         = new StreamReader("UserData.csv");
            var importer       = new CsvDataImporter(reader, configuration);
            var dataCollection = importer.Run();

            //Algo//
            //KMeans
            var k = 5;
            var distanceMetric = new EuclideanMetric();

            var kmeans = new KMeans(dataCollection, k, distanceMetric);

            var start  = DateTime.Now;
            var result = kmeans.Calculate();
            //Other alogs


            //File printing//
            var fileName = string.Format("{0}-{1:MM-dd_HH_mm_ss}.txt", codeName, DateTime.UtcNow);

            FileStream   fs     = new FileStream(fileName, FileMode.CreateNew);
            StreamWriter writer = new StreamWriter(fs);

            writer.Write("Testrun under CodeName {0}. \r\n\r\n", codeName, DateTime.UtcNow);

            if (printKMeans)
            {
                PrintKMeans(writer, result, printKMeansMembers);
            }

            if (distanceMatrix)
            {
                DistanceMatrix(writer, dataCollection, distanceMetric);
            }

            if (multiDimensionalScaling)
            {
                MultiDimensionalScaling(writer, dataCollection, distanceMetric);
            }

            if (localOutlierFactor)
            {
                LocalOutlierFactorD(4, dataCollection, distanceMetric);
            }

            writer.Close();
            fs.Close();
        }
Example #3
0
        public void EuclideanMetric_Dist_Validation()
        {
            var metric = new EuclideanMetric();
            var p1     = new double[] { 1.0F, 0.0F };
            var p2     = new double[] { 0.0F, 1.0F, 3.0F };

            metric.Dist(p1, p2);
        }
Example #4
0
        private void doBayesianParzenAlgorithmTest()
        {
            var metric = new EuclideanMetric();
            var kernel = new GaussianKernel();
            var alg    = new BayesianParzenAlgorithm(metric, kernel, 1.0F);

            alg.Train(Data.TrainingSample);

            // LOO
            var hmin = 0.01D;
            var hmax = 5.0D;
            var step = 0.05D;

            StatUtils.OptimizeLOO(alg, hmin, hmax, step);
            var optH = alg.H;

            Console.WriteLine("Bayesian: optimal h is {0}", optH);
            Console.WriteLine();

            // Margins
            Console.WriteLine("Margins:");
            calculateMargin(alg);
            Console.WriteLine();

            //Error distribution
            var message = string.Empty;

            Console.WriteLine("Errors:");
            for (double h1 = hmin; h1 <= hmax; h1 = Math.Round(h1 + step, 8))
            {
                var h = h1;
                if (h <= optH && h + step > optH)
                {
                    h = optH;
                }

                alg.H = h;
                var errors = alg.GetErrors(Data.Data, 0, true);
                var ec     = errors.Count();
                var dc     = Data.Data.Count;
                var pct    = Math.Round(100.0F * ec / dc, 2);
                var mes    = string.Format("{0}:\t{1} of {2}\t({3}%) {4}", Math.Round(h, 2), ec, dc, pct, h == optH ? "<-LOO optimal" : string.Empty);
                Console.WriteLine(mes);

                if (h == optH)
                {
                    message = mes;
                }
            }
            Console.WriteLine();
            Console.WriteLine("-----------------------------------------");
            Console.WriteLine("Bayesian: optimal h is {0}", optH);
            Console.WriteLine(message);

            alg.H = optH;
            Visualizer.Run(alg);
        }
Example #5
0
 /**
  * Class constructor.
  *
  * @param inputSize
  *            the number of inputs to the map.
  * @param outputDimensions
  *            the number of and size of output dimensions.
  */
 public MapBaseImpl(int inputSize, params int[] outputDimensions)
 {
     inputMetric         = new EuclideanMetric();
     outputMetric        = new EuclideanMetric();
     nhFunction          = new GaussianNeighbourhoodFunction();
     this.inputDimension = inputSize;
     this.weights        = new IterativeArray <double[]>(outputDimensions);
     this.random         = new Random();
     this.initWeights();
 }
Example #6
0
        private void doParzenFixedAlgorithmTest()
        {
            var timer = new System.Diagnostics.Stopwatch();

            timer.Start();

            var metric = new EuclideanMetric();
            var kernel = new GaussianKernel();
            var alg    = new ParzenFixedAlgorithm(metric, kernel, 1.0F);

            alg.Train(Data.TrainingSample);

            // LOO
            StatUtils.OptimizeLOO(alg, 0.1F, 20.0F, 0.2F);
            var optH = alg.H;

            Console.WriteLine("Parzen Fixed: optimal h is {0}", optH);
            Console.WriteLine();

            // Margins
            Console.WriteLine("Margins:");
            calculateMargin(alg);
            Console.WriteLine();

            //var x = algorithm.Classify(new Point(new double[] { -3, 0 }));

            //Error distribution
            Console.WriteLine("Errors:");
            var step = 0.1F;

            for (double h1 = step; h1 < 5; h1 += step)
            {
                var h = h1;
                if (h <= optH && h + step > optH)
                {
                    h = optH;
                }

                alg.H = h;
                var errors = alg.GetErrors(Data.Data, 0, true);
                var ec     = errors.Count();
                var dc     = Data.Data.Count;
                var pct    = Math.Round(100.0F * ec / dc, 2);
                Console.WriteLine("{0}:\t{1} of {2}\t({3}%) {4}", Math.Round(h, 2), ec, dc, pct, h == optH ? "<-LOO optimal" : string.Empty);
            }
            Console.WriteLine();

            Visualizer.Run(alg);

            timer.Stop();
            Console.WriteLine(timer.ElapsedMilliseconds / 1000.0F);
        }
Example #7
0
        static void Main(string[] args)
        {
            //double trainingToTestDataRatio = 0.6;
            int  stopListWordNumber = 100;
            bool IdfOn = true;

            LoadAllDocumentArticles(new DocumentReader());
            LoadAllCustomArticles(new CustomReader());

            IMetric metric = new EuclideanMetric();

            Console.WriteLine(metric.GetType().Name);
            RunFor3Sets(0.2, stopListWordNumber, metric, IdfOn);
            RunFor3Sets(0.6, stopListWordNumber, metric, IdfOn);
            RunFor3Sets(0.8, stopListWordNumber, metric, IdfOn);

            metric = new ChebyshevMetric();
            Console.WriteLine(metric.GetType().Name);
            RunFor3Sets(0.2, stopListWordNumber, metric, IdfOn);
            RunFor3Sets(0.6, stopListWordNumber, metric, IdfOn);
            RunFor3Sets(0.8, stopListWordNumber, metric, IdfOn);

            metric = new ManhattanMetric();
            Console.WriteLine(metric.GetType().Name);
            RunFor3Sets(0.2, stopListWordNumber, metric, IdfOn);
            RunFor3Sets(0.6, stopListWordNumber, metric, IdfOn);
            RunFor3Sets(0.8, stopListWordNumber, metric, IdfOn);


            IdfOn  = false;
            metric = new EuclideanMetric();
            Console.WriteLine(metric.GetType().Name);
            RunFor3Sets(0.2, stopListWordNumber, metric, IdfOn);
            RunFor3Sets(0.6, stopListWordNumber, metric, IdfOn);
            RunFor3Sets(0.8, stopListWordNumber, metric, IdfOn);

            metric = new ChebyshevMetric();
            Console.WriteLine(metric.GetType().Name);
            RunFor3Sets(0.2, stopListWordNumber, metric, IdfOn);
            RunFor3Sets(0.6, stopListWordNumber, metric, IdfOn);
            RunFor3Sets(0.8, stopListWordNumber, metric, IdfOn);

            metric = new ManhattanMetric();
            Console.WriteLine(metric.GetType().Name);
            RunFor3Sets(0.2, stopListWordNumber, metric, IdfOn);
            RunFor3Sets(0.6, stopListWordNumber, metric, IdfOn);
            RunFor3Sets(0.8, stopListWordNumber, metric, IdfOn);

            Console.ReadKey();
        }
        public ClusterSystem(List <Cluster> clusters)
        {
            Clusters = new Dictionary <int, Cluster>();
            foreach (var cluster in clusters)
            {
                Clusters.Add(cluster.Id, cluster);
            }

            Distances = new Dictionary <string, double>();
            for (int i = 0; i < clusters.Count; i++)
            {
                for (int j = i + 1; j < clusters.Count; j++)
                {
                    Distances.Add(i + "to" + j, EuclideanMetric.GetDistance(clusters[i].Objects[0], clusters[j].Objects[0]));
                }
            }
        }
Example #9
0
        private void doNearestNeighbourAlgorithmTest()
        {
            var metric = new EuclideanMetric();
            var alg    = new NearestNeighbourAlgorithm(Data.TrainingSample, metric);

            Console.WriteLine("Margin:");
            calculateMargin(alg);

            Console.WriteLine("Errors:");
            var errors = alg.GetErrors(Data.Data);
            var ec     = errors.Count();
            var dc     = Data.Data.Count;
            var pct    = Math.Round(100.0F * ec / dc, 2);

            Console.WriteLine("{0} of {1} ({2}%)", ec, dc, pct);

            Visualizer.Run(alg);
        }
Example #10
0
        private void doPotentialFixedAlgorithmTest()
        {
            var metric = new EuclideanMetric();
            var kernel = new GaussianKernel();

            var eqps = new PotentialFunctionAlgorithm.KernelEquipment[Data.TrainingSample.Count];

            for (int i = 0; i < Data.TrainingSample.Count; i++)
            {
                eqps[i] = new PotentialFunctionAlgorithm.KernelEquipment(1.0F, 1.5F);
            }
            var alg = new PotentialFunctionAlgorithm(Data.TrainingSample, metric, kernel, eqps);

            Console.WriteLine("Margin:");
            calculateMargin(alg);

            outputError(alg);

            Visualizer.Run(alg);
        }
Example #11
0
        public async void ChooseExtract(string[] path)
        {
            Reuters = await Model.Reuter.GetReutersFromFileAsync(path, ChosenExtractFeature);

            AllReuters = TrainingPatterns.SetTrainingAndTestSet(TrainingSetPercent, Reuters);

            if (ChosenMetricFeature.Equals("Euclidean Metric"))
            {
                _percent = await EuclideanMetric.CalculateAsync(AllReuters, getK);

                Percent = (Math.Round(_percent, 2) * 100).ToString();
                MessageBox.Show("Done");
            }
            else if (ChosenMetricFeature.Equals("Manhattan Metric"))
            {
                _percent = await ManhattanMetric.CalculateAsync(AllReuters, getK);

                Percent = (Math.Round(_percent, 2) * 100).ToString();
                MessageBox.Show("Done");
            }
        }
Example #12
0
        public void EuclideanMetric_Dist2_Test()
        {
            var metric = new EuclideanMetric();
            var p1     = new double[] { 1.0F, 0.0F };
            var p2     = new double[] { 0.0F, 1.0F };
            var p3     = new double[] { 1.0F, 3.0F };

            var d12 = metric.Dist2(p1, p2);
            var d21 = metric.Dist2(p2, p1);
            var d13 = metric.Dist2(p1, p3);
            var d31 = metric.Dist2(p3, p1);
            var d23 = metric.Dist2(p2, p3);
            var d32 = metric.Dist2(p3, p2);

            Assert.AreEqual(2.0F, d12, EPS);
            Assert.AreEqual(2.0F, d21, EPS);
            Assert.AreEqual(9.0F, d13, EPS);
            Assert.AreEqual(9.0F, d31, EPS);
            Assert.AreEqual(5.0F, d32, EPS);
            Assert.AreEqual(5.0F, d23, EPS);
        }
Example #13
0
 public void SetUp()
 {
     metric = new EuclideanMetric();
 }