Eigen is a high-level C++ library of template headers for linear algebra, matrix and vector operations,numerical solvers and related algorithms.
How to install Eigen?
Get the latest Eigen package from here. Unzip it and include the appropriate header files. There is no need to compile or include binaries.
How to use Eigen?
I have included my code for computing ridge regression here to show some sample usages of Eigen. Most of the time it is very straight-forward.
#include<Eigen/SVD>
using namespace Eigen;
void CRidgeRegression::ComputeRegression()
{
int m = m_X.size();
int n = m_X[0].size();
MatrixXd mat(m, n + 1);
VectorXd rhs(m);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n + 1; j++)
{
if (j < n)
{
mat(i, j) = m_X[i][j];
rhs(i) = m_Y[i];
}
else
{
mat(i, j) = 1;
}
}
}
MatrixXd tmp = mat.transpose()*mat + m_alpha*MatrixXd::Identity(n + 1, n + 1);
JacobiSVD<MatrixXd> svd(tmp, ComputeThinU | ComputeThinV);
MatrixXd res = svd.solve(mat.transpose()*rhs);
m_coef.resize(n);
for (int i = 0; i < n; i++)
{
m_coef[i] = res(i, 0);
}
m_intercept = res(n, 0);
}
No comments:
Post a Comment