45 PCA(
const std::vector< std::vector<double> > &data,
bool data_reduction_flag =
true);
47 PCA(
const std::vector< std::vector<double> > &data,
const std::vector<size_t> &cards,
bool data_reduction_flag =
true);
49 PCA(
const std::map< std::vector<double>,
size_t > &data,
bool data_reduction_flag =
true);
51 template<
typename ITER>
PCA(ITER begin, ITER end,
bool data_reduction_flag =
true);
56 virtual ~PCA()
override =
default;
64 const std::vector<double>&
GetMeans() const noexcept {
return means; }
69 const std::vector<double>&
GetDeviations() const noexcept {
return deviations; }
74 const std::multimap<double, MatrixDouble>&
GetEigensystem() const noexcept {
return eigensystem; }
79 std::vector<std::vector<double>>
Transform(
const std::vector< std::vector<double> > &data,
const size_t nb_features = 0u)
const;
82 std::vector<std::vector<double>>
ReverseTransform(
const std::vector< std::vector<double> > &data)
const;
89 std::multimap<double, MatrixDouble> makeCorrelationSpectralEigensystem(
double g)
const;
92 std::vector<double> means;
93 std::vector<double> deviations;
94 std::multimap<double, MatrixDouble> eigensystem;
97 public :
PCA(xml::Element& el):means(1,1),dimension(1),deviations(1,1){
Deserialize(el);}
114 template<typename ITER>
PCA::
PCA(ITER begin, ITER end,
bool data_reduction_flag)
116 std::vector< std::vector<double> > altered_data;
117 std::vector<double> scales;
118 dimension = (begin->first).size();
119 size_t nb_patterns = 0;
120 size_t nb_prototypes = 0;
124 for (ITER it = begin; it != end; ++it)
126 nb_patterns += it->second;
130 means = std::vector<double>(dimension);
132 for (ITER it = begin; it != end; ++it)
134 std::vector<double> pat = it->first;
136 double scale = (double) it->second / (
double) nb_patterns;
138 for (
size_t ft = 0; ft < dimension; ++ft)
139 means[ft] += pat[ft] * scale;
144 for (ITER it = begin; it != end; ++it)
146 std::vector<double> pat = it->first;
147 size_t pop = it->second;
149 std::vector<double> new_pat;
151 for (
size_t ft = 0; ft < dimension; ++ft)
152 new_pat.push_back(pat[ft] - means[ft]);
154 altered_data.push_back(new_pat);
155 scales.push_back((
double) pop / (
double) (nb_patterns - 1));
160 deviations = std::vector<double>(dimension);
162 for (
size_t k = 0; k < nb_prototypes; ++k)
164 std::vector<double> pat = altered_data[k];
165 double scale = scales[k];
167 for (
size_t ft = 0; ft < dimension; ++ft)
169 double val = pat[ft];
170 deviations[ft] += scale * val * val;
174 for (
size_t ft = 0; ft < dimension; ++ft)
175 deviations[ft] = sqrt(deviations[ft]);
179 if (data_reduction_flag)
181 for (
size_t ft = 0; ft < dimension; ++ft)
183 double ratio = 1.0 / deviations[ft];
185 for (
size_t k = 0; k < nb_prototypes; ++k)
186 (altered_data[k])[ft] *= ratio;
193 double scale_correction = (double)(nb_patterns - 1) / (double) nb_patterns;
195 for (
size_t k = 0; k < nb_prototypes; ++k)
197 std::vector<double> pat = altered_data[k];
198 double scale = (double) scales[k] * scale_correction;
200 for (
size_t i = 0; i < dimension; ++i)
201 for (
size_t j = i; j < dimension; ++j)
205 for (
size_t i = 0; i < dimension; ++i)
206 for (
size_t j = i + 1; j < dimension; ++j)
207 cmat.
At(j, i) = cmat.
At(i, j);
211 if (data_reduction_flag)
212 eigensystem = makeCorrelationSpectralEigensystem(cmat.
At(0, 1));
const std::vector< double > & GetDeviations() const noexcept
Returns the deviation computed for sample data.
virtual ~PCA() override=default
Destructor.
size_t GetDimension() const noexcept
Returns the dimension of feature space.
PCA(const MatrixDouble &data, bool data_reduction_flag=true)
Constructor.
std::multimap< double, MatrixDouble > MakeJacobiEigensystem(size_t MaxIteration=100) const
Perform diagonalization for symmetric matrix.
MatrixDouble Transform(const MatrixDouble &patterns, size_t nb_features=1u) const
Apply transform to given patterns.
void Deserialize(xml::Element &el)
const std::vector< double > & GetMeans() const noexcept
Returns the mean values computed for sample data.
double GetDeviation(size_t d) const
Returns the deviation of d-th feature computed for sample data.
Class to perform Principal Componant Analysis.
PCA & operator=(const PCA &)=default
std::vector< std::vector< double > > ReverseTransform(const std::vector< std::vector< double > > &data) const
Apply reverse transform to get given patterns' pre-images.
const T & At(size_t pos) const noexcept
#define CRN_DECLARE_CLASS_CONSTRUCTOR(classname)
Declares a class constructor.
Square double matrix class.
std::multimap< double, MatrixDouble > MakeSpectralEigensystem() const
Perform diagonalization for 2x2 symmetric matrix.
xml::Element Serialize(xml::Element &parent) const
CRN_ALIAS_SMART_PTR(ImageBW)
const std::multimap< double, MatrixDouble > & GetEigensystem() const noexcept
Returns the eigensystem computed on covariance matrix of last sample data.
double GetMean(size_t d) const
Returns the mean value of d-th feature computed for sample data.
void IncreaseElement(size_t r, size_t c, const T &delta)
Increases the value of an element.