123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
-
- #include "vislearning/regression/linregression/LinRegression.h"
- #include "core/vector/Algorithms.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- LinRegression::LinRegression(){
- dim = 0;
- }
- LinRegression::LinRegression(uint dimension){
- dim = dimension;
- }
- LinRegression::~LinRegression()
- {
- }
- void LinRegression::teach ( const NICE::VVector & x, const NICE::Vector & y ){
-
- if (dim == 0){
- dim = x[0].size()+1;
- }
-
- cerr<<"dim: "<<dim<<endl;
- cerr<<"examples: "<<x.size()<<endl;
-
- for ( uint i = 0;i < dim;i++ ){
- alpha.push_back(0.0);
- }
-
- if ( dim == 2 ){
- double meanX;
- double meanY = y.Mean();
- double sumX = 0.0;
-
- for ( uint i = 0;i < x.size();i++ ){
- sumX += x[i][0];
- }
- meanX = sumX / (double)x.size();
-
-
- for ( uint i = 0; i < x.size(); i++ ){
- alpha[1] += x[i][0] * y[i];
- }
-
- alpha[1] -= x.size() * meanX * meanY;
-
- double tmpAlpha = 0.0;
- for ( uint i = 0; i < x.size(); i++ ){
- tmpAlpha += x[i][0] * x[i][0];
- }
- tmpAlpha -= x.size() * meanX * meanX;
-
- alpha[1] /= tmpAlpha;
-
- alpha[0] = meanY - alpha[1] * meanX;
- }
- else {
- NICE::Matrix X, Xtmp, tmpAlpha, tmpAlphaInv, G;
- NICE::Vector params;
- uint X_rows,X_cols;
-
- x.toMatrix(X);
- X_rows = X.rows();
- X_cols = X.cols();
- cout<<X_rows<<" "<<X_cols<<endl;
-
-
- NICE::Vector oneVector(x.size(),1.0);
- Xtmp = X.transpose();
- cout<<Xtmp.rows()<<" "<<Xtmp.cols()<<endl;
- Xtmp.resize(X_cols+1,X_rows);
- cout<<Xtmp.rows()<<" "<<Xtmp.cols()<<endl;
- cout<<"Size of vector; "<<oneVector.size()<<endl;
- Xtmp.setRow(Xtmp.rows()-1,oneVector);
- Xtmp.exchangeRows(0,Xtmp.rows()-1);
- cout<<Xtmp.rows()<<" "<<Xtmp.cols()<<endl;
- X = Xtmp.transpose();
-
- cout<<X.rows()<<" "<<X.cols()<<endl;
-
- tmpAlpha.multiply(X,X,true);
- choleskyDecomp(tmpAlpha,G);
- choleskyInvert(G,tmpAlphaInv);
- params.multiply(X,y,true);
- params.multiply(tmpAlphaInv,params);
- alpha = params.std_vector();
-
- cout<<"Alpha-Vektor: ";
- for(uint i=0;i<alpha.size();i++){
- cout<<alpha[i]<<" ";
- }
- cout<<endl;
- }
- }
- double LinRegression::predict ( const NICE::Vector & x ){
- double y;
- if ( dim == 2 ){
- y = alpha[0] + alpha[1] * x[0];
- }
- else {
-
- NICE::Vector nAlpha(alpha);
- y = x.scalarProduct(nAlpha);
- }
-
- return y;
- }
|