コード例 #1
0
ファイル: KNN.cs プロジェクト: foamliu/NComputerVision
        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
ファイル: KNN.cs プロジェクト: foamliu/NComputerVision
        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);
        }