Ejemplo n.º 1
0
        public static Dictionary <int, string> KnnCreate(Dictionary <List <string>, double[][]> trainingSet)
        {
            // Create some sample learning data.
            int        labelCounter           = -1;
            List <int> classesList            = new List <int>();
            Dictionary <int, string> labelMap = new Dictionary <int, string>();

            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: 4 for the possibility of better accuracy
            var knn = new KNearestNeighbors(k: 5);

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

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

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

            Console.WriteLine("error: " + error);
            Console.WriteLine("accuracy: " + acc);
            Console.WriteLine("kappa: " + kappa);
            Console.WriteLine("pearson: " + cm.Pearson);
            for (int i = 0; i < cm.ColumnErrors.Length; i++)
            {
                if (cm.ColumnErrors[i] != 0)
                {
                    double columnerror = double.Parse(cm.ColumnErrors[i].ToString()) / double.Parse(cm.ColumnTotals[i].ToString());
                    Console.WriteLine("Error of " + labelMap[i] + ": " + columnerror);
                }
            }
            SaveKnn(knn);
            Fingerprinting.WriteLabelMap(labelMap);
            return(labelMap);
        }
Ejemplo n.º 2
0
        public static List <string> getOptions(double[] coordinates, KNearestNeighbors knn)
        {
            List <string>            options  = new List <string>();
            Dictionary <int, string> labelMap = Fingerprinting.ReadLabelMap();

            int[] list = labelMap.Keys.ToArray();
            foreach (double[] g in knn.GetNearestNeighbors(coordinates, out list))
            {
                options.Add(getRoomname(knn.Decide(g)));
            }
            return(options);
        }
Ejemplo n.º 3
0
        public static string getRoomname(int roomInt)
        {
            Dictionary <int, string> labelMap = Fingerprinting.ReadLabelMap();

            return(labelMap[roomInt]);
        }