KNN. h # pragma onceclass KNN {PRIVATE: Double ** trainingdataset; double * arithmeticmean; double * standarddeviation; int M, N; void rescaledistance (double * row); void rescaletrainingdataset (); void computearithmeticmean (); void computestandarddeviation (); double distance (double * X, double * Y); Public: KNN (double ** trainingdataset, int M, int N );~ KNN (); double vote (double * test, int K) ;}; KNN. cpp # include "KNN. H" # include
# Include
Using namespace STD; KNN: KNN (double ** trainingdataset, int M, int N) {This-> trainingdataset = trainingdataset; this-> M = m; this-> N = N; computearithmeticmean (); computestandarddeviation (); rescaletrainbow dataset ();} void KNN: computearithmeticmean () {arithmeticmean = new double [n-1]; double sum; For (INT I = 0; I: iterator Max; Map
Mins; For (INT I = 0; I: value_type (I, distance); else {max = mins. Begin (); For (Map
: Iterator it = mins. Begin (); it! = Mins. end (); It ++) {If (IT-> second> MAX-> second) max = it;} If (distance second) {mins. erase (max); mins. insert (Map
: Value_type (I, distance) ;}} Map
Votes; double temp; For (Map
: Iterator it = mins. Begin (); it! = Mins. End (); It ++) {temp = trainingdataset [it-> first] [n-1]; Map
: Iterator voteit = votes. Find (temp); If (voteit! = Votes. End () voteit-> second ++; else votes. insert (Map
: Value_type (temp, 1);} Map
: Iterator maxvote = votes. Begin (); For (Map
: Iterator it = votes. Begin (); it! = Votes. end (); It ++) {If (IT-> second> maxvote-> second) maxvote = it;} test [n-1] = maxvote-> first; return maxvote-> first;} Main. CPP # include
# Include "KNN. H "using namespace STD; int main (const Int & argc, const char * argv []) {double ** train = new double * [14]; for (INT I = 0; I