Skip to content

Tutorial

Panagiotis Petropoulakis edited this page Jan 4, 2021 · 11 revisions
  • Step 1: Build "random" vectors. Header files: item.h and utils.h
list<Item> dataPoints;
errorCode status; // Keep errors
for(i = 0; i < n; i++){
  vector<double> components;
  
  for(j = 0; j < dim; j++){
    components.push_back(i + 5);
  }
  
  dataPoints.push_back(Item(components, status));
  if(status != SUCCESS){
    printError(status);
    return;
  }
}
  • Step 2: Pick a model. Header files: model.h, lshEuclidean.h or lshCosin.h or hypercubeEuclidean.h or hypercubeCosin.h
model* myModel;
myModel = new lshEuclidean();

or you can pick your favorable hyperparameters

int l = 7, coefficient = 0.2, k = 9, w = 200;
myModel = new lshEuclidean(coefficient, l, k, w);
  • Step 3: Train the model with the vectors
myModel->fit(dataPoints, status);
if(status != SUCCESS){
  printError(status);
  delete myModel
  return;
}
  • Step 4: Find the radius and the nearest neighbors for a query vector
vector<double> queryComponents;
for(i = 0; i < dim; i++)
  queryComponents.push_back(i + 13);
  
Item query(queryComponents, status);
if(status != SUCCESS){
  printError(status);
  delete myModel
  return;
}

int radius = 50;
list<double> neighborsDistances;
list<Item> neighbors;
double nearestDistance;
Item nearestNeighbor;

/* Find radius */
myModel->radiusNeighbors(query, radius, neighbors, neighborsDistances, status);
if(status != SUCCESS){
  printError(status);
  delete myModel
  return;
}

/* Find nearest neighbor */
myModel->nNeighbor(query, nearestNeighbor, nearestDistance, status);
if(status != SUCCESS){
  printError(status);
  delete myModel
  return;
}
  • Step 5: Print results. See item.h header for more accessor functions
list<Item>::iterator iter;
list<double>::iterator distances = neighborsDistances.begin(); 

for(iter = neighbors.begin(); iter != neighbors.end(); iter++){
  cout << Neighbor id: << iter->getId();
  cout << Neighbor dist: << *distances << "\n";
  
  distances++;
}

cout << Nearest neighbor id: << nearestNeighbor.getId();
cout << Nearest neighbor dist: << nearestDistance << "\n";
  • Step 6: Compilation. Take a look in the experiments makefiles
Clone this wiki locally