Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mlpack/core.hpp>
- #include <mlpack/methods/neighbor_search/neighbor_search.hpp>
- #include <mlpack/methods/hdbscan/hdbscan.hpp>
- #include <mlpack/core/tree/cover_tree.hpp>
- #include <iostream>
- #include <math.h>
- using namespace mlpack;
- using namespace mlpack::neighbor; // NeighborSearch and NearestNeighborSort
- using namespace mlpack::metric;
- using namespace mlpack::hdbscan;
- using namespace std;
- // HDBSCAN metric is defined as :
- template<>
- template<typename VecTypeA, typename VecTypeB>
- double HdbscanDistance::Evaluate(
- const VecTypeA& a,
- const VecTypeB& b)
- {
- double dcoreA = a(a.n_rows-1);
- double dcoreB = b(b.n_rows-1);
- typename VecTypeB::elem_type dist = sqrt(arma::accu(arma::square(a - b) - (dcoreA - dcoreB)*(dcoreA - dcoreB)));
- double temp = std::max(std::max(dcoreA, dcoreB), dist);
- return temp;
- }
- int main()
- {
- arma::mat dataSynthetic1;
- data::Load("manyPatches.csv", dataSynthetic1, true);
- dataSynthetic1.resize(dataSynthetic1.n_rows-1, dataSynthetic1.n_cols);
- neighbor::NeighborSearch<neighbor::NearestNeighborSort , metric::EuclideanDistance> neighborSearch;
- arma::Mat<size_t> neighbors;
- arma::mat distances;
- neighborSearch.Train(dataSynthetic1);
- neighborSearch.Search(5, neighbors, distances);
- arma::mat dataWithDcore = arma::conv_to<arma::mat>::from(dataSynthetic1);
- dataWithDcore.resize(dataWithDcore.n_rows+1, dataWithDcore.n_cols);
- dataWithDcore.row(dataWithDcore.n_rows-1) = distances.row(distances.n_rows-1);
- std::cout<<"\nPoint 1741 is : \n";
- dataWithDcore.col(1741).print();
- std::cout<<"\nPoint 1734 is : \n";
- dataWithDcore.col(1734).print();
- std::cout<<"\nPoint 1729 is : \n";
- dataWithDcore.col(1729).print();
- std::cout<<"\nPoint 1731 is : \n";
- dataWithDcore.col(1731).print();
- std::cout<<"\nPoint 1744 is : \n";
- dataWithDcore.col(1744).print();
- std::cout<<"\nPoint 1727 is : \n";
- dataWithDcore.col(1727).print();
- std::cout<<"\nResultant mst is : \n";
- metric::HdbscanDistance h;
- emst::DualTreeBoruvka<metric::HdbscanDistance, arma::mat, tree::KDTree> dtb(dataWithDcore, false, h);
- arma::mat resultsMST;
- dtb.ComputeMST(resultsMST);
- resultsMST = trans(resultsMST);
- resultsMST.print();
- }
- First few lines of output are:
- Point 1741 is :
- 3.0503e+02
- 4.0717e+02
- 2.5126e+00
- Point 1734 is :
- 3.0529e+02
- 4.0621e+02
- 2.4534e+00
- Point 1729 is :
- 3.0286e+02
- 4.0592e+02
- 2.5126e+00
- Point 1731 is :
- 3.0332e+02
- 4.0598e+02
- 2.5770e+00
- Point 1744 is :
- 3.0471e+02
- 4.0751e+02
- 2.6024e+00
- Point 1727 is :
- 3.0600e+02
- 4.0525e+02
- 3.2150e+00
- Distance between 1729 and 1734 : 2.51258
- Distance between 1729 and 1731 : 2.577
- Resultant mst is :
- 1.7340e+03 1.7410e+03 2.5126e+00
- 1.7290e+03 1.7310e+03 2.5770e+00 //Shouldn't this be 1729 and 1734 ??
- 1.7310e+03 1.7340e+03 2.5770e+00
- 1.7340e+03 1.7440e+03 2.6024e+00
- 1.7270e+03 1.7340e+03 3.2150e+00
- 2.5920e+03 2.6040e+03 3.3885e+00
- 2.6030e+03 2.6040e+03 3.3885e+00
Add Comment
Please, Sign In to add comment