/usr/include/shogun/classifier/KNN.h is in libshogun-dev 1.1.0-4ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | /*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2006 Christian Gehl
* Written (W) 1999-2009 Soeren Sonnenburg
* Written (W) 2011 Sergey Lisitsyn
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/
#ifndef _KNN_H__
#define _KNN_H__
#include <stdio.h>
#include <shogun/lib/common.h>
#include <shogun/io/SGIO.h>
#include <shogun/features/Features.h>
#include <shogun/distance/Distance.h>
#include <shogun/machine/DistanceMachine.h>
namespace shogun
{
class CDistanceMachine;
/** @brief Class KNN, an implementation of the standard k-nearest neigbor
* classifier.
*
* An example is classified to belong to the class of which the majority of the
* k closest examples belong to. Formally, kNN is described as
*
* \f[
* label for x = \arg \max_{l} \sum_{i=1}^{k} [label of i-th example = l]
* \f]
*
* This class provides a capability to do weighted classfication using:
*
* \f[
* label for x = \arg \max_{l} \sum_{i=1}^{k} [label of i-th example = l] q^{i},
* \f]
*
* where \f$|q|<1\f$.
*
* To avoid ties, k should be an odd number. To define how close examples are
* k-NN requires a CDistance object to work with (e.g., CEuclideanDistance ).
*
* Note that k-NN has zero training time but classification times increase
* dramatically with the number of examples. Also note that k-NN is capable of
* multi-class-classification. And finally, in case of k=1 classification will
* take less time with an special optimization provided.
*/
class CKNN : public CDistanceMachine
{
public:
/** default constructor */
CKNN();
/** constructor
*
* @param k k
* @param d distance
* @param trainlab labels for training
*/
CKNN(int32_t k, CDistance* d, CLabels* trainlab);
virtual ~CKNN();
/** get classifier type
*
* @return classifier type KNN
*/
virtual inline EClassifierType get_classifier_type() { return CT_KNN; }
//inline EDistanceType get_distance_type() { return DT_KNN;}
/** classify all examples
*
* @return resulting labels
*/
virtual CLabels* apply();
/** classify objects
*
* @param data (test)data to be classified
* @return classified labels
*/
virtual CLabels* apply(CFeatures* data);
/// get output for example "vec_idx"
virtual float64_t apply(int32_t vec_idx)
{
SG_ERROR( "for performance reasons use apply() instead of apply(int32_t vec_idx)\n");
return 0;
}
/** classify all examples for 1...k
*
*/
SGMatrix<int32_t> classify_for_multiple_k();
/** load from file
*
* @param srcfile file to load from
* @return if loading was successful
*/
virtual bool load(FILE* srcfile);
/** save to file
*
* @param dstfile file to save to
* @return if saving was successful
*/
virtual bool save(FILE* dstfile);
/** set k
*
* @param k k to be set
*/
inline void set_k(int32_t k)
{
ASSERT(k>0);
m_k=k;
}
/** get k
*
* @return value of k
*/
inline int32_t get_k()
{
return m_k;
}
/** set q
* @param q value
*/
inline void set_q(float64_t q)
{
ASSERT(q<=1.0 && q>0.0);
m_q = q;
}
/** get q
* @return q parameter
*/
inline float64_t get_q() { return m_q; }
/** @return object name */
inline virtual const char* get_name() const { return "KNN"; }
protected:
/** Stores feature data of underlying model.
*
* Replaces lhs and rhs of underlying distance with copies of themselves
*/
virtual void store_model_features();
/** classify all examples with nearest neighbor (k=1)
* @return classified labels
*/
virtual CLabels* classify_NN();
/** init distances to test examples
* @param data test examples
*/
void init_distance(CFeatures* data);
/** train k-NN classifier
*
* @param data training data (parameter can be avoided if distance or
* kernel-based classifiers are used and distance/kernels are
* initialized with train data)
*
* @return whether training was successful
*/
virtual bool train_machine(CFeatures* data=NULL);
private:
void init();
protected:
/// the k parameter in KNN
int32_t m_k;
/// parameter q of rank weighting
float64_t m_q;
/// number of classes (i.e. number of values labels can take)
int32_t num_classes;
/// smallest label, i.e. -1
int32_t min_label;
/** the actual trainlabels */
SGVector<int32_t> train_labels;
};
}
#endif
|