示例#1
0
        public void Run()
        {
            // The k-Nearest Neighbors algorithm can be used with
            // any kind of data. In this example, we will see how
            // it can be used to compare, for example, Strings.

            string[] inputs =
            {
            "Car",    // class 0
            "Bar",    // class 0
            "Jar",    // class 0

            "Charm",  // class 1
            "Chair"   // class 1
            };

            int[] outputs =
            {
            0, 0, 0,  // First three are from class 0
            1, 1,     // And next two are from class 1
            };

            // Now we will create the K-Nearest Neighbors algorithm. For this
            // example, we will be choosing k = 1. This means that, for a given
            // instance, only its nearest neighbor will be used to cast a new
            // decision.

            // In order to compare strings, we will be using Levenshtein's string distance
            KNearestNeighbors<string> knn = new KNearestNeighbors<string>(k: 1, classes: 2,
                inputs: inputs, outputs: outputs, distance: Distance.Levenshtein);

            // After the algorithm has been created, we can use it:
            int answer = knn.Compute("Chars"); // answer should be 1.
        }
示例#2
0
        /// <summary>
        /// Classify our data using k-nearest neighbors classifer and save the model.
        /// </summary>
        /// <param name="train_data">Frame objects that we will use to train classifers.</param>
        /// <param name="test_data">Frame objects that we will use to test classifers.</param>
        /// <param name="train_label">Labels of the train data.</param>
        /// <param name="test_label">Labels of the test data.</param>
        /// <param name="Classifier_Path">Path where we want to save the classifer on the disk.</param>
        /// <param name="Classifier_Name">Name of the classifer we wnat to save.</param>
        /// <returns></returns>
        public void Knn(double[][] train_data, double[][] test_data, int[] train_label, int[] test_label, String Classifier_Path, String Classifier_Name)
        {
            KNearestNeighbors knn = new KNearestNeighbors(k: 5);

            knn.Learn(train_data, train_label);

            int    answer = knn.Decide(new double[] { 117.07004523277283, 119.9104585647583 });
            var    cm     = GeneralConfusionMatrix.Estimate(knn, test_data, test_label);
            double error  = cm.Error;

            Console.WriteLine(error);

            knn.Save(Path.Combine(Classifier_Path, Classifier_Name));
        }
示例#3
0
        public string knn()
        {
            double[][] inputs =
            {
                // The first two are from class 0
                new double[] { 10 },

                // The next four are from class 1
                new double[] { 30 },

                // The last three are from class 2
                new double[] { 50 },
            };

            int[] outputs =
            {
                0,     // First two from class 0
                1,     // Next four from class 1
                2,     // Last three from class 2
            };


            // Now we will create the K-Nearest Neighbors algorithm. For this
            // example, we will be choosing k = 4. This means that, for a given
            // instance, its nearest 4 neighbors will be used to cast a decision.
            var knn = new KNearestNeighbors(k: 1);

            // We learn the algorithm:
            knn.Learn(inputs, outputs);
            //put a diffoult video
            if (!User.Identity.IsAuthenticated)
            {
                return("instegram.mp4");
            }
            int customerId = int.Parse(User.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.Sid).Value);
            int age        = _context.Customers.Where(a => a.ID == customerId).Select(a => a.BirthDate.Year).Single();
            // After the algorithm has been created, we can classify a new instance:
            int answer = knn.Decide(new double[] { (DateTime.Now.Year - age) }); // answer will be 2.

            if (answer == 0)
            {
                return("bracletRing.mp4");
            }
            if (answer == 1)
            {
                return("instegram.mp4");
            }
            return("bracletRing2.mp4");
        }
示例#4
0
        static void Dirbam()
        {
            DownloadTrainingAndTestingData();
            List <int[]> trainingData = ReadData("poker-hand-training-true.data");
            List <int[]> testingData  = ReadData("poker-hand-testing.data");

            double[]          precision    = new double[3];
            RandomForest      ranForest    = RandomForestClassification(trainingData, testingData, out precision[0]);
            DecisionTree      decisionTree = DecisionTreeClassification(trainingData, testingData, out precision[1]);
            KNearestNeighbors knn          = kNearestNeighbours(trainingData, testingData, out precision[2]);

            BestClassificator(ranForest, decisionTree, knn, precision);

            Console.ReadKey();
        }
        public void learn_test()
        {
            #region doc_learn_distance
            // Create some sample learning data. In this data,
            // the first two instances belong to a class, the
            // four next belong to another class and the last
            // three to yet another.

            double[][] inputs =
            {
                // The first two are from class 0
                new double[] { -5, -2, -1 },
                new double[] { -5, -5, -6 },

                // The next four are from class 1
                new double[] {  2,  1,  1 },
                new double[] {  1,  1,  2 },
                new double[] {  1,  2,  2 },
                new double[] {  3,  1,  2 },

                // The last three are from class 2
                new double[] { 11,  5,  4 },
                new double[] { 15,  5,  6 },
                new double[] { 10,  5,  6 },
            };

            int[] outputs =
            {
                0, 0,       // First two from class 0
                1, 1, 1, 1, // Next four from class 1
                2, 2, 2     // Last three from class 2
            };


            // Now we will create the K-Nearest Neighbors algorithm. For this
            // example, we will be choosing k = 4. This means that, for a given
            // instance, its nearest 4 neighbors will be used to cast a decision.
            var knn = new KNearestNeighbors <double[]>(k: 4, distance: new SquareEuclidean());

            // We learn the algorithm:
            knn.Learn(inputs, outputs);

            // After the algorithm has been created, we can classify a new instance:
            int answer = knn.Decide(new double[] { 11, 5, 4 }); // answer will be 2.
            #endregion

            Assert.AreEqual(2, answer);
        }
 public ICU()
 {
     InitializeComponent();
     current_frame_num1 = 0;
     current_frame_num2 = 0;
     F_E = new FeaturesExtraction();
     knn = Serializer.Load <KNearestNeighbors>(Path.Combine(path, "knn7.bin"));
     RF  = Serializer.Load <RandomForest>(Path.Combine(path, "RF7.bin"));
     LR  = Serializer.Load <LogisticRegression>(Path.Combine(path, "LR7.bin"));
     SVM = Serializer.Load <SupportVectorMachine <Gaussian> >(Path.Combine(path, "SVM7.bin"));
     NB  = Serializer.Load <NaiveBayes>(Path.Combine(path, "NB7.bin"));
     HMM = Serializer.Load <HiddenMarkovModel>(Path.Combine(path, "HMM_seq7.bin"));
     dataGridView1.RowTemplate.Height = 120;
     ((DataGridViewImageColumn)dataGridView1.Columns[0]).ImageLayout = DataGridViewImageCellLayout.Stretch;
     dataGridView1.Columns[1].Visible = false;
 }
示例#7
0
        public void KNearestNeighborConstructorTest2()
        {
            // Create some sample learning data. In this data,
            // the first two instances belong to a class, the
            // four next belong to another class and the last
            // three to yet another.

            double[][] inputs =
            {
                // The first two are from class 0
                new double[] { -5, -2, -1 },
                new double[] { -5, -5, -6 },

                // The next four are from class 1
                new double[] {  2,  1,  1 },
                new double[] {  1,  1,  2 },
                new double[] {  1,  2,  2 },
                new double[] {  3,  1,  2 },

                // The last three are from class 2
                new double[] { 11,  5,  4 },
                new double[] { 15,  5,  6 },
                new double[] { 10,  5,  6 },
            };

            int[] outputs =
            {
                0, 0,       // First two from class 0
                1, 1, 1, 1, // Next four from class 1
                2, 2, 2     // Last three from class 2
            };


            // Now we will create the K-Nearest Neighbors algorithm. For this
            // example, we will be choosing k = 4. This means that, for a given
            // instance, its nearest 4 neighbors will be used to cast a decision.
            KNearestNeighbors knn = new KNearestNeighbors(k: 4, classes: 3,
                                                          inputs: inputs, outputs: outputs);


            // After the algorithm has been created, we can classify a new instance:

            int answer = knn.Compute(new double[] { 11, 5, 4 }); // answer will be 2.


            Assert.AreEqual(2, answer);
        }
示例#8
0
        private void KNN_Load(object sender, EventArgs e)
        {
            int numFeatures = 5, numSamples = 5000;

            inputs  = new double[numSamples * nums.Length][];
            outputs = new int[numSamples * nums.Length];

            loadData(ref inputs, ref outputs, numFeatures, numSamples);

            knn = new KNearestNeighbors(k: nums.Length);
            knn.Learn(inputs, outputs);

            //var cm = GeneralConfusionMatrix.Estimate(knn, inputs, outputs);

            //double error = cm.Error;
            //double acc = cm.Accuracy;
            //double kappa = cm.Kappa;
        }
        public override ConfusionMatrix Execute()
        {
            //Create a knn classifer with 2 classes
            var knn = new KNearestNeighbors(k: k,
                classes: 2,
                inputs: trainingSet,
                outputs: trainingOutput);

            //Map the classifier over the test set
            //This wil return an array where index i is the classificatioon of the i-th vector
            //of the testSet
            var predicted = AlgorithmHelpers
                .MergeArrays(trainingSet, testSet)
                .Select(x => knn.Compute(x))
                .ToArray();

            //Create a new confusion matrix with the calculated parameters
            var cmatrix = new ConfusionMatrix(predicted, AlgorithmHelpers.MergeArrays(trainingOutput, expected), POSITIVE, NEGATIVE);
            return cmatrix;
        }
