7 #include "../../Matrix/Matrix.h" 8 #include "../../Matrix/MatrixTools.h" 9 #include "../../VectorTools.h" 19 const vector<double>& rowW,
20 const vector<double>& colW,
43 setData(tmpData, rowW, colW, nbAxes, tol, verbose);
62 vector<double> rowW(nRow);
63 vector<double> colW(nCol);
81 setData(tmpData, rowW, colW, nbAxes, tol, verbose);
90 if (nRow != rowW.size())
91 throw Exception(
"PrincipalComponentAnalysis::center. The number of row weigths have to be equal to the number of rows!");
95 vector<double> columnMeans(nCol);
96 for (
unsigned int i = 0; i < nCol; i++)
99 for (
unsigned int j = 0; j < nRow; j++)
101 tmp += matrix(j, i) * rowW[j];
103 columnMeans[i] = tmp / sumRowWeights;
106 for (
unsigned int i = 0; i < nCol; i++)
108 for (
unsigned int j = 0; j < nRow; j++)
110 matrix(j, i) -= columnMeans[i];
121 if (nRow != rowW.size())
122 throw Exception(
"PrincipalComponentAnalysis::scale. The number of row weigths have to be equal to the number of rows!");
126 vector<double> columnSd(nCol);
127 for (
size_t i = 0; i < nCol; i++)
130 for (
unsigned int j = 0; j < nRow; j++)
132 tmp += pow(matrix(j, i), 2) * rowW[j];
134 columnSd[i] = sqrt(tmp / sumRowWeights);
137 for (
size_t i = 0; i < nCol; i++)
139 for (
unsigned int j = 0; j < nRow; j++)
141 if (columnSd[i] == 0.)
144 matrix(j, i) /= columnSd[i];
The matrix template interface.
void setData(const Matrix< double > &matrix, const std::vector< double > &rowWeights, const std::vector< double > &colWeights, unsigned int nbAxes, double tol=0.0000001, bool verbose=true)
Set the data and perform computations.
PrincipalComponentAnalysis(const Matrix< double > &data, unsigned int nbAxes, const std::vector< double > &rowW, const std::vector< double > &colW, bool centered=true, bool scaled=true, double tol=0.0000001, bool verbose=true)
Build a new PrincipalComponentAnalysis object.
std::vector< double > columnSd_
static void scale(Matrix< double > &matrix, const std::vector< double > &rowW)
This function allows to center an input matrix from its column means.
std::vector< double > columnMeans_
static void center(Matrix< double > &matrix, const std::vector< double > &rowW)
This function allows to center an input matrix from its column means.
virtual size_t getNumberOfColumns() const =0
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
virtual size_t getNumberOfRows() const =0
The core class of a multivariate analysis.