Xmipp  v3.23.11-Nereus
knn_classifier.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Vahid Abrishami (vabrishami@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef KNN_CLASSIFIER_HH
27 #define KNN_CLASSIFIER_HH
28 
29 /* Includes-----------------------------------------------------------------*/
30 #include <core/multidim_array.h>
31 
41 class KNN
42 {
43 public:
45  typedef enum { EUCLIDEAN = 0, CITYBLOCK = 1} distType;
46 
51  KNN(int k);
52 
57  void train(MultidimArray<double> &dataset,MultidimArray<double> &dataLabel,
58  MultidimArray<double> &labelset);
59 
64  int predict(MultidimArray<double> &sample,double &score);
65 
68 
69 #ifdef UNUSED // detected as unused 29.6.2018
70  void saveModel(const FileName &fn);
72 
74  void loadModel(const FileName &fn);
75 #endif
76 
78  void setK(int k);
79 
80 private:
82  double euclideanDistance(MultidimArray<double> &sample,int index,double maximumDist);
83 
84 #ifdef UNUSED // detected as unused 29.6.2018
85  double cityBlockDistance(MultidimArray<double> &sample,int index,double maximumDist);
87 #endif
88 
90  int findMaxIndex(MultidimArray<double> &inputArray);
91 
93  int findMinIndex(MultidimArray<double> &inputArray);
94 
95 private:
97  int K;
98 
100  MultidimArray<double> __dataset;
101 
103  MultidimArray<double> __dataLabel;
104 
106  MultidimArray<double> __labelSet;
107 
109  MultidimArray<int> neighborsIndex;
110 
112  MultidimArray<double> maxDist;
113 };
115 #endif
void train(MultidimArray< double > &dataset, MultidimArray< double > &dataLabel, MultidimArray< double > &labelset)
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
viol index
void setK(int k)
Method for setting the K.
distType
Type of distance.
void KNearestNeighbors(MultidimArray< double > &sample)
Compute the K nearest neighbors to the sample.
KNN(int k)
int predict(MultidimArray< double > &sample, double &score)