Xmipp  v3.23.11-Nereus
Public Types | Public Member Functions | List of all members

#include <knn_classifier.h>

Public Types

enum  distType { EUCLIDEAN = 0, CITYBLOCK = 1 }
 Type of distance. More...
 

Public Member Functions

 KNN (int k)
 
void train (MultidimArray< double > &dataset, MultidimArray< double > &dataLabel, MultidimArray< double > &labelset)
 
int predict (MultidimArray< double > &sample, double &score)
 
void KNearestNeighbors (MultidimArray< double > &sample)
 Compute the K nearest neighbors to the sample. More...
 
void setK (int k)
 Method for setting the K. More...
 

Detailed Description

This class implements KNN (K Nearest Neighbors). It does the classification by a voting of nearest neighbors of the point. It uses the exhaustive search in order to find the nearest neighbors.

Definition at line 41 of file knn_classifier.h.

Member Enumeration Documentation

◆ distType

Type of distance.

Enumerator
EUCLIDEAN 
CITYBLOCK 

Definition at line 45 of file knn_classifier.h.

45 { EUCLIDEAN = 0, CITYBLOCK = 1} distType;
distType
Type of distance.

Constructor & Destructor Documentation

◆ KNN()

KNN::KNN ( int  k)

Constructs the algorithm Parameter: k The number of nearest neighbors

Definition at line 28 of file knn_classifier.cpp.

29 {
30  setK(k);
31  neighborsIndex.resize(1,1,1,K);
32 }
void resize(size_t Ndim, size_t Zdim, size_t Ydim, size_t Xdim, bool copy=true)
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
void setK(int k)
Method for setting the K.
constexpr int K

Member Function Documentation

◆ KNearestNeighbors()

void KNN::KNearestNeighbors ( MultidimArray< double > &  sample)

Compute the K nearest neighbors to the sample.

Definition at line 42 of file knn_classifier.cpp.

43 {
44  double maximum;
45  double distance;
46  int maximumIndex;
47  maxDist.resize(1,1,1,K);
49  {
50  DIRECT_A1D_ELEM(maxDist,i)=euclideanDistance(sample,i,-1.0);
51  DIRECT_A1D_ELEM(neighborsIndex,i)=i;
52  }
53  maximumIndex=findMaxIndex(maxDist);
54  maximum=DIRECT_A1D_ELEM(maxDist,maximumIndex);
55  for (size_t i=K;i<YSIZE(__dataset);i++)
56  {
57  distance=euclideanDistance(sample,i,maximum);
58  if (distance==-1)
59  continue;
60  if (distance<maximum)
61  {
62  DIRECT_A1D_ELEM(maxDist,maximumIndex)=distance;
63  DIRECT_A1D_ELEM(neighborsIndex,maximumIndex)=i;
64  maximumIndex=findMaxIndex(maxDist);
65  maximum=DIRECT_A1D_ELEM(maxDist,maximumIndex);
66  }
67  }
68 }
#define YSIZE(v)
void resize(size_t Ndim, size_t Zdim, size_t Ydim, size_t Xdim, bool copy=true)
double euclideanDistance(const std::vector< T > &_v1, const std::vector< T > &_v2)
Definition: vector_ops.h:377
#define FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY1D(v)
#define i
#define DIRECT_A1D_ELEM(v, i)
TYPE distance(struct Point_T *p, struct Point_T *q)
Definition: point.cpp:28
constexpr int K

◆ predict()

int KNN::predict ( MultidimArray< double > &  sample,
double &  score 
)

Get an item an predict the related class of that by doing the voting among its neighbors.

Definition at line 70 of file knn_classifier.cpp.

71 {
72  MultidimArray<double> voteArray;
73  int index;
74  voteArray.initZeros(XSIZE(__labelSet));
75  KNearestNeighbors(sample);
77  {
78  index=DIRECT_A1D_ELEM(neighborsIndex,i);
79  for (size_t j=0;j<XSIZE(__labelSet);++j)
80  if (DIRECT_A1D_ELEM(__labelSet,j)==DIRECT_A1D_ELEM(__dataLabel,index))
81  DIRECT_A1D_ELEM(voteArray,j)+=1;
82  }
83  index=findMaxIndex(voteArray);
84  score=DIRECT_A1D_ELEM(voteArray,index)/double(K);
85  if (DIRECT_A1D_ELEM(voteArray,index)>(K*0.5))
86  return (int)DIRECT_A1D_ELEM(__labelSet,index);
87  index=findMinIndex(maxDist);
88  return (int)DIRECT_A1D_ELEM(__dataLabel,DIRECT_A1D_ELEM(neighborsIndex,index));
89 }
#define FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY1D(v)
#define i
#define DIRECT_A1D_ELEM(v, i)
viol index
#define XSIZE(v)
void KNearestNeighbors(MultidimArray< double > &sample)
Compute the K nearest neighbors to the sample.
#define j
constexpr int K
void initZeros(const MultidimArray< T1 > &op)

◆ setK()

void KNN::setK ( int  k)

Method for setting the K.

Definition at line 193 of file knn_classifier.cpp.

194 {
195  K=k;
196 }
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
constexpr int K

◆ train()

void KNN::train ( MultidimArray< double > &  dataset,
MultidimArray< double > &  dataLabel,
MultidimArray< double > &  labelset 
)

Now actually there is no training step.In future more complex train step can be added.

Definition at line 34 of file knn_classifier.cpp.

36 {
37  __dataset=dataset;
38  __dataLabel=dataLabel;
39  __labelSet=labelset;
40 }

The documentation for this class was generated from the following files: