コード例 #1
0
ファイル: KNN.cs プロジェクト: foamliu/NPatternRecognizer
        public ExampleSet GetProof(SparseVector x)
        {
            ExampleSet res = new ExampleSet();

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

            foreach (Example e in m_t_set.Examples)
            {
                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.AddExample(pair.Example);
            }

            return(res);
        }
コード例 #2
0
ファイル: KNN.cs プロジェクト: foamliu/NPatternRecognizer
        public override int Predict(SparseVector x)
        {
            int iResult;

            if (m_t_set.Examples.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.Examples)
            {
                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
ファイル: KNN.cs プロジェクト: foamliu/NPatternRecognizer
        public int CompareTo(object obj)
        {
            ExampleDistancePair other = (ExampleDistancePair)obj;

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