# include & ltcmath & gt
# include & ltfstream & gt
Use namespace std
# Define Number of Attributes 5// Number of Attributes
# define the maximum size of MAXSZ 1700// training set.
# define max value10000.0//The maximum attribute value is lower than 10000(int).
# Define K 5
Structure vector {
Double attribute [nattrs];
Double classlabel
};
Structural project {
Double distance;
Double classlabel
};
struct vector trSet[MAXSZ]; //Global variable, training set
Structure item KNN [k]; //Global variable, k-near east-neighborhood set
int curTSize = 0; //Current size of the training set
Int AddtoTSet (structure vector v)
{
if(curTSize & gt; =MAXSZ) {
Cout & lt& ltendl & lt& lt training set has "<& ltMAXSZ & lt& lt examples!" & lt& ltendl & lt& ltendl
Returns 0;
}
trSet[curTSize]= v;
curtsize++;
Returns1;
}
Double precision distance (structure vector v 1, structure vector v2)
{
Double d = 0.0
Double tem = 0.0
for(int I = 0; I & ltNATTRSi++)
tem+=(v 1 . attributes[I]-v2 . attributes[I])*(v 1 . attributes[I]-v2 . attributes[I]);
d = sqrt(tem);
Return d;
}
Int max(struct item KNN[])// Returns the number of the item with the largest distance (
//should be replaced)
{
int maxNo = 0;
if(K & gt; 1)
for(int I = 1; I & ltk;; i++)
if(knn[i])。 Distance & gtknn[maxNo]. Distance)
maxNo = I;
Return to maxNo
} doubleclassify (struct vector v)//Decide which class to assign the label to.
//Given input vector using knn method
{
double DD = 0;
int maxn = 0;
int freq[K];
double MFR eqc = 0; //Category labels appear most frequently.
int I;
for(I = 0; I & ltk;; i++)
knn[i]。 Distance = MAXVALUE
for(I = 0; I & ltcurTSizei++)
{
Dd = distance (trSet[i], v);
maxn = max(KNN); //maxn should be updated for each new state of the training set.
if(DD & lt; knn[maxn]。 Distance)
knn[maxn]。 Distance = dd
knn[maxn]。 classlabel = trSet[i]。 classlabel
}
}
for(I = 0; I & ltk;; I++)//freq[i] stands for knn[i]. How many times does classlabel appear?
freq[I]= 1;
for(I = 0; I & ltk;; i++)
for(int j = 0; j & ltk; j++)
If ((I! = j)& amp; & amp(knn[i]。 classlabel == knn[j]。 classlabel))
freq[I]+= 1;
int MFR eq = 1;
mfreqC = knn[0]。 classlabel
for(I = 0; I & ltk;; i++)
if(freq[I]& gt; mfreq) {
MFR eq = freq[I]; //mfreq stands for the most frequencies.
mfreqC = knn[i]。 Classlabel//mfreqNo is the most frequent item number.
//classlabel
}
Return mfreqC
}
void main()
{ double classlabel
Double c;
Double n;
Structure vector trExmp
int I;
ifstream filein(" G:\ \ data \ \ for KNN \ \ data . txt ");
if(filein . fail()){ cout & lt; & lt "Unable to open data. txt”& lt;; & ltendl returned; }
And (! filein.eof()) {
filein & gt& gtc;
trex MP . class label = c;
cout & lt& lttrex MP . class label & lt; & lt" "; for(int I = 0; I & ltNATTRSi++) {
filein & gt& gtn;
trex MP . attributes[I]= n;
cout & lt& lttrex MP . attributes[I]& lt; & lt" ";
} cout & lt& ltendl
If (! AddtoTSet(trExmp))
Break;
} filein . close(); Structure vector testv={{ 142, 188, 1 1,159, 0.5513196.
class label = Classify(testv);
The category of cout & lt& lt" testv is: ";
cout & lt& ltclasslabel & lt& ltendl
for(I = 0; I & ltk;; i++)
cout & lt& ltknn[i]。 Distance & lt& lt" \ t "<& ltknn[i]. classlabel & lt& ltendl
//cout & lt; & ltmax(KNN);
}