示例#10
0
        public void learn_string()
        {
            #region doc_learn_text
            // The k-Nearest Neighbors algorithm can be used with
            // any kind of data. In this example, we will see how
            // it can be used to compare, for example, Strings.

            string[] inputs =
            {
                "Car",     // class 0
                "Bar",     // class 0
                "Jar",     // class 0

                "Charm",   // class 1
                "Chair"    // class 1
            };

            int[] outputs =
            {
                0, 0, 0,  // First three are from class 0
                1, 1,     // And next two are from class 1
            };


            // Now we will create the K-Nearest Neighbors algorithm. For this
            // example, we will be choosing k = 1. This means that, for a given
            // instance, only its nearest neighbor will be used to cast a new
            // decision.

            // In order to compare strings, we will be using Levenshtein's string distance
            var knn = new KNearestNeighbors <string>(k: 1, distance: new Levenshtein());

            // We learn the algorithm:
            knn.Learn(inputs, outputs);

            // After the algorithm has been created, we can use it:
            int answer = knn.Decide("Chars"); // answer should be 1.
            #endregion

            Assert.AreEqual(1, answer);
        }
示例#11
0
        public void Run()
        {
            // Create some sample learning data. In this data,
            // the first two instances belong to a class, the
            // four next belong to another class and the last
            // three to yet another.

            double[][] inputs =
            {
            // The first two are from class 0
            new double[] { -5, -2, -1 },
            new double[] { -5, -5, -6 },

            // The next four are from class 1
            new double[] {  2,  1,  1 },
            new double[] {  1,  1,  2 },
            new double[] {  1,  2,  2 },
            new double[] {  3,  1,  2 },

            // The last three are from class 2
            new double[] { 11,  5,  4 },
            new double[] { 15,  5,  6 },
            new double[] { 10,  5,  6 },
            };

            int[] outputs =
            {
            0, 0,        // First two from class 0
            1, 1, 1, 1,  // Next four from class 1
            2, 2, 2      // Last three from class 2
            };

            // Now we will create the K-Nearest Neighbors algorithm. For this
            // example, we will be choosing k = 4. This means that, for a given
            // instance, its nearest 4 neighbors will be used to cast a decision.
            KNearestNeighbors knn = new KNearestNeighbors(k: 4, classes: 3,
                inputs: inputs, outputs: outputs);

            // After the algorithm has been created, we can classify a new instance:
            int answer = knn.Compute(new double[] { 11, 5, 4 }); // answer will be 2.
        }
示例#12
0
        private static void knn(double[][] inputs, int[] outputs)
        {
            // Create a new k-NN algorithm:
            var knn = new KNearestNeighbors()
            {
                K        = 3,              // base a decision on the class labels of the three nearest neighbors of the query point
                Distance = new Euclidean() // actually the default
            };

            // Learn a k-NN classifier
            knn = knn.Learn(inputs, outputs);

            // Get predictions according to kNN
            int[] predicted = knn.Decide(inputs);

            // Create a confusion matrix to check the quality of the predictions:
            var cm = new ConfusionMatrix(predicted: predicted, expected: outputs);

            // Check the accuracy measure:
            double accuracy = cm.Accuracy; // (should be 1.0 or 100%)
        }
示例#13
0
        public override ConfusionMatrix Execute()
        {
            //Create a knn classifer with 2 classes
            var knn = new KNearestNeighbors(k: k,
                                            classes: 2,
                                            inputs: trainingSet,
                                            outputs: trainingOutput);

            //Map the classifier over the test set
            //This wil return an array where index i is the classificatioon of the i-th vector
            //of the testSet
            var predicted = AlgorithmHelpers
                            .MergeArrays(trainingSet, testSet)
                            .Select(x => knn.Compute(x))
                            .ToArray();

            //Create a new confusion matrix with the calculated parameters
            var cmatrix = new ConfusionMatrix(predicted, AlgorithmHelpers.MergeArrays(trainingOutput, expected), POSITIVE, NEGATIVE);

            return(cmatrix);
        }
示例#14
0
        private KnnManager()
        {
            // Read the file line by line
            string line;
            int    index = 0;

            System.IO.StreamReader file = new System.IO.StreamReader(GlobalConstant.coodbookFilePath);
            while ((line = file.ReadLine()) != null)
            {
                codebook[index]     = Array.ConvertAll(line.Split(' '), new Converter <string, double>(Double.Parse));
                clusterIndex[index] = index;
                index++;
            }

            file.Close();

            knn = new KNearestNeighbors(k: GlobalConstant.numberOfKNearestNeighbour
                                        , classes: GlobalConstant.numberOfClusters
                                        , inputs: codebook
                                        , outputs: clusterIndex);
        }
示例#15
0
        public static KeyValuePair <Dictionary <int, string>, KNearestNeighbors> KnnCreateWithLabelMap(Dictionary <List <string>, double[][]> trainingSet)
        {
            int        labelCounter           = -1;
            List <int> classesList            = new List <int>();
            Dictionary <int, string> labelMap = new Dictionary <int, string>();

            /* Since the kNN algorithm generates a model with int values instead of string values for the label,
             * it is imperative to generate a map for reference.
             */
            foreach (string label in trainingSet.First().Key.ToArray())
            {
                if (!labelMap.ContainsValue(label))
                {
                    labelCounter++;
                    classesList.Add(labelCounter);
                    labelMap.Add(labelCounter, label);
                    //Console.WriteLine(labelCounter + ": " + label);
                }
                else
                {
                    classesList.Add(labelCounter);
                }
            }

            int[]      classes = classesList.ToArray();
            double[][] inputs  = trainingSet.First().Value;


            // Now we will create the K-Nearest Neighbors algorithm.
            // It's possible to swtich around the k: 5 for the possibility of better accuracy
            var knn = new KNearestNeighbors(k: 5);

            // We train the algorithm:
            knn.Learn(inputs, classes);

            // Generate the result.
            KeyValuePair <Dictionary <int, string>, KNearestNeighbors> result = new KeyValuePair <Dictionary <int, string>, KNearestNeighbors>(labelMap, knn);

            return(result);
        }
