/usr/include/fcl/learning/classifier.h is in libfcl-dev 0.5.0-5.
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 | #ifndef FCL_LEARNING_CLASSIFIER_H
#define FCL_LEARNING_CLASSIFIER_H
#include "fcl/math/vec_nf.h"
namespace fcl
{
template<std::size_t N>
struct Item
{
Vecnf<N> q;
bool label;
FCL_REAL w;
Item(const Vecnf<N>& q_, bool label_, FCL_REAL w_ = 1) : q(q_),
label(label_),
w(w_)
{}
Item() {}
};
template<std::size_t N>
struct Scaler
{
Vecnf<N> v_min, v_max;
Scaler()
{
// default no scale
for(std::size_t i = 0; i < N; ++i)
{
v_min[i] = 0;
v_max[i] = 1;
}
}
Scaler(const Vecnf<N>& v_min_, const Vecnf<N>& v_max_) : v_min(v_min_),
v_max(v_max_)
{}
Vecnf<N> scale(const Vecnf<N>& v) const
{
Vecnf<N> res;
for(std::size_t i = 0; i < N; ++i)
res[i] = (v[i] - v_min[i]) / (v_max[i] - v_min[i]);
return res;
}
Vecnf<N> unscale(const Vecnf<N>& v) const
{
Vecnf<N> res;
for(std::size_t i = 0; i < N; ++i)
res[i] = v[i] * (v_max[i] - v_min[i]) + v_min[i];
return res;
}
};
struct PredictResult
{
bool label;
FCL_REAL prob;
PredictResult() {}
PredictResult(bool label_, FCL_REAL prob_ = 1) : label(label_),
prob(prob_)
{}
};
template<std::size_t N>
class SVMClassifier
{
public:
~SVMClassifier() {}
virtual PredictResult predict(const Vecnf<N>& q) const = 0;
virtual std::vector<PredictResult> predict(const std::vector<Vecnf<N> >& qs) const = 0;
virtual std::vector<Item<N> > getSupportVectors() const = 0;
virtual void setScaler(const Scaler<N>& scaler) = 0;
virtual void learn(const std::vector<Item<N> >& data) = 0;
FCL_REAL error_rate(const std::vector<Item<N> >& data) const
{
std::size_t num = data.size();
std::size_t error_num = 0;
for(std::size_t i = 0; i < data.size(); ++i)
{
PredictResult res = predict(data[i].q);
if(res.label != data[i].label)
error_num++;
}
return error_num / (FCL_REAL)num;
}
};
template<std::size_t N>
Scaler<N> computeScaler(const std::vector<Item<N> >& data)
{
Vecnf<N> lower_bound, upper_bound;
for(std::size_t j = 0; j < N; ++j)
{
lower_bound[j] = std::numeric_limits<FCL_REAL>::max();
upper_bound[j] = -std::numeric_limits<FCL_REAL>::max();
}
for(std::size_t i = 0; i < data.size(); ++i)
{
for(std::size_t j = 0; j < N; ++j)
{
if(data[i].q[j] < lower_bound[j]) lower_bound[j] = data[i].q[j];
if(data[i].q[j] > upper_bound[j]) upper_bound[j] = data[i].q[j];
}
}
return Scaler<N>(lower_bound, upper_bound);
}
template<std::size_t N>
Scaler<N> computeScaler(const std::vector<Vecnf<N> >& data)
{
Vecnf<N> lower_bound, upper_bound;
for(std::size_t j = 0; j < N; ++j)
{
lower_bound[j] = std::numeric_limits<FCL_REAL>::max();
upper_bound[j] = -std::numeric_limits<FCL_REAL>::max();
}
for(std::size_t i = 0; i < data.size(); ++i)
{
for(std::size_t j = 0; j < N; ++j)
{
if(data[i][j] < lower_bound[j]) lower_bound[j] = data[i][j];
if(data[i][j] > upper_bound[j]) upper_bound[j] = data[i][j];
}
}
return Scaler<N>(lower_bound, upper_bound);
}
}
#endif
|