예제 #1
0
        public Collection <Example> GetProof(SparseVector x)
        {
            Collection <Example> res = new Collection <Example>();

            List <ExampleDistancePair> list = new List <ExampleDistancePair>();

            foreach (Example e in m_t_set)
            {
                list.Add(new ExampleDistancePair(e, SparseVector.Distance(x, e.X)));
            }

            list.Sort();

            int[] votes = new int[m_catnum];
            for (int i = 0; i < votes.Length; i++)
            {
                votes[i] = 0;
            }

            for (int i = 0; i < this.m_k; i++)
            {
                ExampleDistancePair pair = list[i];
                res.Add(pair.Example);
            }

            return(res);
        }
예제 #2
0
        public override int Predict(SparseVector x)
        {
            int iResult;

            if (m_t_set.Count < this.m_k)
            {
                throw new ApplicationException("K great than training set count.");
            }

            List <ExampleDistancePair> list = new List <ExampleDistancePair>();

            foreach (Example e in m_t_set)
            {
                list.Add(new ExampleDistancePair(e, SparseVector.Distance(x, e.X)));
            }

            list.Sort();

            int[] votes = new int[m_catnum];
            for (int i = 0; i < votes.Length; i++)
            {
                votes[i] = 0;
            }

            for (int i = 0; i < this.m_k; i++)
            {
                ExampleDistancePair pair = list[i];
                votes[(pair.Example.Label.Id + 1) / 2]++;
            }

            int max   = 0;
            int index = -1;

            for (int i = 0; i < votes.Length; i++)
            {
                if (votes[i] > max)
                {
                    index = i;
                    max   = votes[i];
                }
            }

            iResult = index * 2 - 1;
            return(iResult);
        }
예제 #3
0
        public int CompareTo(object obj)
        {
            ExampleDistancePair other = (ExampleDistancePair)obj;

            return(Math.Sign(this.Distance - other.Distance));
        }