KNN.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #ifndef KNN_HEADER_H
  2. #define KNN_HEADER_H
  3. using namespace std;
  4. typedef struct tagRoadKey
  5. {int x;
  6. int y;
  7. bool operator <(const tagRoadKey& other) const
  8. {
  9. if (x < other.x) //类型按升序排序
  10. {
  11. return true;
  12. }
  13. else if (x == other.x) //如果类型相同,按比例尺升序排序
  14. {
  15. if (y < other.y) //类型按升序排序
  16. {
  17. return true;
  18. }
  19. else
  20. {
  21. return false;
  22. }
  23. }
  24. return false;
  25. }
  26. }Point;
  27. typedef Point tLabel;
  28. typedef double tData;
  29. typedef pair<int,double> PAIR;
  30. const int colLen = 3;
  31. const int rowLen = 13000;
  32. extern ifstream fin;
  33. extern ofstream fout;
  34. class KNN
  35. {
  36. private:
  37. tData dataSet[rowLen][colLen];
  38. tLabel labels[rowLen];
  39. tData testData[colLen];
  40. int k;
  41. map<int,double> map_index_dis;
  42. map<tLabel,int> map_label_freq;
  43. double get_distance(tData *d1,tData *d2);
  44. public:
  45. KNN(int magic_x, int magic_y, int magic_z);
  46. void get_all_distance();
  47. tLabel get_max_freq_label();
  48. struct CmpByValue
  49. {
  50. bool operator() (const PAIR& lhs,const PAIR& rhs)
  51. {
  52. return lhs.second < rhs.second;
  53. }
  54. };
  55. };
  56. #endif