Guest User

Untitled

a guest
Mar 11th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #include <mlpack/core.hpp>
  2. #include <mlpack/methods/neighbor_search/neighbor_search.hpp>
  3. #include <mlpack/methods/hdbscan/hdbscan.hpp>
  4. #include <mlpack/core/tree/cover_tree.hpp>
  5. #include <iostream>
  6. #include <math.h>
  7.  
  8. using namespace mlpack;
  9. using namespace mlpack::neighbor; // NeighborSearch and NearestNeighborSort
  10. using namespace mlpack::metric;
  11. using namespace mlpack::hdbscan;
  12. using namespace std;
  13.  
  14. // HDBSCAN metric is defined as :
  15. template<>
  16. template<typename VecTypeA, typename VecTypeB>
  17. double HdbscanDistance::Evaluate(
  18. const VecTypeA& a,
  19. const VecTypeB& b)
  20. {
  21. double dcoreA = a(a.n_rows-1);
  22. double dcoreB = b(b.n_rows-1);
  23. typename VecTypeB::elem_type dist = sqrt(arma::accu(arma::square(a - b) - (dcoreA - dcoreB)*(dcoreA - dcoreB)));
  24.  
  25. double temp = std::max(std::max(dcoreA, dcoreB), dist);
  26. return temp;
  27. }
  28.  
  29. int main()
  30. {
  31. arma::mat dataSynthetic1;
  32.  
  33. data::Load("manyPatches.csv", dataSynthetic1, true);
  34. dataSynthetic1.resize(dataSynthetic1.n_rows-1, dataSynthetic1.n_cols);
  35. neighbor::NeighborSearch<neighbor::NearestNeighborSort , metric::EuclideanDistance> neighborSearch;
  36. arma::Mat<size_t> neighbors;
  37. arma::mat distances;
  38. neighborSearch.Train(dataSynthetic1);
  39. neighborSearch.Search(5, neighbors, distances);
  40. arma::mat dataWithDcore = arma::conv_to<arma::mat>::from(dataSynthetic1);
  41. dataWithDcore.resize(dataWithDcore.n_rows+1, dataWithDcore.n_cols);
  42. dataWithDcore.row(dataWithDcore.n_rows-1) = distances.row(distances.n_rows-1);
  43.  
  44. std::cout<<"\nPoint 1741 is : \n";
  45. dataWithDcore.col(1741).print();
  46. std::cout<<"\nPoint 1734 is : \n";
  47. dataWithDcore.col(1734).print();
  48. std::cout<<"\nPoint 1729 is : \n";
  49. dataWithDcore.col(1729).print();
  50. std::cout<<"\nPoint 1731 is : \n";
  51. dataWithDcore.col(1731).print();
  52. std::cout<<"\nPoint 1744 is : \n";
  53. dataWithDcore.col(1744).print();
  54. std::cout<<"\nPoint 1727 is : \n";
  55. dataWithDcore.col(1727).print();
  56. std::cout<<"\nResultant mst is : \n";
  57.  
  58. metric::HdbscanDistance h;
  59. emst::DualTreeBoruvka<metric::HdbscanDistance, arma::mat, tree::KDTree> dtb(dataWithDcore, false, h);
  60. arma::mat resultsMST;
  61. dtb.ComputeMST(resultsMST);
  62. resultsMST = trans(resultsMST);
  63. resultsMST.print();
  64. }
  65.  
  66. First few lines of output are:
  67. Point 1741 is :
  68. 3.0503e+02
  69. 4.0717e+02
  70. 2.5126e+00
  71.  
  72. Point 1734 is :
  73. 3.0529e+02
  74. 4.0621e+02
  75. 2.4534e+00
  76.  
  77. Point 1729 is :
  78. 3.0286e+02
  79. 4.0592e+02
  80. 2.5126e+00
  81.  
  82. Point 1731 is :
  83. 3.0332e+02
  84. 4.0598e+02
  85. 2.5770e+00
  86.  
  87. Point 1744 is :
  88. 3.0471e+02
  89. 4.0751e+02
  90. 2.6024e+00
  91.  
  92. Point 1727 is :
  93. 3.0600e+02
  94. 4.0525e+02
  95. 3.2150e+00
  96.  
  97. Distance between 1729 and 1734 : 2.51258
  98.  
  99. Distance between 1729 and 1731 : 2.577
  100.  
  101. Resultant mst is :
  102. 1.7340e+03 1.7410e+03 2.5126e+00
  103. 1.7290e+03 1.7310e+03 2.5770e+00 //Shouldn't this be 1729 and 1734 ??
  104. 1.7310e+03 1.7340e+03 2.5770e+00
  105. 1.7340e+03 1.7440e+03 2.6024e+00
  106. 1.7270e+03 1.7340e+03 3.2150e+00
  107. 2.5920e+03 2.6040e+03 3.3885e+00
  108. 2.6030e+03 2.6040e+03 3.3885e+00
Add Comment
Please, Sign In to add comment