SHOGUN  6.0.0
BruteKNNSolver.cpp
Go to the documentation of this file.
1 /* This software is distributed under BSD 3-clause license (see LICENSE file).
2  *
3  * Copyright (c) 2012-2013 Sergey Lisitsyn
4  */
5 
7 #include <shogun/lib/Signal.h>
8 
9 using namespace shogun;
10 
11 CBruteKNNSolver::CBruteKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector<int32_t> train_labels, const SGMatrix<index_t> NN):
12 CKNNSolver(k, q, num_classes, min_label, train_labels)
13 {
14  init();
15  nn=NN;
16 }
17 
18 CMulticlassLabels* CBruteKNNSolver::classify_objects(CDistance* knn_distance, const int32_t num_lab, SGVector<int32_t>& train_lab, SGVector<float64_t>& classes) const
19 {
20  CMulticlassLabels* output=new CMulticlassLabels(num_lab);
21  //get the k nearest neighbors of each example
22  SGMatrix<index_t> NN = this->nn;
23 
24  //from the indices to the nearest neighbors, compute the class labels
25  for (index_t i=0; i<num_lab && (!CSignal::cancel_computations()); i++)
26  {
27  //write the labels of the k nearest neighbors from theirs indices
28  for (index_t j=0; j<m_k; j++)
29  train_lab[j] = m_train_labels[ NN(j,i) ];
30 
31  //get the index of the 'nearest' class
32  index_t out_idx = choose_class(classes.vector, train_lab.vector);
33  //write the label of 'nearest' in the output
34  output->set_label(i, out_idx + m_min_label);
35  }
36 
37  return output;
38 }
39 
40 SGVector<int32_t> CBruteKNNSolver::classify_objects_k(CDistance* knn_distance, const int32_t num_lab, SGVector<int32_t>& train_lab, SGVector<int32_t>& classes) const
41 {
42  SGVector<int32_t> output(m_k*num_lab);
43 
44  //get the k nearest neighbors of each example
45  SGMatrix<index_t> NN = this->nn;
46 
47  for (index_t i=0; i<num_lab && (!CSignal::cancel_computations()); i++)
48  {
49  //write the labels of the k nearest neighbors from theirs indices
50  for (index_t j=0; j<m_k; j++)
51  train_lab[j] = m_train_labels[ NN(j,i) ];
52 
53  choose_class_for_multiple_k(output.vector+i, classes.vector, train_lab.vector, num_lab);
54  }
55 
56  return output;
57 }
int32_t m_k
the k parameter in KNN
Definition: KNNSolver.h:94
Class Distance, a base class for all the distances used in the Shogun toolbox.
Definition: Distance.h:87
SGVector< int32_t > m_train_labels
Definition: KNNSolver.h:106
int32_t index_t
Definition: common.h:72
virtual CMulticlassLabels * classify_objects(CDistance *d, const int32_t num_lab, SGVector< int32_t > &train_lab, SGVector< float64_t > &classes) const
int32_t choose_class(float64_t *classes, const int32_t *train_lab) const
Definition: KNNSolver.cpp:35
virtual SGVector< int32_t > classify_objects_k(CDistance *d, const int32_t num_lab, SGVector< int32_t > &train_lab, SGVector< int32_t > &classes) const
bool set_label(int32_t idx, float64_t label)
Multiclass Labels for multi-class classification.
void choose_class_for_multiple_k(int32_t *output, int32_t *classes, const int32_t *train_lab, const int32_t step) const
Definition: KNNSolver.cpp:62
double float64_t
Definition: common.h:60
SGMatrix< index_t > nn
static bool cancel_computations()
Definition: Signal.h:111
int32_t m_min_label
smallest label, i.e. -1
Definition: KNNSolver.h:103
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18

SHOGUN Machine Learning Toolbox - Documentation