#include "stdafx.h"
#include "orsciJWVCL.h"
#include "orsciVM.h"
using namespace orsci;
using namespace orsci::vmt;
int main()
{
cout << " orsci:Principal component analysis (PCA)" << endl
<< " --- http://www.orsci.cn" << endl;
cout << "Part1:进行主成分分析系数矩阵计算!" << endl;
mdouble X;
//randn(13, 5, X);
X = "148 41 72 78;"
"139 34 71 76;"
"160 49 77 86;"
"149 36 67 79;"
"159 45 80 86;"
"142 31 66 76;"
"153 43 76 83;"
"150 43 77 79;"
"151 42 77 80;"
"139 31 68 74;"
"140 29 64 74;"
"161 47 78 84;"
"158 49 78 83;"
"140 33 67 77;"
"137 31 66 73;"
"152 35 73 79;"
"149 47 82 79;"
"145 35 70 77;"
"160 47 74 87;"
"156 44 78 85;"
"151 42 73 82;"
"147 38 73 78;"
"157 39 68 80;"
"147 30 65 75;"
"157 48 80 88;"
"151 36 74 80;"
"144 36 68 76;"
"141 30 67 76;"
"139 32 68 73;"
"148 38 70 78;";
cout << "X = " << endl;
cout << X << endl;
cout << "sample count = " << X.rowCount() << endl;
mdouble coeff_out, score_out;
coldouble latent_out;
coldouble tsquared_out;
bool mFlag = princomp(X, coeff_out, score_out, latent_out, tsquared_out);
cout << "主成分分析成功标记:" << mFlag << endl;
cout << "coeff = " << endl;
cout << coeff_out << endl;
cout << "score = " << endl;
cout << score_out << endl;
cout << "latent = " << endl;
cout << latent_out << endl;
cout << "tsquared = " << endl;
cout << tsquared_out << endl;
rowdouble mu;
mu.assign(vmt::mean(X, 0));
cout << "X的列均值mu=" << endl;
cout << mu << endl;
cout << "=======now display the result: (X - mu) * coeff_out=====" << endl;
cout << (X - mu) * coeff_out << endl;
cout << "计算贡献值与累积贡献值!" << endl;
double mSum = vmt::sum(latent_out);
if (mSum == 0) {cout << "there is a bug here!" << endl; return 0;}
coldouble mContribute = latent_out / mSum;
cout << mContribute << endl;
coldouble mSumContribute = mContribute;
for (int k = 1; k < mSumContribute.size(); ++k)
{
mSumContribute(k) = mSumContribute(k) + mSumContribute(k - 1);
}
cout << "累计贡献:" << endl;
cout << mSumContribute << endl;
const double mThreshold = 0.95; //按照0.95做贡献值过滤。
const int mIndex = vmt::find_first_of(mSumContribute, vmt::GEQ(mThreshold));
cout << "保留的属性个数为:" << mIndex + 1 << endl;
if (mIndex < 0) {cout << "there is a bug here!" << endl; return 0;}
mdouble coeff, score;
coeff = coeff_out.subcol(span(0, mIndex)); //截取0至mIndex列
score = score_out.subcol(span(0, mIndex)); //截取0至mIndex列
cout << "保留的系数矩阵coeff=" << endl;
cout << coeff << endl;
cout << "保留的分数矩阵score=" << endl;
cout << score << endl;
cout << "Part2:下面开始测试或应用!" << endl;
mdouble test = "150 31 70 80";
cout << "假设新的测试样例test=" << endl;
cout << test << endl;
mdouble test_score = (test - mu) * coeff;
cout << "新测试样例的特征表示test_score=" << endl;
cout << test_score << endl;
cout << endl;
cout << "press any key to stop..." << endl;
char pp;
cin >> pp;
return 0;
}
int Demo_test() //下面的程序执行与前面程序等价!
{
cout << " orsci:Principal component analysis (PCA)" << endl
<< " --- http://www.orsci.cn" << endl;
cout << "Part1:进行主成分分析系数矩阵计算!" << endl;
mdouble X;
//randn(13, 5, X);
X = "148 41 72 78;"
"139 34 71 76;"
"160 49 77 86;"
"149 36 67 79;"
"159 45 80 86;"
"142 31 66 76;"
"153 43 76 83;"
"150 43 77 79;"
"151 42 77 80;"
"139 31 68 74;"
"140 29 64 74;"
"161 47 78 84;"
"158 49 78 83;"
"140 33 67 77;"
"137 31 66 73;"
"152 35 73 79;"
"149 47 82 79;"
"145 35 70 77;"
"160 47 74 87;"
"156 44 78 85;"
"151 42 73 82;"
"147 38 73 78;"
"157 39 68 80;"
"147 30 65 75;"
"157 48 80 88;"
"151 36 74 80;"
"144 36 68 76;"
"141 30 67 76;"
"139 32 68 73;"
"148 38 70 78;";
cout << "X = " << endl;
cout << X << endl;
cout << "sample count = " << X.rowCount() << endl;
mdouble coeff_out, score_out;
coldouble latent_out;
rowdouble mu;
coldouble tsquared_out;
const double mThreshold = 0.95; //按照0.95做贡献值过滤。
bool mFlag = princomp(X, coeff_out, score_out, latent_out, tsquared_out, mThreshold, mu);
cout << "主成分分析成功标记:" << mFlag << endl;
cout << "coeff = " << endl;
cout << coeff_out << endl;
cout << "score = " << endl;
cout << score_out << endl;
cout << "latent = " << endl;
cout << latent_out << endl;
cout << "tsquared = " << endl;
cout << tsquared_out << endl;
cout << "X的列均值mu=" << endl;
cout << mu << endl;
cout << "Part2:下面开始测试或应用!" << endl;
mdouble test = "150 31 70 80";
cout << "假设新的测试样例test=" << endl;
cout << test << endl;
mdouble test_score = princomp_test(test, mu, coeff_out);
cout << "新测试样例的特征表示test_score=" << endl;
cout << test_score << endl;
return 0;
|