Esempio n. 1
0
File: kNN.cs Progetto: Lino3D/MSI2
        public static double HammingDistance(myVector a, myVector b)
        {
            List <double> v1 = a.GetVector();
            List <double> v2 = b.GetVector();

            int Distance = 0;

            if (v1.Count != v2.Count)
            {
                return(-1);
            }

            else
            {
                for (int i = 0; i < v1.Count; i++)
                {
                    if (v1[i] != v2[i])
                    {
                        Distance++;
                    }
                }
            }

            return(Distance);
        }
Esempio n. 2
0
        public void AddVector(List <string> words, string Name)
        {
            AddWords(words);
            myVector v = new myVector(Words, words, Name, StopWords);

            Vectors.Add(v);
        }
Esempio n. 3
0
File: kNN.cs Progetto: Lino3D/MSI2
        public static double CosineDistance(myVector a, myVector b)
        {
            List <double> v1       = a.GetVector();
            List <double> v2       = b.GetVector();
            double        Distance = 0;

            if (v1.Count != v2.Count)
            {
                return(-1);
            }
            else
            {
                double top     = 0;
                double bottom1 = 0;
                double bottom2 = 0;

                for (int i = 0; i < v1.Count(); i++)
                {
                    top     += v1[i] * v2[i];
                    bottom1 += Math.Pow(Math.Abs(v1[i]), 2);
                    bottom2 += Math.Pow(Math.Abs(v2[i]), 2);
                }

                Distance = top / (Math.Sqrt(bottom1) * Math.Sqrt(bottom2));
            }

            return(Distance);
        }
Esempio n. 4
0
        public void AddVector(string text, string Name)
        {
            var tmp = Helper.FormatText(text);

            AddWords(tmp);
            myVector v = new myVector(Words, tmp, Name, StopWords);

            Vectors.Add(v);
        }
Esempio n. 5
0
File: kNN.cs Progetto: Lino3D/MSI2
        public static int CalculateKNN(myVector V, List <DataClass> Classes, int k)
        {
            //  int minDistance = int.MaxValue;
            double distance = 0;
            List <List <double> > distances = new List <List <double> >();

            foreach (DataClass C in Classes)
            {
                List <myVector> Vectors       = C.GetVectors();
                List <double>   classDistance = new List <double>();

                foreach (myVector vector in Vectors)
                {
                    distance = CosineDistance(V, vector);
                    classDistance.Add(distance);
                }
                distances.Add(classDistance);
            }
            List <Neighbour> kNearest = new List <Neighbour>(k);

            foreach (List <double> cd in distances)
            {
                foreach (double d in cd)
                {
                    if (kNearest.Count < k)
                    {
                        kNearest.Add(new Neighbour(d, distances.IndexOf(cd)));
                    }
                    else
                    {
                        for (int i = 0; i < k; i++)
                        {
                            if (d > kNearest[i].Distance && (ContainCheck(kNearest, d, distances.IndexOf(cd)) == false))
                            {
                                kNearest[i].Distance = d;
                                kNearest[i].Id       = distances.IndexOf(cd);
                            }
                        }
                    }
                }
            }
            int[] best = new int[Classes.Count];
            for (int i = 0; i < best.Count(); i++)
            {
                best[i] = 0;
            }
            foreach (Neighbour N in kNearest)
            {
                best[N.Id]++;
            }
            int bestOption = best.ToList().IndexOf(best.Max());

            return(bestOption);
        }
Esempio n. 6
0
        public TestResult filltestData(List <DataClass> classes, int id, myVector v)
        {
            foundclass = classes[id].GetName();

            correctclass = v.GetVectorName();
            correctclass = correctclass.Remove(correctclass.Length - 2);

            if (correctclass.IndexOf('_') != -1)
            {
                correctclass = correctclass.Remove(correctclass.IndexOf('_'), 1);
            }

            if (correctclass == foundclass)
            {
                result = true;
            }

            return(this);
        }
Esempio n. 7
0
        // Tego nie uzywamy ostatecznie
        private static float CalculateError(Network net, myVector V, List <DataClass> classes)
        {
            var   outputLayer = net.getNetwork().Where(o => o.type == 2).ToList();
            int   id          = classes.Where(o => o.GetVectors().Contains(V)).First().GetID();
            float Error       = 0;

            for (int i = 0; i < outputLayer.Count(); i++)
            {
                if (id == i)
                {
                    Error += (float)0.5 * (float)Math.Pow(((double)1 - outputLayer[i].Input), 2);
                }
                else
                {
                    Error += (float)0.5 * (float)Math.Pow(((double)0 - outputLayer[i].Input), 2);
                }
            }
            //      int id = classes.Where(o => o.GetVectors().Contains(V));


            return(Error);
        }
Esempio n. 8
0
 public void AddVector(myVector V)
 {
     Vectors.Add(V);
 }
Esempio n. 9
0
 public DataClass(myVector x, string y)
 {
     Vectors.Add(x);
     ClassName = y;
     AssignId();
 }