rakha asyrofi
/
KNN_coba7
mantap
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 //#include<iostream> 00002 //#include <stdio.h> 00003 #include<math.h> 00004 #include<stdlib.h> 00005 #include "mbed.h" 00006 #include "Serial.h" 00007 #include <algorithm> 00008 Serial uart1(USBTX,USBRX); 00009 #define MAX 20 00010 #define k 4 00011 using namespace std; 00012 enum category{SHORT,TALL,AVERAGE}; 00013 class data{ 00014 int x,y; 00015 category cat; 00016 public: 00017 void setd(int a,int b,category c){ 00018 x=a; 00019 y=b; 00020 cat=c; 00021 } 00022 int getx(){return x;} 00023 int gety(){return y;} 00024 category getcat(){ 00025 return cat; 00026 } 00027 };//end of class 00028 int dis(data d1,data d2) 00029 { 00030 //return sqrt(pow((d2.getx()-d1.getx()),2)+pow((d2.gety()-d1.gety()),2)); 00031 return sqrt(pow(((double)d2.getx()-(double)d1.getx()),2.0)+pow(((double)d2.gety()-(double)d1.gety()),2.0)); 00032 } 00033 int main() 00034 { 00035 uart1.baud(9600); 00036 do{ 00037 int p,q; //input 00038 int a[MAX]; //store distances 00039 int b[k]; //to get min distances, used in calc 00040 int c[k]; //to store freq 00041 for(int i=0;i<k;i++){ //initiLIZATION 00042 b[i]=-1; 00043 c[i]=0; 00044 } int min=1000; 00045 //cout<<"Enter x,y(negative value to exit): "; 00046 uart1.printf("Enter x,y(negative value to exit):"); 00047 //cin>>p>>q; 00048 uart1.scanf("%ld,%ld",&p,&q); 00049 if((p < 0) | (q < 0)) 00050 exit(0); 00051 data n; //data point to classify 00052 n.setd(p,q,SHORT); 00053 data d[MAX]; //training set 00054 00055 d[0].setd(1,1,SHORT); 00056 d[1].setd(1,2,SHORT); 00057 d[2].setd(1,3,SHORT); 00058 d[3].setd(1,4,SHORT); 00059 d[4].setd(1,5,SHORT); 00060 d[5].setd(1,6,SHORT); 00061 d[6].setd(1,7,SHORT); 00062 d[7].setd(2,1,SHORT); 00063 d[8].setd(2,2,SHORT); 00064 d[9].setd(2,3,AVERAGE); 00065 d[10].setd(2,4,AVERAGE); 00066 d[11].setd(2,5,AVERAGE); 00067 d[12].setd(2,6,AVERAGE); 00068 d[13].setd(2,7,AVERAGE); 00069 d[14].setd(5,1,TALL); 00070 d[15].setd(5,2,TALL); 00071 d[16].setd(5,3,TALL); 00072 d[17].setd(5,4,TALL); 00073 d[18].setd(5,5,TALL); 00074 d[19].setd(5,6,TALL); 00075 for(int i=0;i<20;i++){ 00076 a[i]=dis(n,d[i]); 00077 //cout<<"\t\t"<<a[i]<<endl; 00078 uart1.printf("\t\t %d\n", a[i]); 00079 } 00080 //k-nearest neighbours calculation i.e smallest k distances 00081 for(int j=0;j<k;j++) 00082 { 00083 min=1000; 00084 for(int i=0;i<20;i++) 00085 { 00086 if(i!=b[0]&&i!=b[1]&&i!=b[2]) 00087 { 00088 if((a[i]<=min)) 00089 { 00090 min=a[i]; 00091 b[j]=i; 00092 } 00093 } 00094 } 00095 //cout<<min<<endl; 00096 uart1.printf("%d\n",min); 00097 } 00098 //counting frequency of a class in each neighbour 00099 for(int i=0;i<k;i++) 00100 { 00101 switch (d[b[i]].getcat()) 00102 { 00103 case SHORT: 00104 c[0]++; 00105 break; 00106 case AVERAGE: 00107 c[2]++; 00108 break; 00109 case TALL: 00110 c[1]++; 00111 break; 00112 } 00113 } //counting max frequency 00114 int max=-1,j; 00115 for(int i=0;i<k;i++) 00116 { 00117 if(c[i]>max){ 00118 max=c[i]; 00119 j=i; 00120 } 00121 } 00122 //cout<<"Prediction is : "; 00123 printf("Prediction is:"); 00124 switch (j) 00125 { 00126 case 0: 00127 //cout<<"SHORT"<<endl; 00128 uart1.printf("SHORT\n"); 00129 break; 00130 case 1: 00131 //cout<<"TALL"<<endl; 00132 uart1.printf("TALL\n"); 00133 break; 00134 case 2: 00135 //cout<<"AVERAGE"<<endl; 00136 uart1.printf("AVERAGE\n"); 00137 break; 00138 } 00139 }while(true); 00140 return 0; 00141 }
Generated on Sun Jul 17 2022 04:45:38 by 1.7.2