knn c++ code changing

asked 2014-06-12 14:51:48 -0600

Hm gravatar image

i have c++ code for knn classifier i can use this code after self organizing map, it give me accuracy for classification but now i have group of vectors each has mean in my knn code: it take No of train image, No of test image, and som map dimension but with these vector: i have No of train vectors, No of test vectors, and its mean what about map dimension now how can i change this code in order to take these vectors and make classification with only its mean without self organizing map this is my code which work with som code and i need to work it without som:

#include "knn.h"
#include <string>

KNN::KNN(int Ntrn,int Ntest,int NF)
{

    NumTrn=Ntrn;
    NumTst=Ntest;
    NumFe=NF;
    TrainData=new int*[Ntrn];
    for(int i=0;i<Ntrn;i++)
        TrainData[i]=new int[NF];

    TestData=new int*[Ntest];
    for(int i=0;i<Ntest;i++)
        TestData[i]=new int[NF];

    Dist=new double*[Ntest];
    for(int i=0;i<Ntest;i++)
        Dist[i]=new double[Ntrn];

    SLabels=new int*[Ntest];
    for(int i=0;i<Ntest;i++)
        SLabels[i]=new int[Ntrn];

    TrainLabels=new int[Ntrn];
    TestLabels=new int[Ntest];
    index=new int[Ntest];

    DistLabels=new int[Ntrn];

    Count=new int*[Ntest];
    for(int i=0;i<Ntest;i++)
        Count[i]=new int[Ntrn];

    for(int i=0;i<Ntest;i++)
        for(int j=0;j<Ntrn;j++)
            Count[i][j]=0;

}

void KNN::LoadData(const char*TrnFile,const char*TstFile)
{
    ifstream Trn(TrnFile);
    ifstream Tst(TstFile);
    for(int i=0;i<NumTrn;i++)
    {
        Trn>>TrainLabels[i];
        for(int j=0;j<NumFe;j++)
            Trn>>TrainData[i][j];
    }
    for(int i=0;i<NumTst;i++)
    {
        Tst>>TestLabels[i];
        for(int j=0;j<NumFe;j++)
            Tst>>TestData[i][j];
    }

    //ofstream sam("name.txt");
    int k=0;
    for(int i=0;i<NumTrn;i++)
    {
        DistLabels[i]=k;
        //j=i+1;
        while(TrainLabels[i]==TrainLabels[i+1])
            {
                DistLabels[i++]=k;

        }k++;}

    NumClasses=k;

}



void KNN::Distance()
{
    ofstream Dis("Distance.trn");
    ofstream Lab("labels.trn");


    double sum;

    for(int i=0;i<NumTst;i++)
    {

        for(int j=0;j<NumTrn;j++)
        {
            sum=0.0;
            SLabels[i][j]=TrainLabels[j];
            for(int k=0;k<NumFe;k++)
                sum+=pow((double)(TestData[i][k]-TrainData[j][k]),2);
            Dist[i][j]=sqrt(sum);
            Dis<<Dist[i][j]<<"\t";
            Lab<<SLabels[i][j]<<"\t";
        }
        Dis<<endl;
        Lab<<endl;

    }
}
//Sort the array Dist using insertion sort in ascending order
void KNN::Sort()
{
    double keyd;
    int keyl;
    int k;
    ofstream Sor("sort.trn");
    for(int i=0;i<NumTst;i++)
    {
        for(int j=1;j<NumTrn;j++)
        {
            keyd=Dist[i][j];
            keyl=SLabels[i][j];//sorted labels
            k=j-1;
            while(k>=0&&Dist[i][k]>keyd)
            {
                Dist[i][k+1]=Dist[i][k];
                SLabels[i][k+1]=SLabels[i][k];
                k--;
            }
            Dist[i][k+1]=keyd;
            SLabels[i][k+1]=keyl;

        }

        for(int j=0;j<NumTrn;j++)
            Sor<<Dist[i][j]<<"\t";
        Sor<<endl ...
(more)
edit retag flag offensive close merge delete