コード例 #1
0
ファイル: Algorithm.cs プロジェクト: nvnhcmus/kNN
        public void setResponse( Customer cus )
        {
            //normalize cus
            Normalize ncus = new Normalize(cus, maxAge, maxIncome, maxNumCard);

            //calculate all distances
            for (int i = 0; i < this.totalTrainset; i++)
            {
                distances[i] = new Distance();
                distances[i].distance = 0;
                distances[i].index = i;

                //normalize element
                Normalize tmp = new Normalize(this.trainset[i], maxAge, maxIncome, maxNumCard);

                //distance between two age normalized
                distances[i].distance = distances[i].distance + getDistance( ncus.age , tmp.age);

                //distance between two gender
                distances[i].distance = distances[i].distance + getDistance(cus.getGender(), this.trainset[i].getGender());

                //distance between two incoming normalized
                distances[i].distance = distances[i].distance + getDistance(ncus.incoming, tmp.incoming);

                //distance between two number of card normalized
                distances[i].distance = distances[i].distance + getDistance( ncus.numCard, tmp.numCard);

            }// end loop

            //sort
            for (int i = 0; i < totalTrainset - 1; i++)
            {
                for (int j = i + 1; j < totalTrainset; j++)
                {
                    if (distances[i].distance > distances[j].distance)
                    {
                        Distance tmp = distances[i];
                        distances[i] = distances[j];
                        distances[j] = tmp;
                    }//swap
                }//end j loop

            }//end i loop

            //select k nearest neighbor
            int yesCount = 0;
            int noCount = 0;

            for (int i = 0; i < kNN; i++)
            {
                Customer tmp = trainset[ distances[i].index ];
                if (tmp.getResponse() == 0)
                {
                    noCount = noCount + 1;
                }
                else if ( tmp.getResponse() == 1 )
                {
                    yesCount = yesCount + 1;
                }
            }

            //set response value for unknown customer
            if (yesCount > noCount)
            {
                cus.setResponse(1);
            }
            else if (yesCount < noCount)
            {
                cus.setResponse(0);
            }
        }
コード例 #2
0
        public void setResponse(Customer cus)
        {
            //normalize cus
            Normalize ncus = new Normalize(cus, maxAge, maxIncome, maxNumCard);


            //calculate all distances
            for (int i = 0; i < this.totalTrainset; i++)
            {
                distances[i]          = new Distance();
                distances[i].distance = 0;
                distances[i].index    = i;

                //normalize element
                Normalize tmp = new Normalize(this.trainset[i], maxAge, maxIncome, maxNumCard);


                //distance between two age normalized
                distances[i].distance = distances[i].distance + getDistance(ncus.age, tmp.age);

                //distance between two gender
                distances[i].distance = distances[i].distance + getDistance(cus.getGender(), this.trainset[i].getGender());

                //distance between two incoming normalized
                distances[i].distance = distances[i].distance + getDistance(ncus.incoming, tmp.incoming);

                //distance between two number of card normalized
                distances[i].distance = distances[i].distance + getDistance(ncus.numCard, tmp.numCard);
            }// end loop


            //sort
            for (int i = 0; i < totalTrainset - 1; i++)
            {
                for (int j = i + 1; j < totalTrainset; j++)
                {
                    if (distances[i].distance > distances[j].distance)
                    {
                        Distance tmp = distances[i];
                        distances[i] = distances[j];
                        distances[j] = tmp;
                    } //swap
                }     //end j loop
            }         //end i loop



            //select k nearest neighbor
            int yesCount = 0;
            int noCount  = 0;

            for (int i = 0; i < kNN; i++)
            {
                Customer tmp = trainset[distances[i].index];
                if (tmp.getResponse() == 0)
                {
                    noCount = noCount + 1;
                }
                else if (tmp.getResponse() == 1)
                {
                    yesCount = yesCount + 1;
                }
            }



            //set response value for unknown customer
            if (yesCount > noCount)
            {
                cus.setResponse(1);
            }
            else if (yesCount < noCount)
            {
                cus.setResponse(0);
            }
        }