示例#16
0
        private static Hashtable Train(Hashtable usersData)
        {
            Hashtable usersTraining = new Hashtable();

            foreach (DictionaryEntry userData in usersData)
            {
                try
                {
                    KNearestNeighbors userKNNModel = TrainUser((DataTable)userData.Value);
                    if (userKNNModel != null)
                    {
                        usersTraining.Add(userData.Key, userKNNModel);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            return(usersTraining);
        }
        private static void Create(int n1, int n2, int k, out double[][] inputs, out NaiveKNearestNeighbors naive, out KNearestNeighbors <double[]> normal, out KNearestNeighbors target)
        {
            int n = n1 + n2;

            double[][] gauss1 = MultivariateNormalDistribution.Generate(n1,
                                                                        mean: new double[] { 2, 1 },
                                                                        covariance: new double[, ]
            {
                { 1, 0 },
                { 0, 1 },
            });

            double[][] gauss2 = MultivariateNormalDistribution.Generate(n2,
                                                                        mean: new double[] { -1, 4 },
                                                                        covariance: new double[, ]
            {
                { 2, 1 },
                { 0, 3 },
            });

            inputs = gauss1.Stack(gauss2);
            int[] outputs = Matrix.Vector(n1, 0).Concatenate(Matrix.Vector(n2, +1));

            var idx = Vector.Sample(n1 + n2);

            inputs  = inputs.Submatrix(idx);
            outputs = outputs.Submatrix(idx);

            naive  = new NaiveKNearestNeighbors(k, inputs, outputs);
            normal = new KNearestNeighbors <double[]>(k, inputs, outputs, new Euclidean());
            Assert.AreEqual(2, normal.NumberOfInputs);
            Assert.AreEqual(2, normal.NumberOfOutputs);
            Assert.AreEqual(2, normal.NumberOfClasses);

            target = new KNearestNeighbors(k, inputs, outputs);
            Assert.AreEqual(2, target.NumberOfInputs);
            Assert.AreEqual(2, target.NumberOfOutputs);
            Assert.AreEqual(2, target.NumberOfClasses);
        }
示例#18
0
        public Pokedex()
        {
            PokeContext ct = new PokeContext();
            var pokeList = ct.Pokemons.ToList();
            var pokeMatrix = pokeList.Select(p => new string[] { p.Body.ToString() , p.Color , p.Habitat , p.Ability1 , p.Ability2 , p.Exp }).ToArray();
            var pokeClasses = pokeList.Select(p => (int)p.Type ).ToArray();

            discPokeMatrix = new System.Data.DataTable();

            discPokeMatrix.Columns.Add("Body");
            discPokeMatrix.Columns.Add("Color");
            discPokeMatrix.Columns.Add("Habitat");
            discPokeMatrix.Columns.Add("Ability1");
            discPokeMatrix.Columns.Add("Ability2");
            discPokeMatrix.Columns.Add("Exp");

            foreach (var poke in pokeMatrix)
            {
                discPokeMatrix.Rows.Add(poke[0] ?? "0", poke[1] ?? "0", poke[2] ?? "0", poke[3] ?? "0", poke[4] ?? "0", poke[5] ?? "0");
            }

            codebook = new Accord.Statistics.Filters.Normalization();

            var codPokemon = new double[718][];

            discPokeMatrix = codebook.Apply(discPokeMatrix);
            //int i = 0;
            //foreach (var poke in pokeMatrix)
            //{
            //    codPokemon[i] = codebook.Translate(poke[0], poke[1], poke[2], poke[3], poke[4], poke[5]).Select(k => Convert.ToDouble(k)).ToArray();
            //    i++;
            //}

            for (int i = 0; i < 718; i++)
            {
                codPokemon[i] = discPokeMatrix.Rows[i].ItemArray.Select(o => (double)o).ToArray();
            }
            knnPoke = new KNearestNeighbors<double[]>(1, codPokemon, pokeClasses, Accord.Math.Distance.Hamming);
        }
示例#19
0
        public void KNearestNeighborConstructorTest3()
        {
            // The k-Nearest Neighbors algorithm can be used with
            // any kind of data. In this example, we will see how
            // it can be used to compare, for example, Strings.

            string[] inputs =
            {
                "Car",     // class 0
                "Bar",     // class 0
                "Jar",     // class 0

                "Charm",   // class 1
                "Chair"    // class 1
            };

            int[] outputs =
            {
                0, 0, 0,  // First three are from class 0
                1, 1,     // And next two are from class 1
            };


            // Now we will create the K-Nearest Neighbors algorithm. For this
            // example, we will be choosing k = 1. This means that, for a given
            // instance, only its nearest neighbor will be used to cast a new
            // decision.

            // In order to compare strings, we will be using Levenshtein's string distance
            KNearestNeighbors <string> knn = new KNearestNeighbors <string>(k: 1, classes: 2,
                                                                            inputs: inputs, outputs: outputs, distance: Distance.Levenshtein);


            // After the algorithm has been created, we can use it:
            int answer = knn.Compute("Chars"); // answer should be 1.

            Assert.AreEqual(1, answer);
        }
示例#20
0
        public void Main()
        {
            WriteLine("Execution begins...");

            var fn   = @"c:\DEMO\Data\train.csv";
            var f    = File.ReadLines(fn);
            var data = from z in f.Skip(1)
                       let zz = z.Split(',').Select(int.Parse)
                                select new Digit
            {
                Label = zz.First(),
                Image = zz.Skip(1).ToArray()
            };
            var train = data.Take(10000).ToArray();
            var test  = data.Skip(10000).Take(1000).ToArray();

            var classifier = new KNearestNeighbors(1);

            classifier.Learn(
                (from x in train select x.Image.Select(z => (double)z).ToArray()).ToArray(),
                (from x in train select x.Label).ToArray());

            int count = 0, correct = 0;

            foreach (var z in test)
            {
                var n = classifier.Decide(z.Image.Select(t => (double)t).ToArray());
                WriteLine("{0} => {1}", z.Label, n);
                if (z.Label == n)
                {
                    correct++;
                }
                count++;
            }

            WriteLine("Done, {0} of {1} correct ({2}%)", correct, count, (double)correct / (double)count * 100);
            ReadKey();
        }
示例#21
0
        public int DoSimplePrediction(double[][] inputs, int output)
        {
            Dataset simpleTrainedDataset = new Dataset(Constants.Constants.SimpleTrainedDataFilePath);

            double[][] simpleTrainedDatasetInputs  = simpleTrainedDataset.Instances;
            int[]      simpleTrainedDatasetOutputs = simpleTrainedDataset.ClassLabels;

            var knn = new KNearestNeighbors()
            {
                K        = 5,
                Distance = new Euclidean()
            };


            knn = knn.Learn(simpleTrainedDatasetInputs, simpleTrainedDatasetOutputs);
            int[] predicted = knn.Decide(inputs);

            return(predicted
                   .GroupBy(_ => _)
                   .OrderByDescending(_ => _.Count())
                   .Select(_ => _.Key)
                   .First());
        }
示例#22
0
        public void weights_test_tree_1()
        {
            KNearestNeighbors a;
            KNearestNeighbors b;

            {
                double[][] inputs  = Jagged.ColumnVector(4.2, 0.7, 0.7, 0.7, 1.3, 9.4, 9.4, 12);
                int[]      outputs = { 0, 0, 0, 1, 1, 2, 2, 2 };
                double[]   weights = { 1, 1, 0, 0, 1, 1, 0, 1 };
                var        knn     = new KNearestNeighbors(k: inputs.Length);
                a = knn.Learn(inputs, outputs, weights);
            }

            {
                double[][] inputs  = Jagged.ColumnVector(4.2, 0.7, 1.3, 9.4, 12);
                int[]      outputs = { 0, 0, 1, 2, 2 };
                var        knn     = new KNearestNeighbors(k: inputs.Length);
                b = knn.Learn(inputs, outputs);
            }

            double[][] x = Jagged.ColumnVector(4.2, 0.7, 1.3, 9.4, 12);
            Assert.AreEqual(a.Scores(x), b.Scores(x));
        }
示例#23
0
        public void weights_test_1()
        {
            KNearestNeighbors <string> a;
            KNearestNeighbors <string> b;

            {
                string[] inputs  = { "Car", "Bar", "Bar", "Bar", "Jar", "Charm", "Charm", "Chair" };
                int[]    outputs = { 0, 0, 0, 1, 1, 2, 2, 2 };
                double[] weights = { 1, 1, 0, 0, 1, 1, 0, 1 };
                var      knn     = new KNearestNeighbors <string>(k: inputs.Length, distance: new Levenshtein());
                a = knn.Learn(inputs, outputs, weights);
            }

            {
                string[] inputs  = { "Car", "Bar", "Jar", "Charm", "Chair" };
                int[]    outputs = { 0, 0, 1, 2, 2 };
                var      knn     = new KNearestNeighbors <string>(k: inputs.Length, distance: new Levenshtein());
                b = knn.Learn(inputs, outputs);
            }

            string[] x = new[] { "Car", "Bar", "Jar", "Charm", "Chair" };
            Assert.AreEqual(a.Scores(x), b.Scores(x));
        }
示例#24
0
        private static void KNNCompute(int K, double[][] inputs, int[] outputs, double[][] test, int[] answer, List <string> testData, System.IO.StreamWriter fw)
        {
            var knn = new KNearestNeighbors(K);

            knn.Learn(inputs, outputs);

            //测试
            int    i = 0;
            double accuracy;
            int    correctCount = 0;

            foreach (var testDetail in test)
            {
                var predict = knn.Decide(testDetail);
                //fw.WriteLine($"歌曲:{testData[i].Split(',')[0]}, 正确答案是{answer[i]}, KNN(K={K}认为):{predict}");
                if (answer[i] == predict)
                {
                    correctCount++;
                }
                i++;
            }
            accuracy = (double)correctCount / (double)test.Count();
            fw.WriteLine($"KNN(K={K})的正确率:" + accuracy);
        }
示例#25
0
        public void learn_string()
        {
            string basePath = NUnit.Framework.TestContext.CurrentContext.TestDirectory;

            #region doc_learn_text
            // The k-Nearest Neighbors algorithm can be used with
            // any kind of data. In this example, we will see how
            // it can be used to compare, for example, Strings.

            string[] inputs =
            {
                "Car",     // class 0
                "Bar",     // class 0
                "Jar",     // class 0

                "Charm",   // class 1
                "Chair"    // class 1
            };

            int[] outputs =
            {
                0, 0, 0,  // First three are from class 0
                1, 1,     // And next two are from class 1
            };


            // Now we will create the K-Nearest Neighbors algorithm. For this
            // example, we will be choosing k = 1. This means that, for a given
            // instance, only its nearest neighbor will be used to cast a new
            // decision.

            // In order to compare strings, we will be using Levenshtein's string distance
            var knn = new KNearestNeighbors <string>(k: 1, distance: new Levenshtein());

            // We learn the algorithm:
            knn.Learn(inputs, outputs);

            // After the algorithm has been created, we can use it:
            int answer = knn.Decide("Chars"); // answer should be 1.

            // Let's say we would like to compute the error matrix for the classifier:
            var cm = ConfusionMatrix.Estimate(knn, inputs, outputs);

            // We can use it to estimate measures such as
            double error = cm.Error;    // should be 0
            double acc   = cm.Accuracy; // should be 1
            double kappa = cm.Kappa;    // should be 1
            #endregion

            Assert.AreEqual(1, answer);
            Assert.AreEqual(0, error);
            Assert.AreEqual(1, acc);
            Assert.AreEqual(1, kappa);

#if !NO_BINARY_SERIALIZATION
            knn.Save(Path.Combine(basePath, "string_knn.bin"));

            var loaded_knn = Serializer.Load <KNearestNeighbors <string> >(Path.Combine(basePath, "string_knn.bin"));

            Assert.AreEqual(1, loaded_knn.Decide("Chars"));
            cm = ConfusionMatrix.Estimate(loaded_knn, inputs, outputs);
            Assert.AreEqual(0, cm.Error);
            Assert.AreEqual(1, cm.Accuracy);
            Assert.AreEqual(1, cm.Kappa);

            Assert.AreEqual(knn.ClassCount, loaded_knn.ClassCount);
            Assert.AreEqual(knn.Distance, loaded_knn.Distance);
            Assert.AreEqual(knn.K, loaded_knn.K);
            Assert.AreEqual(knn.NumberOfClasses, loaded_knn.NumberOfClasses);
            Assert.AreEqual(knn.NumberOfInputs, loaded_knn.NumberOfInputs);
            Assert.AreEqual(knn.NumberOfOutputs, loaded_knn.NumberOfOutputs);
            Assert.AreEqual(knn.Outputs, loaded_knn.Outputs);
            Assert.AreEqual(knn.Token, loaded_knn.Token);
#endif
        }
示例#26
0
        private List<String> GetSimilaresDatabaseKNN(List<Double> descriptoresEntrada)
        {
            ModeloSimilitudEntities db=new ModeloSimilitudEntities();
            Double[] vectorEntrada=descriptoresEntrada.ToArray();
            vectorEntrada = Normalizar(vectorEntrada);
            Double[][] matriz = csvtoMatrix("descriptoresNormalizados");

            int[] pertenencia=new int[matriz.Length];
            for(int i=0;i<pertenencia.Length;i++){
                pertenencia[i]=1;
            }
            pertenencia[23] = 2;

            KNearestNeighbors knn = new KNearestNeighbors(k: 10, inputs: matriz, outputs: pertenencia);
            int answer = knn.Compute(vectorEntrada);
            int[] a = new int[1]; a[0] = 1;
            Double[][] cercanos = knn.GetNearestNeighbors(vectorEntrada,out a);

            List<String> listaSimilares = new List<String>();
            List<canciones> dbcanciones = db.canciones.ToList();
            for (int i = 0; i < matriz.Length; i++)
            {
                if (cercanos.Contains(matriz[i]))
                {
                    listaSimilares.Add(dbcanciones[i].id_spotify.Substring(14));
                }
            }

                //string select="select * from canciones where energy={0} and liveness={1} and tempo={2} and speechiness={3} and acousticness={4} and loudness={5} and valence={6} and danceability={7} and instrumentalness={8} and key={9}";
                //string select2 = "select * from canciones";
                //for(int j=0;j<cercanos.Length;j++){
                //    object[] parameters = new object[10];
                //    for (int i = 0; i < 10; i++)
                //    {
                //            SqlParameter param = new SqlParameter("i", cercanos[j][i]);
                //            parameters[i] = cercanos[j][i];
                //        }
                //        var stores = db.Database.SqlQuery<canciones>(select, parameters).ToList();
                //        listaSimilares.Add(stores[0].id_spotify);
                //}

                return listaSimilares;
        }
示例#27
0
        private static void Create(int n1, int n2, int k, out double[][] inputs, out NaiveKNearestNeighbors naive, out KNearestNeighbors<double[]> normal, out KNearestNeighbors target)
        {
            int n = n1 + n2;

            double[][] gauss1 = MultivariateNormalDistribution.Generate(n1,
                mean: new double[] { 2, 1 },
                covariance: new double[,] 
                {
                    { 1, 0 },
                    { 0, 1 },
                });

            double[][] gauss2 = MultivariateNormalDistribution.Generate(n2,
                mean: new double[] { -1, 4 },
                covariance: new double[,] 
                {
                    { 2, 1 },
                    { 0, 3 },
                });

            inputs = gauss1.Stack(gauss2);
            int[] outputs = Matrix.Vector(n1, 0).Concatenate(Matrix.Vector(n2, +1));

            var idx = Vector.Sample(n1 + n2);
            inputs = inputs.Submatrix(idx);
            outputs = outputs.Submatrix(idx);

            naive = new NaiveKNearestNeighbors(k, inputs, outputs);
            normal = new KNearestNeighbors<double[]>(k, inputs, outputs, new Euclidean());
            target = new KNearestNeighbors(k, inputs, outputs);
        }
示例#28
0
        public void ClusterAlg()
        {
            var data = new double[10][];
            for (int i = 0; i < 10; i++)
            {
                var curr = new double[3];

                for (int j = 0; j < 3; j++)
                {
                    curr[j] = Math.Sin((double)j);
                }

                data[i] = curr;
            }

            var cout = new ConsoleOut("Temp.txt");
            var kNNNode = new KNearestNeighbors(3, data, new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            kNNNode.Process.LinkTo(cout.Output);
            kNNNode.Process.Post(new MachineLearningData<double[], int>(new double[] { 0, 1, 2 }));
            System.Threading.Thread.Sleep(1000);
        }
示例#29
0
 internal void train(int k, int classes)
 {
     knn = new KNearestNeighbors(k, classes, feature, answer);
 }
示例#30
0
        private void browse_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "All images|*.jpeg;*.png;*.jpg;*.bmp|JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|Bitmap Files (*.bmp)|*.bmp";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                string filename = dlg.FileName;
                Uri uri = new Uri(filename);
                BitmapImage imgSource = new BitmapImage(uri);

                FileNameTextBox.Text = filename;
                picture.Source = imgSource;

                /* KNN */
                List<double []> letters = new List<double[]>();
                List<int> outputList = new List<int>();
                List<string> indexes = new List<string>();

                foreach (string letterPath in dictionary) {
                    FileInfo file = new FileInfo(letterPath);
                    string nameClean = file.Name.Substring(0, file.Name.Length - 4);

                    while (Char.IsDigit(nameClean[nameClean.Length - 1])) {
                        nameClean = nameClean.Substring(0, nameClean.Length - 1);
                    }
                    int i = indexes.IndexOf(nameClean);

                    if (i <= -1) {
                        indexes.Add(nameClean);
                        outputList.Add(indexes.Count - 1);
                    } else {
                        outputList.Add(i);
                    }
                    letters.Add(getVectors(cropImage(getImageBitmap(file.FullName))));
                }

                DateTime start = DateTime.Now;

                KNearestNeighbors knn = new KNearestNeighbors(k: 3, classes: indexes.Count, inputs: letters.ToArray(), outputs: outputList.ToArray());
                int answer = knn.Compute(getVectors(cropImage(MakeGrayscale(getImageBitmap(filename)))));
                string res = indexes[answer];
                lettre.Content = res;
                timeSpent.Content = "Execution time: " + (DateTime.Now - start);
            }
        }
 public void Reset()
 {
     knn = null;
 }
示例#32
0
        public void KNearestNeighborConstructorTest()
        {
            double[][] inputs = 
            {
                new double[] { -5, -2, -1 },
                new double[] { -5, -5, -6 },

                new double[] {  2,  1,  1 },
                new double[] {  1,  1,  2 },
                new double[] {  1,  2,  2 },
                new double[] {  3,  1,  2 },

                new double[] { 11,  5,  4 },
                new double[] { 15,  5,  6 },
                new double[] { 10,  5,  6 },
            };

            int[] outputs =
            {
                0, 0,
                1, 1, 1, 1,
                2, 2, 2
            };

            int k = 3;
           
            KNearestNeighbors target = new KNearestNeighbors(k, inputs, outputs);

            for (int i = 0; i < inputs.Length; i++)
            {
                int actual = target.Compute(inputs[i]);
                int expected = outputs[i];

                Assert.AreEqual(expected, actual);
            }

            double[][] test = 
            {
                new double[] { -4, -3, -1 },
                new double[] { -5, -4, -4 },

                new double[] {  5,  3,  4 },
                new double[] {  3,  1,  6 },

                new double[] { 10,  5,  4 },
                new double[] { 13,  4,  5 },
            };

            int[] expectedOutputs =
            {
                0, 0,
                1, 1,
                2, 2,
            };

            for (int i = 0; i < test.Length; i++)
            {
                int actual = target.Compute(test[i]);
                int expected = expectedOutputs[i];

                Assert.AreEqual(expected, actual);
            }
        }
        public void KNearestNeighbor_CrossValidation()
        {
            // Create some sample learning data. In this data,
            // the first two instances belong to a class, the
            // four next belong to another class and the last
            // three to yet another.

            double[][] inputs = 
            {
                // The first two are from class 0
                new double[] { -5, -2, -1 },
                new double[] { -5, -5, -6 },

                // The next four are from class 1
                new double[] {  2,  1,  1 },
                new double[] {  1,  1,  2 },
                new double[] {  1,  2,  2 },
                new double[] {  3,  1,  2 },

                // The last three are from class 2
                new double[] { 11,  5,  4 },
                new double[] { 15,  5,  6 },
                new double[] { 10,  5,  6 },
            };

            int[] outputs =
            {
                0, 0,        // First two from class 0
                1, 1, 1, 1,  // Next four from class 1
                2, 2, 2      // Last three from class 2
            };



            // Create a new Cross-validation algorithm passing the data set size and the number of folds
            var crossvalidation = new CrossValidation(size: inputs.Length, folds: 3);

            // Define a fitting function using Support Vector Machines. The objective of this
            // function is to learn a SVM in the subset of the data indicated by cross-validation.

            crossvalidation.Fitting = delegate(int k, int[] indicesTrain, int[] indicesValidation)
            {
                // The fitting function is passing the indices of the original set which
                // should be considered training data and the indices of the original set
                // which should be considered validation data.

                // Lets now grab the training data:
                var trainingInputs = inputs.Submatrix(indicesTrain);
                var trainingOutputs = outputs.Submatrix(indicesTrain);

                // And now the validation data:
                var validationInputs = inputs.Submatrix(indicesValidation);
                var validationOutputs = outputs.Submatrix(indicesValidation);

                // Now we will create the K-Nearest Neighbors algorithm. For this
                // example, we will be choosing k = 4. This means that, for a given
                // instance, its nearest 4 neighbors will be used to cast a decision.
                KNearestNeighbors knn = new KNearestNeighbors(k: 4, classes: 3,
                    inputs: inputs, outputs: outputs);


                // After the algorithm has been created, we can classify instances:
                int[] train_predicted = trainingInputs.Apply(knn.Compute);
                int[] test_predicted = validationInputs.Apply(knn.Compute);

                // Compute classification error
                var cmTrain = new ConfusionMatrix(train_predicted, trainingOutputs);
                double trainingAcc = cmTrain.Accuracy;

                // Now we can compute the validation error on the validation data:
                var cmTest = new ConfusionMatrix(test_predicted, validationOutputs);
                double validationAcc = cmTest.Accuracy;

                // Return a new information structure containing the model and the errors achieved.
                return new CrossValidationValues(knn, trainingAcc, validationAcc);
            };


            // Compute the cross-validation
            var result = crossvalidation.Compute();

            // Finally, access the measured performance.
            double trainingAccs = result.Training.Mean;
            double validationAccs = result.Validation.Mean;


            Assert.AreEqual(1, trainingAccs);
            Assert.AreEqual(1, validationAccs);
        }
示例#34
0
        public static List<testResult> RunTest(string path,Hashtable dictionary ,int dicSize, Hashtable idfTable, KNearestNeighbors knn)
        {
            List<testResult> result = new List<testResult>();
            //int[] trainingAnswer = new int[17998];
            int count = 0;
            string[] categories = Directory.GetDirectories(path);
            for (int i = 0; i < categories.Count(); i++) //traverse Categories
            {
                Console.WriteLine(Path.GetFileName(categories[i]));
                string[] file_names = Directory.GetFiles(categories[i]);
                for (int j = 0; j < file_names.Count(); j++) //file in Cagetory
                {
                    Console.WriteLine(Path.GetFileName(file_names[j]));
                    System.IO.StreamReader file = new System.IO.StreamReader(file_names[j]);
                    double[] featureV = new double[dicSize];
                    for(int k = 0;k<dicSize;k++) //initial
                        featureV[k] = 0;
                    String line;
                    int counter = 0;
                    Hashtable docWord = new Hashtable();
                    Stemmer stemmer = new Stemmer();
                    int sumWordCount = 0;
                    stemmer.stem();
                    //Console.WriteLine(stemmer.stem("running"));
                    //String word;

                    /******Structured Column*****/
                    while ((line = file.ReadLine()) != null)
                    {
                        //Console.WriteLine(line);
                        if (line.Contains(": "))
                        {
                            string[] splitPart = line.Split(new string[] { ": " }, StringSplitOptions.None);
                            string columnName = splitPart[0].Trim();
                            string content = splitPart[splitPart.Length - 1];
                            if (columnName.ToLower() == "subject")
                            {
                                foreach (string iter_word in Regex.Split(content, @"[^A-Za-z0-9_-]"))
                                {
                                    String word = iter_word.ToLower().Trim(new Char[] { '_', '-' });
                                    double Num;
                                    bool isNum = double.TryParse(word, out Num);
                                    if (isNum)
                                    {
                                        continue;
                                    }
                                    stemmer.add(word.ToCharArray(), word.Length);
                                    stemmer.stem();
                                    word = stemmer.ToString();
                                    if (word.Length == 0)
                                    {
                                        continue;
                                    }
                                    if (stopWordTable.ContainsKey(word))
                                    {
                                        continue;
                                    }
                                    sumWordCount += 1 * subjectWeight;
                                    // word preprocess done
                                    if (docWord.ContainsKey(word))
                                    {
                                        int temp = (int)docWord[word];
                                        temp += 1 * subjectWeight;
                                        docWord[word] = temp;
                                    }
                                    else
                                    {
                                        docWord[word] = 1 * subjectWeight;
                                    }
                                }
                            }
                            /*else if (columnName.ToLower() == "keywords")
                            {
                                foreach (string iter_word in Regex.Split(content, @"[^A-Za-z0-9_-]"))
                                {
                                    String word = iter_word.ToLower().Trim(new Char[] { '_', '-' });
                                    double Num;
                                    bool isNum = double.TryParse(word, out Num);
                                    if (isNum)
                                    {
                                        continue;
                                    }
                                    stemmer.add(word.ToCharArray(), word.Length);
                                    stemmer.stem();
                                    word = stemmer.ToString();
                                    if (word.Length == 0)
                                    {
                                        continue;
                                    }
                                    if (stopWordTable.ContainsKey(word))
                                    {
                                        continue;
                                    }
                                    sumWordCount += 1 * keywordsWeight;
                                    // word preprocess done
                                    if (docWord.ContainsKey(word))
                                    {
                                        int temp = (int)docWord[word];
                                        temp += 1 * keywordsWeight;
                                        docWord[word] = temp;
                                    }
                                    else
                                    {
                                        docWord[word] = 1 * keywordsWeight;
                                    }
                                }
                            }
                            if (columnName.ToLower() == "newsgroups")
                            {
                                foreach (string iter_word in content.Split(new char[] { ',' }))
                                {
                                    String word = iter_word.ToLower().Trim();
                                    sumWordCount += 1 * newsgroupsWeight;
                                    // word preprocess done
                                    if (docWord.ContainsKey(word))
                                    {
                                        int temp = (int)docWord[word];
                                        temp += 1 * newsgroupsWeight;
                                        docWord[word] = temp;
                                    }
                                    else
                                    {
                                        docWord[word] = 1 * newsgroupsWeight;
                                    }
                                }
                            }*/
                            /*else if (columnName.ToLower() == "from")
                            {
                                Regex emailRegex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
                                //find items that matches with our pattern
                                MatchCollection emailMatches = emailRegex.Matches(content);
                                foreach (Match emailMatch in emailMatches)
                                {
                                    String word = emailMatch.Value;
                                    // word preprocess done
                                    if (docWord.ContainsKey(word))
                                    {
                                        int temp = (int)docWord[word];
                                        temp += 1 * fromWeight;
                                        docWord[word] = temp;
                                    }
                                    else
                                    {
                                        docWord[word] = 1 * fromWeight;
                                    }
                                }
                            }*/
                        }
                        else
                        {
                            break;
                        }
                    }

                    /******Text******/
                    while ((line = file.ReadLine()) != null)
                    {
                        if (line.StartsWith(">") || line.StartsWith("|>"))
                        {
                            continue;
                        }
                        //foreach(string iter_word in line.Split(new Char [] {' ', ',', '.', ':', '\t', '\n' }))
                        foreach (string iter_word in Regex.Split(line, @"[^A-Za-z0-9_-]"))
                        {
                            String word = iter_word.ToLower().Trim(new Char[] { '_', '-' });
                            double Num;
                            bool isNum = double.TryParse(word, out Num);
                            if (isNum)
                            {
                                continue;
                            }
                            stemmer.add(word.ToCharArray(), word.Length);
                            stemmer.stem();
                            word = stemmer.ToString();
                            if (word.Length == 0)
                            {
                                continue;
                            }
                            if (stopWordTable.ContainsKey(word))
                            {
                                continue;
                            }
                            sumWordCount++;
                            // word preprocess done
                            if (docWord.ContainsKey(word))
                            {
                                int temp = (int)docWord[word];
                                temp++;
                                docWord[word] = temp;
                            }
                            else
                            {
                                docWord[word] = 1;
                            }
                        }
                    }// line end
                    foreach (string word in docWord.Keys)
                    {
                        if (dictionary.ContainsKey(word))
                        {
                            int indexOfDic = (int)dictionary[word];
                            double TF = System.Convert.ToDouble((int)docWord[word])/System.Convert.ToDouble(sumWordCount);
                            double IDF = (double)idfTable[word];
                            featureV[indexOfDic] = TF * IDF;
                        }
                    }
                    testResult resultTemp = new testResult();
                    resultTemp.docName = Path.GetFileName(file_names[j]);
                    resultTemp.oriClass = i;
                    resultTemp.resultClass = knn.Compute(featureV);
                    result.Add(resultTemp);
                    Console.WriteLine(resultTemp.resultClass);
                }//file end
                //Console.ReadLine();
            }//category end
            return result;
        }
示例#35
0
 static void Main(string[] args)
 {
     //for (int j = 1000; j < 1001; j += 1)
     int j = 5000;
     {
         columnCountTable = new Hashtable();
         wordCountTable = new Hashtable();
         stopWordTable = new Hashtable();
         docFeatureTable = new List<Hashtable>();
         fileList = new List<string>();
         classWordTable = new List<Hashtable>();
         classIDFTable = new Hashtable();
         GC.Collect();
         subjectWeight = 5;
         fromWeight = 0;
         keywordsWeight = 3;
         newsgroupsWeight = 20;
         //Console.WriteLine(test());
         //String text = System.IO.File.ReadAllLines();
         Hashtable process_dictionary = new Hashtable();
         StreamReader stopFile = new StreamReader(@"D:\work\KPMG\learning\project1\stopword.txt");
         string line;
         while ((line = stopFile.ReadLine()) != null)
         {
             stopWordTable[line.Trim()] = 1;
         }
         stopFile.Close();
         int[] trainingAnswer = ProcessDirectory(@"D:\work\KPMG\learning\project1\test_data\1\Training");
         //StreamWriter dicFile = new StreamWriter(@"D:\work\KPMG\learning\project1\dictionary_1.txt");
         Hashtable idfTable = TFIDF();
         idfTable = classIDFTable;
         Hashtable dictionary = gen_dic(j);
         /*for (int i = 0; i < docFeatureTable.Count; i++)
         {
             List<string> wordList = docFeatureTable[i].Keys.Cast<string>().ToList();
             foreach (string word in wordList)
             {
                 double temp = (double)docFeatureTable[i][word];
                 if (process_dictionary.ContainsKey(word))
                 {
                     temp += (double)process_dictionary[word];
                 }
                 process_dictionary[word] = temp;
             }
         }
         List<dicWord> process_list = new List<dicWord>();
         string[] dictionary = new string[10005];
         foreach (string word in process_dictionary.Keys)
         {
             dicWord temp;
             temp.word = word;
             temp.tfidf = (double)process_dictionary[word];
             process_list.Add(temp);
             //dicFile.WriteLine(word + ": " + process_dictionary[word]);
         }
         process_list.Sort((a, b) => b.tfidf.CompareTo(a.tfidf));
         for (int i = 0; i < process_list.Count(); i++)// gen sorted dictionary
         {
             if (i >= 10000)
                 break;
             dictionary[i] = process_list[i].word;
             dicFile.WriteLine(i + ": " + process_list[i].word + ": " + process_list[i].tfidf);
         }*/
         var trainingResult = new double[trainingAnswer.Count()][];
         GC.Collect();
         trainingResult = gen_training_data(trainingAnswer.Count(), dictionary, j);
         KNearestNeighbors knn;
         //for (int i = 3; i <= 10; i++)
         //{
         int i = 3;
         Console.WriteLine("======================" + i + "======================");
         knn = new KNearestNeighbors(k: i, classes: 20, inputs: trainingResult, outputs: trainingAnswer);
         List<testResult> result = RunTest(@"D:\work\KPMG\learning\project1\test_data\1\Testing", dictionary,j, idfTable, knn);
         System.IO.Directory.CreateDirectory(@"D:\work\KPMG\learning\project1\result\" + j);
         StreamWriter resultFile = new StreamWriter(@"D:\work\KPMG\learning\project1\result\" + j + "\\test_result.csv");
         StreamWriter statisticFile = new StreamWriter(@"D:\work\KPMG\learning\project1\result\" + j + "\\test_statistic.csv");
         Hashtable classResultMap = new Hashtable();
         foreach (testResult temp in result)
         {
             testClassResult classResult = new testClassResult();
             classResult.correct = 0;
             classResult.count = 0;
             if (classResultMap.ContainsKey(temp.oriClass))
             {
                 classResult = (testClassResult)classResultMap[temp.oriClass];
             }
             classResult.count++;
             if (temp.oriClass == temp.resultClass)
             {
                 resultFile.WriteLine(temp.docName + "," + temp.oriClass + "," + temp.resultClass + ",1");
                 classResult.correct++;
             }
             else
             {
                 resultFile.WriteLine(temp.docName + "," + temp.oriClass + "," + temp.resultClass + ",0");
             }
             classResultMap[temp.oriClass] = classResult;
         }
         foreach (int classNo in classResultMap.Keys)
         {
             testClassResult classResult = (testClassResult)classResultMap[classNo];
             statisticFile.WriteLine(classNo + "," + classResult.correct + "," + classResult.count + "," + System.Convert.ToDouble(classResult.correct) / System.Convert.ToDouble(classResult.count));
         }
         resultFile.Close();
         statisticFile.Close();
         // }
         //Console.WriteLine(knn.Compute(trainingResult[13366]));
         /*Console.WriteLine(Array.IndexOf(dictionary, "god"));
         Console.WriteLine(Array.IndexOf(dictionary, "nonzero"));*/
         /*for (int i = 1; i <= 10; i++)
         {
             gen_test(@"D:\work\KPMG\learning\project1\20_newsgroups", @"D:\work\KPMG\learning\project1\test_data", i, 10);
         }*/
         //Console.ReadLine();
     }
 }
示例#36
0
    public void trainClassifier()
    {
        List <FeatureVector> featureVectors = dataService.getAllFeatureVectors();

        double [][] inputs  = new double[featureVectors.Count][];
        int[]       outputs = new int[featureVectors.Count];

        createInputsAndOutputs(inputs, outputs, featureVectors);


        // Code For creating a MulticlassSupportVectorMachine
        // Create the multi-class learning algorithm for the machine

        /*
         * var teacher = new MulticlassSupportVectorLearning<Gaussian>()
         * {
         *  // Configure the learning algorithm to use SMO to train the
         *  //  underlying SVMs in each of the binary class subproblems.
         *  Learner = (param) => new SequentialMinimalOptimization<Gaussian>()
         *  {
         *      // Estimate a suitable guess for the Gaussian kernel's parameters.
         *      // This estimate can serve as a starting point for a grid search.
         *      UseKernelEstimation = true
         *  }
         * };
         *
         * // Learn a machine
         * var machine = teacher.Learn(inputs, outputs);
         *
         *
         * // Create the multi-class learning algorithm for the machine
         * var calibration = new MulticlassSupportVectorLearning<Gaussian>()
         * {
         *  Model = machine, // We will start with an existing machine
         *
         *  // Configure the learning algorithm to use Platt's calibration
         *  Learner = (param) => new ProbabilisticOutputCalibration<Gaussian>()
         *  {
         *      Model = param.Model // Start with an existing machine
         *  }
         * };
         *
         * classifier = calibration.Learn(inputs, outputs);*/

        // Code for creating a KNN classifier

        int K = (int)(Mathf.Sqrt(inputs.GetLength(0)) / 2.0f);

        classifier = new KNearestNeighbors(k: K, distance: new Euclidean());
        classifier.Learn(inputs, outputs);


        // Code For creating a random forest classifier.

        // Create the forest learning algorithm

        /*
         * var teacher = new RandomForestLearning()
         * {
         *  NumberOfTrees = 50,
         * };
         *
         * classifier = teacher.Learn(inputs, outputs);
         */

        saveModel();
        TrainingFinished = true;
    }
示例#37
0
文件: KNN.cs 项目: AriFreyr/Arti-CV
 public void TrainKNN(double[][] inputs, int[] outputs, int k)
 {
     _knn = new KNearestNeighbors(k, 3, inputs, outputs);
 }
示例#38
0
        public JsonResult PredictPossibleProducts()
        {
            var userId       = 0;
            int knnNum       = 5;
            int clusterNum   = 4;
            var userIdString = "";

            if (HttpContext.Session["userid"] == null)
            {
                return(Json(new { errorCode = 1, errorMessage = "יוזר לא חוקי" }));
            }

            userIdString = HttpContext.Session["userid"].ToString();
            var didParsed = Int32.TryParse(userIdString, out userId);

            if (!didParsed)
            {
                return(Json(new { errorCode = 1, errorMessage = "יוזר לא חוקי" }));
            }

            var userGender = _context.Users
                             .Where(x => x.Id == userId)
                             .Select(x => x.Gender)
                             .SingleOrDefault();

            var trainData = _context.Purchases
                            .OrderBy(x => x.UserId)
                            .Where(x => x.Product != null)
                            .Select(x => new
            {
                userId     = x.UserId.Value,
                size       = x.Product.Size,
                type       = x.Product.ProductTypeId,
                gender     = x.Product.ProductType.Gender,
                genderUser = x.User.Gender
            })
                            .ToList();

            if (trainData.Count < knnNum || trainData.Count < clusterNum)
            {
                return(Json(new { errorCode = 2, errorMessage = "אין מספיק מידע" }));
            }
            var inputs = trainData.Select(x =>
            {
                double[] res = new double[]
                {
                    Convert.ToInt32(x.gender),
                    Convert.ToInt32(x.genderUser),
                    x.type.Value,
                    x.size
                };

                return(res);
            })
                         .ToArray();

            var codification = new Codification <double>()
            {
                CodificationVariable.Categorical,
                CodificationVariable.Categorical,
                CodificationVariable.Categorical,
                CodificationVariable.Discrete
            };

            // Learn the codification from observations
            var model = codification.Learn(inputs);

            // Transform the mixed observations into only continuous:
            double[][] newInputs = model.ToDouble().Transform(inputs);

            KMedoids kmeans   = new KMedoids(k: clusterNum);
            var      clusters = kmeans.Learn(newInputs);

            int[] labels = clusters.Decide(newInputs);

            var knn5 = new KNearestNeighbors(k: knnNum);

            knn5.Learn(newInputs, labels);

            var purchasesById = _context.Purchases
                                .Where(x => x.Product != null)
                                .Select(x => new
            {
                userId     = x.UserId.Value,
                size       = x.Product.Size,
                type       = x.Product.ProductTypeId,
                gender     = x.Product.ProductType.Gender,
                genderUser = x.User.Gender
            })
                                .GroupBy(x => x.userId)
                                .ToList();

            IList <Tuple <int, int[]> > labelsForUsers = new List <Tuple <int, int[]> >();

            for (int i = 0; i < purchasesById.Count; i++)
            {
                var userInputs = purchasesById[i].
                                 Select(x =>
                {
                    double[] res = new double[]
                    {
                        Convert.ToInt32(x.gender),
                        Convert.ToInt32(x.genderUser),
                        x.type.Value,
                        x.size
                    };

                    return(res);
                })
                                 .ToArray();

                double[][] newUserInputs = model.ToDouble().Transform(userInputs);
                labelsForUsers.Add(new Tuple <int, int[]>(purchasesById[i].Key, clusters.Decide(newUserInputs).Distinct().ToArray()));
            }

            var productIdsUserBought = _context.Purchases
                                       .Where(x => x.UserId == userId)
                                       .Select(x => x.ProductId)
                                       .Distinct()
                                       .ToList();

            var validProductTypeIds = _context.Purchases
                                      .Where(x => x.UserId == userId)
                                      .Select(x => x.Product.ProductTypeId)
                                      .Distinct()
                                      .ToList();

            var productsToPredict = _context.Products
                                    .Where(x => !productIdsUserBought.Contains(x.Id))
                                    .Where(x => validProductTypeIds.Contains(x.ProductTypeId))
                                    .Select(x => new
            {
                id         = x.Id,
                size       = x.Size,
                type       = x.ProductTypeId,
                gender     = x.ProductType.Gender,
                genderUser = userGender
            })
                                    .ToList();

            var predInputs = productsToPredict.Select(x =>
            {
                double[] res = new double[]
                {
                    Convert.ToInt32(x.gender),
                    Convert.ToInt32(x.genderUser),
                    x.type.Value,
                    x.size
                };

                return(res);
            })
                             .ToArray();

            double[][] newPredInputs = model.ToDouble().Transform(predInputs);

            int[] newLabels = knn5.Decide(newPredInputs);

            IList <int> productIdsPrediction = new List <int>();
            var         userLabels           = labelsForUsers.Where(x => x.Item1 == userId).FirstOrDefault() != null?
                                               labelsForUsers.Where(x => x.Item1 == userId).FirstOrDefault().Item2 : new int[0];

            for (int i = 0; i < newLabels.Length; i++)
            {
                if (userLabels.Contains(newLabels[i]))
                {
                    productIdsPrediction.Add(productsToPredict[i].id);
                }
            }

            var predictedProduct = _context.Products
                                   .Where(x => productIdsPrediction.Contains(x.Id))
                                   .Select(x => new
            {
                Id          = x.Id,
                Name        = x.Name,
                Price       = x.Price,
                Size        = x.Size,
                PictureName = x.PictureName
            })
                                   .ToList();

            return(Json(new { products = predictedProduct }, JsonRequestBehavior.AllowGet));
        }
示例#39
0
        private void button2_Click(object sender, EventArgs e)
        {
            wv = new Word2Vec(3);

            textBox3.Text = "Processing";

            System.Diagnostics.Debug.WriteLine("Starting Processing");

            //Get list of classes, input and output vectors
            classlist        = new Dictionary <string, int>();
            inverseClassList = new Dictionary <int, string>();
            List <List <double> > IntMatrixInputs = new List <List <double> >();
            List <string>         stringInputs    = new List <string>();

            List <int> IntOutputs = new List <int>();

            bool frequency = true;

            int temp = 0;

            string[]        delimiters = { "," };
            string[]        fields;
            TextFieldParser tfp;

            tfp = new TextFieldParser(textBox1.Text);
            tfp.HasFieldsEnclosedInQuotes = true;
            tfp.Delimiters = delimiters;
            fields         = tfp.ReadFields();
            int indexClass = 0;
            int indexRows  = 0;

            while (!tfp.EndOfData)
            {
                System.Diagnostics.Debug.WriteLine("Processing training row: " + indexRows);
                fields = tfp.ReadFields();
                if (NotNullFields(fields))
                {
                    if (!classlist.TryGetValue(fields[SelectedClass], out temp))
                    {
                        classlist[fields[SelectedClass]] = indexClass;
                        inverseClassList[indexClass]     = fields[SelectedClass];
                        ++indexClass;
                    }

                    /*
                     * stringInputs.Add(GetString(fields,""));
                     * IntMatrixInputs.Add(Word2Vec.Transform(GetString(fields,""), frequency).ToList()); //getLettersVector.toList();
                     */

                    stringInputs.Add(GetString(fields, " "));
                    wv.addSentence(GetString(fields, " "));
                    IntOutputs.Add(classlist[fields[SelectedClass]]);

                    ++indexRows;
                }
            }

            wv.ComputeVectors();

            List <string> strInputsTrain = new List <string>();
            List <string> strInputsTest  = new List <string>();

            double[][] IntInputsTrain = new double[indexRows][];

            int[] outputsTrain = new int[indexRows];

            double [][] IntInputs = new double[IntMatrixInputs.Count][];
            for (int i = 0; i < indexRows; ++i)
            {
                //IntInputs[i] = IntMatrixInputs[i].ToArray();
                System.Diagnostics.Debug.WriteLine("Creating input row: " + i);
                // IntInputsTrain[i] = wv.transform(stringInputs[i], comboBox4.SelectedItem.ToString()); //IntMatrixInputs[i].ToArray();
                strInputsTrain.Add(stringInputs[i]);
                outputsTrain[i] = IntOutputs[i];
            }

            if (comboBox2.SelectedItem.ToString() == "Levenshtein")
            {
                knnStr = new KNearestNeighbors <string>(k: Int32.Parse(textBox2.Text), classes: classlist.Count,
                                                        inputs: strInputsTrain.ToArray(), outputs: outputsTrain, distance: Distance.Levenshtein);
            }
            else if (comboBox2.SelectedItem.ToString() == "Euclidean")
            {
                knn = new KNearestNeighbors(k: Int32.Parse(textBox2.Text), classes: classlist.Count,
                                            inputs: IntInputsTrain, outputs: outputsTrain, distance: Distance.Euclidean);
            }
            else if (comboBox2.SelectedItem.ToString() == "Jaccard")
            {
                knn = new KNearestNeighbors(k: Int32.Parse(textBox2.Text), classes: classlist.Count,
                                            inputs: IntInputsTrain, outputs: outputsTrain, distance: Jaccard);
            }
            else
            {
                knn = new KNearestNeighbors(k: Int32.Parse(textBox2.Text), classes: classlist.Count,
                                            inputs: IntInputsTrain, outputs: outputsTrain, distance: Distance.Cosine);
            }


            int correctCount = 0;
            int wrongCount   = 0;

            List <int> expected  = new List <int>();
            List <int> predicted = new List <int>();

            int positiveValue = 1;
            int negativeValue = 0;

            tfp = new TextFieldParser(textBox8.Text);
            tfp.HasFieldsEnclosedInQuotes = true;
            tfp.Delimiters = delimiters;
            fields         = tfp.ReadFields();
            indexRows      = 0;

            string       outputPath = textBox8.Text.Replace(".csv", "_Labeled.csv");
            StreamWriter sw         = new StreamWriter(outputPath);

            sw.WriteLine("FirstName,LastName,Country");

            while (!tfp.EndOfData)
            {
                System.Diagnostics.Debug.WriteLine("Processing test row: " + indexRows);
                // Console.WriteLine("Processing row: " + indexRows);

                fields = tfp.ReadFields();
                if (fields[1] != "" && fields[2] != "")
                {
                    int answer;
                    if (comboBox2.SelectedItem.ToString() == "Levenshtein")
                    {
                        answer = knnStr.Compute(GetString(fields, " "));
                    }
                    else
                    {
                        answer = knn.Compute(wv.transform(GetString(fields, " "), comboBox4.SelectedItem.ToString()));
                    }

                    int tempClass = -1;

                    classlist.TryGetValue(fields[SelectedClass], out tempClass);
                    expected.Add(tempClass);
                    predicted.Add(answer);

                    if (answer == tempClass)
                    {
                        correctCount++;
                    }
                    else
                    {
                        wrongCount++;
                    }

                    ++indexRows;


                    sw.WriteLine(fields[1] + "," + fields[2] + "," + inverseClassList[answer]);
                }
                else
                {
                    sw.WriteLine(fields[1] + "," + fields[2] + ",Undefined");
                }
            }

            sw.Flush();
            sw.Close();

            ConfusionMatrix matrix = new ConfusionMatrix(predicted.ToArray(), expected.ToArray());

            GeneralConfusionMatrix matrixGen = new GeneralConfusionMatrix(classlist.Count, expected.ToArray(), predicted.ToArray());



            textBox3.Text  = DateTime.Now + "   " + "k: " + textBox2.Text + "   Distance: " + comboBox2.SelectedItem.ToString() + "   Vector: " + comboBox4.SelectedItem.ToString();
            textBox3.Text += "   Number of instances: " + indexRows + "    Number of classes: " + classlist.Count;
            textBox3.Text += "   Correctly classified: " + correctCount + "   Wrongly classified: " + wrongCount;
            textBox3.Text += "   Standard Error " + matrixGen.StandardError;
            textBox3.Text += "   Accuracy " + (double)((double)correctCount / (double)indexRows);
            //textBox3.Text += "   Conf. Matrix " + matrix;

            //textBox8.Text = "Rows " + matrixGen.Matrix.Rows();
            if (checkBox1.Checked)
            {
                paintMatrix(matrixGen, inverseClassList);
            }
            label8.Text = "-";
        }
        public string knn(DataTable tbl)
        {
            Codification codebook = new Codification(tbl);

            DataTable symbols = codebook.Apply(tbl);

            double[][] inputs = symbols.ToIntArray("Clump Thickness", "Uniformity of Cell Size", "Uniformity of Cell Shape", "Marginal Adhesion", "Single Epithelial Cell Size", "Bare Nuclei", "Bland Chromatin", "Normal Nucleoli", "Mitoses").ToDouble();
            int sayac = 0;

            int[] outputs = symbols.ToIntArray("Class").GetColumn(0);

            KNearestNeighbors knn = new KNearestNeighbors(k: 4, classes: 2,
            inputs: inputs, outputs: outputs);

            int answer = knn.Compute(new double[] { Convert.ToInt32(inputlar[0]), Convert.ToInt32(inputlar[1]),
                        Convert.ToInt32( inputlar[2]), Convert.ToInt32( inputlar[3]), Convert.ToInt32( inputlar[4]),
                        Convert.ToInt32( inputlar[5]), Convert.ToInt32( inputlar[6]), Convert.ToInt32( inputlar[7]), Convert.ToInt32( inputlar[8]) }); // answer will be 2.
            if (answer == 0)
                answer = 4;
            else
                answer = 2;

            return answer.ToString();
        }
示例#41
0
        public void KNearestNeighborConstructorTest()
        {
            double[][] inputs =
            {
                new double[] { -5, -2, -1 },
                new double[] { -5, -5, -6 },

                new double[] {  2,  1,  1 },
                new double[] {  1,  1,  2 },
                new double[] {  1,  2,  2 },
                new double[] {  3,  1,  2 },

                new double[] { 11,  5,  4 },
                new double[] { 15,  5,  6 },
                new double[] { 10,  5,  6 },
            };

            int[] outputs =
            {
                0, 0,
                1, 1, 1, 1,
                2, 2, 2
            };

            int k = 3;

            KNearestNeighbors target = new KNearestNeighbors(k, inputs, outputs);

            for (int i = 0; i < inputs.Length; i++)
            {
                int actual   = target.Compute(inputs[i]);
                int expected = outputs[i];

                Assert.AreEqual(expected, actual);
            }

            double[][] test =
            {
                new double[] { -4, -3, -1 },
                new double[] { -5, -4, -4 },

                new double[] {  5,  3,  4 },
                new double[] {  3,  1,  6 },

                new double[] { 10,  5,  4 },
                new double[] { 13,  4,  5 },
            };

            int[] expectedOutputs =
            {
                0, 0,
                1, 1,
                2, 2,
            };

            for (int i = 0; i < test.Length; i++)
            {
                int actual   = target.Compute(test[i]);
                int expected = expectedOutputs[i];

                Assert.AreEqual(expected, actual);
            }
        }
        private static void Main(string[] args)
        {
            if (args.Length < 3 || args.Length > 6)
            {
                Console.WriteLine(Help);

                return;
            }

            var parser = new EventsParser();

            IList <EventModel> events = null;

            try
            {
                events = parser.ParseFromCSV(args[0]);
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(Strings.WrongFileFormat);
                Console.WriteLine(Strings.Tweets);

                return;
            }

            if (events == null)
            {
                Console.WriteLine(Strings.FileNotFound);

                return;
            }

            ISymptomAlgorithm symtopmAlgorithm;
            IDetectionMethod  method;

            var isFastTest = false;

            switch (args[1].ToLower())
            {
            case "bow":

                symtopmAlgorithm = new BagOfWords();

                break;

            case "tf-idf":

                symtopmAlgorithm = new TermFrequencyInverseDocumentFrequency();

                break;

            case "ft":

                isFastTest = true;

                symtopmAlgorithm = new FastTest(args[2]);

                break;

            default:

                Console.WriteLine(Strings.SymptomAlgorithmError);
                Console.WriteLine(Strings.Bow);

                return;
            }

            switch (args[isFastTest ? 3 : 2].ToLower())
            {
            case "mdm":

                method = new MinimalDistanceMethod();

                break;

            case "knn":

                if (args.Length < 4 || !int.TryParse(args[isFastTest ? 4 : 3], out var k) || k <= 0)
                {
                    Console.WriteLine(Strings.PositiveIntegerError);
                    Console.WriteLine(Strings.KNN);

                    return;
                }

                method = new KNearestNeighbors(k);

                break;

            default:

                Console.WriteLine(Strings.DetectionMethodError);
                Console.WriteLine(Strings.MDM);

                return;
            }

            Console.WriteLine("Generating symtoms...");

            symtopmAlgorithm.GenerateSymptoms(events);

            Console.WriteLine("Symptoms generated successfully.");
            Console.WriteLine($"There are { events.Count(s => s.SymptomModel != null) } symptoms.");

            Console.WriteLine("\nMaking etalons...");

            var symptomLength = events[0].SymptomModel.Length;

            var etalons = new List <ClusterModel>();

            foreach (var e in events)
            {
                var exist = etalons.Where(etalon => e.EventType == etalon.Type).FirstOrDefault();

                if (exist != null)
                {
                    exist.Events.Add(e);
                    continue;
                }

                etalons.Add(new ClusterModel
                {
                    Type   = e.EventType,
                    Events = new List <EventModel> {
                        e
                    }
                });
            }

            etalons.ForEach(e => Console.WriteLine($"Created etalon { e.Type }"));

            Console.WriteLine("\nDetection...");

            var clusters = method.Detect(events, etalons);

            Console.WriteLine("Detecting done.");

            var output = args.Last();

            if (output.LastIndexOf('.') > 0 && output.Substring(output.LastIndexOf('.')).ToLower().CompareTo(".csv") == 0)
            {
                Console.WriteLine("\nWriting result into csv file...\n");

                var classifiedEvents = clusters.Select(c => c.Clone() as ClusterModel).ToList();

                classifiedEvents.ForEach(c => c.Events.ForEach(e => e.EventType = c.Type));

                try
                {
                    parser.ParseIntoCSV(output, classifiedEvents.SelectMany(c => c.Events.Select(e => e)).ToList());
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);

                    return;
                }
            }
            else
            {
                Console.WriteLine(Environment.NewLine + Strings.OutputError + Environment.NewLine);
            }

            Console.WriteLine("Results:");

            double correctResults = clusters.Sum(c => c.Events.Count(e => e.EventType == c.Type));

            double all = clusters.Sum(c => c.Events.Count);

            var precision = correctResults / all;

            var recall = (correctResults / Math.Abs((all - correctResults))) / 100;

            var fMeasure = (2 * (precision * recall)) / (precision + recall);

            Console.WriteLine($"Precision: { precision }\nRecall: { recall }\nF-measure: { fMeasure }\n");

            foreach (var c in clusters)
            {
                foreach (var e in c.Events)
                {
                    if (e.EventType != c.Type)
                    {
                        Console.WriteLine($"Cluster { c.Type } contain wrong classified { e.EventType }");
                    }
                }
            }
        }
示例#43
0
        //The KNN algorithm
        //It creates a knn library object, classifies the entire data set
        //and returns the confusion matrix for the classification
        private ConfusionMatrix RunKNN(int k, Double[][] trainingSet, int[] trainingOutput, Double[][] testSet, int[] expected)
        {
            //Create a knn classifer with 2 classes
            KNearestNeighbors knn = new KNearestNeighbors(k: k, classes: 2,
                inputs: trainingSet, outputs: trainingOutput);
            //Map the classifier over the test set
            //This wil return an array where index i is the classificatioon of the i-th vector
            //of the testSet
            var predicted = UtilityProvider.MergeArrays(trainingSet, testSet)
                            .Select(x => knn.Compute(x))
                            .ToArray();

            //For test, assume 0 as positive and 1 as negative
            int positive = 0;
            int negative = 1;

            //Create a new confusion matrix with the calculated parameters
            ConfusionMatrix cmatrix = new ConfusionMatrix(predicted, UtilityProvider.MergeArrays(trainingOutput, expected), positive, negative);
            return cmatrix;
        }
示例#44
0
        public void KNearestNeighbor_CrossValidation()
        {
            // Create some sample learning data. In this data,
            // the first two instances belong to a class, the
            // four next belong to another class and the last
            // three to yet another.

            double[][] inputs =
            {
                // The first two are from class 0
                new double[] { -5, -2, -1 },
                new double[] { -5, -5, -6 },

                // The next four are from class 1
                new double[] {  2,  1,  1 },
                new double[] {  1,  1,  2 },
                new double[] {  1,  2,  2 },
                new double[] {  3,  1,  2 },

                // The last three are from class 2
                new double[] { 11,  5,  4 },
                new double[] { 15,  5,  6 },
                new double[] { 10,  5,  6 },
            };

            int[] outputs =
            {
                0, 0,       // First two from class 0
                1, 1, 1, 1, // Next four from class 1
                2, 2, 2     // Last three from class 2
            };



            // Create a new Cross-validation algorithm passing the data set size and the number of folds
            var crossvalidation = new CrossValidation(size: inputs.Length, folds: 3);

            // Define a fitting function using Support Vector Machines. The objective of this
            // function is to learn a SVM in the subset of the data indicated by cross-validation.

            crossvalidation.Fitting = delegate(int k, int[] indicesTrain, int[] indicesValidation)
            {
                // The fitting function is passing the indices of the original set which
                // should be considered training data and the indices of the original set
                // which should be considered validation data.

                // Lets now grab the training data:
                var trainingInputs  = inputs.Submatrix(indicesTrain);
                var trainingOutputs = outputs.Submatrix(indicesTrain);

                // And now the validation data:
                var validationInputs  = inputs.Submatrix(indicesValidation);
                var validationOutputs = outputs.Submatrix(indicesValidation);

                // Now we will create the K-Nearest Neighbors algorithm. For this
                // example, we will be choosing k = 4. This means that, for a given
                // instance, its nearest 4 neighbors will be used to cast a decision.
                KNearestNeighbors knn = new KNearestNeighbors(k: 4, classes: 3,
                                                              inputs: inputs, outputs: outputs);


                // After the algorithm has been created, we can classify instances:
                int[] train_predicted = trainingInputs.Apply(knn.Compute);
                int[] test_predicted  = validationInputs.Apply(knn.Compute);

                // Compute classification error
                var    cmTrain     = new ConfusionMatrix(train_predicted, trainingOutputs);
                double trainingAcc = cmTrain.Accuracy;

                // Now we can compute the validation error on the validation data:
                var    cmTest        = new ConfusionMatrix(test_predicted, validationOutputs);
                double validationAcc = cmTest.Accuracy;

                // Return a new information structure containing the model and the errors achieved.
                return(new CrossValidationValues(knn, trainingAcc, validationAcc));
            };


            // Compute the cross-validation
            var result = crossvalidation.Compute();

            // Finally, access the measured performance.
            double trainingAccs   = result.Training.Mean;
            double validationAccs = result.Validation.Mean;


            Assert.AreEqual(1, trainingAccs);
            Assert.AreEqual(1, validationAccs);
        }