classify.cpp
上传用户:ccczzzlll
上传日期:2015-04-06
资源大小:15k
文件大小:3k
源码类别:

控制台编程

开发平台:

Visual C++

  1. #include <iostream.h>
  2. #include <math.h>
  3. #include <fstream.h>
  4. #define  NATTRS 5 //number of attributes
  5. #define  MAXSZ  1700 //max size of training set
  6. #define  MAXVALUE  10000.0 //the biggest attribute's value is below 10000(int)
  7. #define  K  5   
  8. struct vector {
  9. double attributes[NATTRS];
  10. double classlabel;
  11. };
  12. struct item {
  13. double distance;
  14. double classlabel;
  15. };
  16. struct vector trSet[MAXSZ];//global variable,the training set
  17. struct item knn[K];//global variable,the k-neareast-neighbour set
  18. int curTSize = 0; //current size of the training set
  19. int AddtoTSet(struct vector v)
  20. {
  21. if(curTSize>=MAXSZ) {
  22. cout<<endl<<"The training set has "<<MAXSZ<<" examples!"<<endl<<endl; 
  23. return 0;
  24. }
  25. trSet[curTSize] = v;
  26. curTSize++;
  27. return 1;
  28. }
  29. double Distance(struct vector v1,struct vector v2)
  30. {
  31. double d = 0.0;
  32. double tem = 0.0;
  33. for(int i = 0;i < NATTRS;i++)
  34. tem += (v1.attributes[i]-v2.attributes[i])*(v1.attributes[i]-v2.attributes[i]);
  35. d = sqrt(tem);
  36. return d;
  37. }
  38. int max(struct item knn[]) //return the no. of the item which has biggest distance(
  39.                            //should be replaced) 
  40. {
  41. int maxNo = 0;
  42. if(K > 1)
  43. for(int i = 1;i < K;i++)
  44. if(knn[i].distance>knn[maxNo].distance)
  45. maxNo = i;
  46.     return maxNo;
  47. }
  48. double Classify(struct vector v)//decide which class label will be assigned to
  49.                              //a given input vetor with the knn method
  50. {
  51. double dd = 0;
  52. int maxn = 0;
  53. int freq[K];
  54. double mfreqC = 0;//the class label appears most frequently 
  55. int i;
  56. for(i = 0;i < K;i++)
  57. knn[i].distance = MAXVALUE;
  58. for(i = 0;i < curTSize;i++)
  59. {
  60. dd = Distance(trSet[i],v);
  61. maxn = max(knn);//for every new state of the training set should update maxn
  62. if(dd < knn[maxn].distance) {
  63. knn[maxn].distance = dd;
  64. knn[maxn].classlabel = trSet[i].classlabel;
  65. }
  66. }
  67. for(i = 0;i < K;i++)//freq[i] represents knn[i].classlabel appears how many times 
  68. freq[i] = 1;
  69. for(i = 0;i < K;i++)  
  70. for(int j = 0;j < K;j++)
  71. if((i!=j)&&(knn[i].classlabel == knn[j].classlabel))
  72. freq[i]+=1;
  73. int mfreq = 1;
  74. mfreqC = knn[0].classlabel;
  75. for(i = 0;i < K;i++)
  76. if(freq[i] > mfreq)  {
  77. mfreq = freq[i];//mfreq represents the most frepuences
  78.   mfreqC = knn[i].classlabel; //mfreqNo is the item no. with the most frequent
  79.                              //classlabel
  80. }
  81. return mfreqC;
  82. }
  83. void main()
  84. {   
  85. double classlabel;
  86. double c; 
  87. double n;
  88. struct vector trExmp;
  89. int i;
  90. ifstream filein("E:\knn\data.txt");
  91. if(filein.fail()){cout<<"Can't open data.txt"<<endl; return;}
  92. while(!filein.eof()) 
  93. {
  94. filein>>c;
  95. trExmp.classlabel = c;
  96. cout<<trExmp.classlabel<<" ";
  97. for(int i = 0;i < NATTRS;i++) 
  98. {
  99. filein>>n;
  100. trExmp.attributes[i] = n;
  101. cout<<trExmp.attributes[i]<<" ";
  102. }
  103. cout<<endl;
  104.  if(!AddtoTSet(trExmp))
  105. break;
  106. }
  107. filein.close();
  108. struct vector testv={{1,18,11,11,0.5513196},17};
  109. classlabel = Classify(testv);
  110. cout<<"The classlable of the testv is: ";
  111. cout<<classlabel<<endl;
  112. for(i = 0;i < K;i++)
  113. cout<<knn[i].distance<<"t"<<knn[i].classlabel<<endl;
  114. //cout<<max(knn);
  115. }