Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have downloaded knn c++ code. This code takes two text files, train file and test file. Each has vectors and its labels. The vector in text file is look like: 1.2,2.2,5.6,....,1 2.1,1,5,5.5,....,1 1.2,2.5,3.6,....,2. The last index in vector is the label for this vector. The code read the data from these files with the function below:
bool readData4File (char *filename, TRAINING_EXAMPLES_LIST *rlist, 
                    int *rlistExamples)
{
    FILE *fp = NULL;
    int len = 0;
    char line[LINE_MAX+1];
    int lineSize = LINE_MAX;
    TrainingExample *TEObj;
    int index = 0;
    int numExamples = 0;

    *rlistExamples = 0;

    line[0] = 0;

    if((fp = fopen (filename, "r")) == NULL)
    {
        cout<<"Error in opening file."<<endl;
        return false;
    }

    //Initialize weights to random values
    srand (time(NULL));

    char *tmp;
    int tmpParams = 0; //NO_OF_ATT;
    int i = 0;
    double cd = 0.0;

    /* Read the data file line by line */
    while((len = GetLine (line, lineSize, fp))!=0) 
    {
        TEObj = new TrainingExample ();
        tmp = strtok (line,",");
        while (tmp != NULL)
        {
            cd = atof (tmp);
            TEObj->Value[tmpParams] = cd;
            tmpParams ++;

            tmp = strtok (NULL, ",");

            if(tmpParams == NO_OF_ATT)
            {
                tmpParams = 0;
                cd = 0.0;
                line[0] = 0;
                numExamples ++;

                //Not using this normalization anymore. 
                // N(y) = y/(1+y)
                // Doing normalization by standard deviation and mean
                //TEObj->NormalizeVals ();

                /* Generating random weights for instances. */
                /* These weights are used in instance WKNN  */
                double rno = (double)(rand () % 100 + 1);
                TEObj->Weight = rno/100;
                TEObj->index = index++;
                TEObj->isNearest2AtleastSome = false;
                break;
            }
        }

        rlist->insert (rlist->end(), *TEObj);

        delete TEObj;
    }

Then the code send the list of vectors in order to classify test vectors. I have 1405 train vector and 810 test vector. When running the code read the number of vectors as 1405 and 810 but the list of training and text vectors has 2810 and 1620 index what is the content of these lists and why they have the double number of train and test vectors! my result is 183% for accuracy as the code send the list not the examples why!
Posted
Updated 29-Sep-14 14:40pm
v2
Comments
[no name] 29-Sep-14 19:18pm    
http://www.codeproject.com/Questions/822834/i-have-an-error-with-k-nearest-neighborhood?arn=0
Sergey Alexandrovich Kryukov 29-Sep-14 19:23pm    
This is you who downloaded the code sample, who know from what source. Why anyone would dig in it? Why not asking the author?
If you write your own code and ask for help, we will gladly try to help. At least you will be considered to be responsible for the code, will probably answer questions, and so on.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900