فهرست منبع

added N-dimensional linear regression

Frank Prüfer 11 سال پیش
والد
کامیت
e52f7bb34e
1فایلهای تغییر یافته به همراه47 افزوده شده و 2 حذف شده
  1. 47 2
      regression/linregression/LinRegression.cpp

+ 47 - 2
regression/linregression/LinRegression.cpp

@@ -7,6 +7,7 @@
 */  
 
 #include "vislearning/regression/linregression/LinRegression.h"
+#include "core/vector/Algorithms.h"
 
 using namespace OBJREC;
 
@@ -15,7 +16,7 @@ using namespace std;
 using namespace NICE;
 
 LinRegression::LinRegression(){
-  dim = 2;
+  dim = 0;
 }
 
 LinRegression::LinRegression(uint dimension){
@@ -66,13 +67,57 @@ void LinRegression::teach ( const NICE::VVector & x, const NICE::Vector & y ){
     
     alpha[0] = meanY - alpha[1] * meanX;    
   }
+  else {  //N-dimensional least squares
+    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;
+    
+    // attach front column with ones (UGLY)
+    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;
+
+    // alpha =(X'X)^-1 * X'y
+    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 ){  //two-dimensional least squares
+  if ( dim == 2 ){  //two-dimensional least squares
     y = alpha[0] + alpha[1] * x[0];
   }
+  else {
+    // y = x * alpha
+    NICE::Vector nAlpha(alpha);
+    y = x.scalarProduct(nAlpha);
+  }
   
   return y;
 }