1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
int maximum(int a,int b,int c){
int max = a;
if(max < b)
max = b;
if(max < c)
max = c;
return max;}
int main()
{float firstfeature,secondfeature,thirdfeature,forthfeature,sınıf, mindist=100000,diff,allvariables;
vector<float> list;
vector<float> first;
vector<float>second;
vector<float>third;
vector<float>forth;
vector<float>clas;
vector<float>difflist;
vector<float>sorteddiff;
ifstream myfile ("iristrain.txt");
ofstream yourfile;
yourfile.open("nearest.txt");
ofstream herfile;
herfile.open("thirdnearest.txt");
ofstream hisfile;
hisfile.open("fifthnearest.txt");
ofstream itsfile;
itsfile.open("seventhnearest.txt");
int j,i,neighbour;
for( j=0;j<375;j++)
{ myfile >> allvariables;
list.push_back(allvariables);}//reading datas from file and sending to vector
for( j=0;j<375;j++)
{ int div,mod=j%5;
div=j/5+1;
if(mod==0){
firstfeature=list.at(j) ;
first.push_back(firstfeature) ; }
if(mod==1){
secondfeature = list.at(j) ;
second.push_back(secondfeature);}
if(mod==2){
thirdfeature= list.at(j);
third.push_back (thirdfeature);}
if(mod==3){
forthfeature= list.at(j);
forth.push_back (forthfeature);}
if(mod==4){
sınıf= list.at(j);
clas.push_back (sınıf);} }//sorting datas as 1st 2nd 3rd 4th and classes.
for(i=0;i<75;i++){
for(j=0;j<75;j++){
diff=sqrt(pow((first.at(i)-first.at(j)),2)+pow((second.at(i)-second.at(j)),2)+pow((third.at(i)-third.at(j)),2)+pow((forth.at(i)-forth.at(j)),2));
if(i==j)
diff=100000;
difflist.push_back(diff);}//findig distances and listing them
sorteddiff=difflist;
std::sort(sorteddiff.begin(), sorteddiff.end());
float smallest,secondsmallest,thirdsmallest,forthsmallest,fifthsmallest,sixthsmallest,seventhsmallest;
int nearrow,secondnearrow,thirdnearrow,forthnearrow,fifthnearrow,sixthnearrow,seventhnearrow;
smallest=sorteddiff.at(0);
secondsmallest=sorteddiff.at(1);
thirdsmallest=sorteddiff.at(2);
forthsmallest=sorteddiff.at(3);
fifthsmallest=sorteddiff.at(4);
sixthsmallest=sorteddiff.at(5);
seventhsmallest=sorteddiff.at(6);
for(j=0;j<75;j++){
if(smallest==difflist.at(j))
nearrow=j;
if(secondsmallest==difflist.at(j))
secondnearrow=j;
if(thirdsmallest==difflist.at(j))
thirdnearrow=j;
if(forthsmallest==difflist.at(j))
forthnearrow=j;
if(fifthsmallest==difflist.at(j))
fifthnearrow=j;
if(sixthsmallest==difflist.at(j))
sixthnearrow=j;
if(seventhsmallest==difflist.at(j))
seventhnearrow=j;
}
//k=1;
yourfile << clas.at(nearrow) << "\n";
//k=3;
int countone=0;
int counttwo=0;
int countthree=0;
if(clas.at(nearrow)==1)
countone++;
if(clas.at(nearrow)==2)
counttwo++;
if(clas.at(nearrow)==3)
countthree++;
if(clas.at(secondnearrow)==1)
countone++;
if(clas.at(secondnearrow)==2)
counttwo++;
if(clas.at(secondnearrow)==3)
countthree++;
if(clas.at(thirdnearrow)==1)
countone++;
if(clas.at(thirdnearrow)==2)
counttwo++;
if(clas.at(thirdnearrow)==3)
countthree++;
int max=maximum(countone,counttwo,countthree);
if(max==countone)
herfile <<"1"<< "\n";
if(max==counttwo)
herfile <<"2"<< "\n";
if(max==countthree)
herfile <<"3"<< "\n";
//k=5;
if(clas.at(forthnearrow)==1)
countone++;
if(clas.at(forthnearrow)==2)
counttwo++;
if(clas.at(forthnearrow)==3)
countthree++;
if(clas.at(fifthnearrow)==1)
countone++;
if(clas.at(fifthnearrow)==2)
counttwo++;
if(clas.at(fifthnearrow)==3)
countthree++;
max=maximum(countone,counttwo,countthree);
if(max==countone)
hisfile <<"1"<< "\n";
if(max==counttwo)
hisfile <<"2"<< "\n";
if(max==countthree)
hisfile <<"3"<< "\n";
//k=7;
if(clas.at(sixthnearrow)==1)
countone++;
if(clas.at(sixthnearrow)==2)
counttwo++;
if(clas.at(sixthnearrow)==3)
countthree++;
if(clas.at(seventhnearrow)==1)
countone++;
if(clas.at(seventhnearrow)==2)
counttwo++;
if(clas.at(seventhnearrow)==3)
countthree++;
max=maximum(countone,counttwo,countthree);
if(max==countone)
itsfile <<"1"<< "\n";
if(max==counttwo)
itsfile <<"2"<< "\n";
if(max==countthree)
itsfile <<"3"<< "\n";
mindist=100000;
difflist.clear();}
}
|