1.svd简介
2.svd分解例子
eigen库是一个c++库,实现了线性矩阵部分,其他还有openblas等。如果有时间自己要实现一个SVD的c++代码,现在只用库来代替。eigen不用cmake只要一步配置即可使用。在右键项目,在c/c++常规中附加包含目录包含eigen的解压目录即可。博客 [1]给出了eigen库的配置和使用例程。在官网[2]上给出了一个例子。
完整的代码如下
#include "stdafx.h"
#include <iostream>
#include <Eigen/SVD>
using namespace Eigen;
using namespace Eigen::internal;
using namespace Eigen::Architecture;
using namespace std;
int main(){
MatrixXf m = MatrixXf::Random(3,2);
cout << "Here is the matrix m:" << endl << m << endl;
JacobiSVD<MatrixXf> svd(m, ComputeThinU | ComputeThinV);
cout << "Its singular values are:" << endl << svd.singularValues() << endl;
cout << "Its left singular vectors are the columns of the thin U matrix:" << endl << svd.matrixU() << endl;
cout << "Its right singular vectors are the columns of the thin V matrix:" << endl << svd.matrixV() << endl;
Vector3f rhs(1, 0, 0);
cout << "Now consider this rhs vector:" << endl << rhs << endl;
cout << "A least-squares solution of m*x = rhs is:" << endl << svd.solve(rhs) << endl;
getchar();
return 0;
}
运行的效果如下
3.协同过滤简单例子
4.利用大点的数据集设计推荐系统的例子
[1] 小美女的博客
[2] JocabiSVD官网