فهرست منبع

Added copy-constructors for LinRegression and RegKNN.

Frank Prüfer 11 سال پیش
والد
کامیت
7821ff0ec8

+ 28 - 28
regression/linregression/LinRegression.cpp

@@ -23,6 +23,12 @@ LinRegression::LinRegression(uint dimension){
   dim = dimension;
 }
 
+LinRegression::LinRegression ( const LinRegression & src ) : 
+RegressionAlgorithm ( src )
+{
+dim = src.dim;
+}
+
 LinRegression::~LinRegression()
 {
 }
@@ -36,8 +42,8 @@ void LinRegression::teach ( const NICE::VVector & x, const NICE::Vector & y ){
   cerr<<"dim: "<<dim<<endl;
   cerr<<"examples: "<<x.size()<<endl;
   
-  for ( uint i = 0;i < dim;i++ ){  //initialize alpha-vector
-    alpha.push_back(0.0);
+  for ( uint i = 0;i < dim;i++ ){  //initialize vector of model parameters
+    modelParams.push_back(0.0);
   }
   
   if ( dim == 2 ){  //two-dimensional least squares
@@ -52,29 +58,23 @@ void LinRegression::teach ( const NICE::VVector & x, const NICE::Vector & y ){
  
     
     for ( uint i = 0; i < x.size(); i++ ){
-	alpha[1] += x[i][0] * y[i];
+	modelParams[1] += x[i][0] * y[i];
     }
     
-    alpha[1] -= x.size() * meanX * meanY;
+    modelParams[1] -= x.size() * meanX * meanY;
     
-    double tmpAlpha = 0.0;
+    double tmp = 0.0;
     for ( uint i = 0; i < x.size(); i++ ){
-      tmpAlpha += x[i][0] * x[i][0];
+      tmp += x[i][0] * x[i][0];
     }
-    tmpAlpha -= x.size() * meanX * meanX;
-    
-    alpha[1] /= tmpAlpha;
+    tmp -= x.size() * meanX * meanX;
     
-    alpha[0] = meanY - alpha[1] * meanX;
+    modelParams[1] /= tmp;
     
-//     cerr<<"Alpha-Vektor: ";
-//     for(uint i=0;i<alpha.size();i++){
-//       cerr<<alpha[i]<<" ";
-//     }
-//     cerr<<endl;
+    modelParams[0] = meanY - modelParams[1] * meanX;
   }
   else {  //N-dimensional least squares
-    NICE::Matrix X, tmpAlpha, G;
+    NICE::Matrix X, tmp, G;
     NICE::Vector params;
     
     x.toMatrix(X);
@@ -89,38 +89,38 @@ void LinRegression::teach ( const NICE::VVector & x, const NICE::Vector & y ){
       }
     }
 
-    // alpha =(X'X)^-1 * X'y
-    NICE::Matrix tmpAlphaInv;
+    // modelParams =(X'X)^-1 * X'y
+    NICE::Matrix tmpInv;
     NICE::Vector rhs;
       
     rhs.multiply(Xtmp,y,true);
       
-    tmpAlpha.multiply(Xtmp,Xtmp,true);
+    tmp.multiply(Xtmp,Xtmp,true);
       
-    choleskyDecomp(tmpAlpha,G);
-    choleskyInvert(G,tmpAlphaInv);
+    choleskyDecomp(tmp,G);
+    choleskyInvert(G,tmpInv);
       
-    params.multiply(tmpAlphaInv,rhs);
+    params.multiply(tmpInv,rhs);
 
-    alpha = params.std_vector();
+    modelParams = params.std_vector();
   }
 }
 
 std::vector<double> LinRegression::getModelParams(){
-  return alpha;
+  return modelParams;
 }
 
 double LinRegression::predict ( const NICE::Vector & x ){
   double y;
   if ( dim == 2 ){  //two-dimensional least squares
-    y = alpha[0] + alpha[1] * x[0];
+    y = modelParams[0] + modelParams[1] * x[0];
   }
   else {
-    // y = x * alpha
-    NICE::Vector nAlpha(alpha);
+    // y = x * modelParams
+    NICE::Vector nModel(modelParams);
     NICE:: Vector xTmp(1,1.0);
     xTmp.append(x);
-    y = xTmp.scalarProduct(nAlpha);
+    y = xTmp.scalarProduct(nModel);
   }
   
   return y;

+ 4 - 1
regression/linregression/LinRegression.h

@@ -21,7 +21,7 @@ class LinRegression : public RegressionAlgorithm
 {
   protected:
     /** vector containing all model parameters */
-    std::vector<double> alpha;
+    std::vector<double> modelParams;
     
     /** dimensionality of the model (i.e. number of model parameters) */
     uint dim;
@@ -33,6 +33,9 @@ class LinRegression : public RegressionAlgorithm
     /** constructor, specifying the dimensionality of the model*/
     LinRegression(uint dimension);
     
+    /** copy constructor */
+    LinRegression ( const LinRegression & src ); 
+    
     /** simple destructor */
     virtual ~LinRegression();
     

+ 8 - 0
regression/npregression/Makefile

@@ -0,0 +1,8 @@
+#TARGETS_FROM:=$(notdir $(patsubst %/,%,$(shell pwd)))/$(TARGETS_FROM)
+#$(info recursivly going up: $(TARGETS_FROM) ($(shell pwd)))
+
+all:
+
+%:
+	$(MAKE) TARGETS_FROM=$(notdir $(patsubst %/,%,$(shell pwd)))/$(TARGETS_FROM) -C .. $@
+

+ 103 - 0
regression/npregression/Makefile.inc

@@ -0,0 +1,103 @@
+# LIBRARY-DIRECTORY-MAKEFILE
+# conventions:
+# - all subdirectories containing a "Makefile.inc" are considered sublibraries
+#   exception: "progs/" and "tests/" subdirectories!
+# - all ".C", ".cpp" and ".c" files in the current directory are linked to a
+#   library
+# - the library depends on all sublibraries 
+# - the library name is created with $(LIBNAME), i.e. it will be somehow
+#   related to the directory name and with the extension .a
+#   (e.g. lib1/sublib -> lib1_sublib.a)
+# - the library will be added to the default build list ALL_LIBRARIES
+
+# --------------------------------
+# - remember the last subdirectory
+#
+# set the variable $(SUBDIR) correctly to the current subdirectory. this
+# variable can be used throughout the current makefile.inc. The many 
+# SUBDIR_before, _add, and everything are only required so that we can recover
+# the previous content of SUBDIR before exitting the makefile.inc
+
+SUBDIR_add:=$(dir $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))
+SUBDIR_before:=$(SUBDIR)
+SUBDIR:=$(strip $(SUBDIR_add))
+SUBDIR_before_$(SUBDIR):=$(SUBDIR_before)
+ifeq "$(SUBDIR)" "./"
+SUBDIR:=
+endif
+
+# ------------------------
+# - include subdirectories
+#
+# note the variables $(SUBDIRS_OF_$(SUBDIR)) are required later on to recover
+# the dependencies automatically. if you handle dependencies on your own, you
+# can also dump the $(SUBDIRS_OF_$(SUBDIR)) variable, and include the
+# makefile.inc of the subdirectories on your own...
+
+SUBDIRS_OF_$(SUBDIR):=$(patsubst %/Makefile.inc,%,$(wildcard $(SUBDIR)*/Makefile.inc))
+include $(SUBDIRS_OF_$(SUBDIR):%=%/Makefile.inc)
+
+# ----------------------------
+# - include local dependencies
+#
+# you can specify libraries needed by the individual objects or by the whole
+# directory. the object specific additional libraries are only considered
+# when compiling the specific object files
+# TODO: update documentation...
+
+-include $(SUBDIR)libdepend.inc
+
+$(foreach d,$(filter-out %progs %tests,$(SUBDIRS_OF_$(SUBDIR))),$(eval $(call PKG_DEPEND_INT,$(d))))
+
+# ---------------------------
+# - objects in this directory
+#
+# the use of the variable $(OBJS) is not mandatory. it is mandatory however
+# to update $(ALL_OBJS) in a way that it contains the path and name of
+# all objects. otherwise we can not include the appropriate .d files.
+
+OBJS:=$(patsubst %.cpp,$(OBJDIR)%.o,$(notdir $(wildcard $(SUBDIR)*.cpp))) \
+      $(patsubst %.C,$(OBJDIR)%.o,$(notdir $(wildcard $(SUBDIR)*.C))) \
+	  $(shell grep -ls Q_OBJECT $(SUBDIR)*.h | sed -e's@^@/@;s@.*/@$(OBJDIR)moc_@;s@\.h$$@.o@') \
+      $(patsubst %.c,$(OBJDIR)%.o,$(notdir $(wildcard $(SUBDIR)*.c)))
+ALL_OBJS += $(OBJS)
+
+# ----------------------------
+# - binaries in this directory
+#
+# output of binaries in this directory. none of the variables has to be used.
+# but everything you add to $(ALL_LIBRARIES) and $(ALL_BINARIES) will be
+# compiled with `make all`. be sure again to add the files with full path.
+
+LIBRARY_BASENAME:=$(call LIBNAME,$(SUBDIR))
+ifneq "$(SUBDIR)" ""
+ALL_LIBRARIES+=$(LIBDIR)$(LIBRARY_BASENAME).$(LINK_FILE_EXTENSION)
+endif
+
+# ---------------------
+# - binary dependencies
+#
+# there is no way of determining the binary dependencies automatically, so we
+# follow conventions. the current library depends on all sublibraries.
+# all other dependencies have to be added manually by specifying, that the
+# current .pc file depends on some other .pc file. binaries depending on
+# libraries should exclusivelly use the .pc files as well.
+
+ifeq "$(SKIP_BUILD_$(OBJDIR))" "1"
+$(LIBDIR)$(LIBRARY_BASENAME).a:
+else
+$(LIBDIR)$(LIBRARY_BASENAME).a:$(OBJS) \
+	$(call PRINT_INTLIB_DEPS,$(PKGDIR)$(LIBRARY_BASENAME).a,.$(LINK_FILE_EXTENSION))
+endif
+
+$(PKGDIR)$(LIBRARY_BASENAME).pc: \
+	$(call PRINT_INTLIB_DEPS,$(PKGDIR)$(LIBRARY_BASENAME).pc,.pc)
+
+# -------------------
+# - subdir management
+#
+# as the last step, always add this line to correctly recover the subdirectory
+# of the makefile including this one!
+
+SUBDIR:=$(SUBDIR_before_$(SUBDIR))
+

+ 7 - 0
regression/npregression/RegKNN.cpp

@@ -29,6 +29,13 @@ RegKNN::RegKNN ( const Config *_conf, NICE::VectorDistance<double> *_distancefun
 		distancefunc = new EuclidianDistance<double>();
 }
 
+RegKNN::RegKNN ( const RegKNN & src ) : RegressionAlgorithm ( src )
+{
+    dataSet = src.dataSet;
+    labelSet = src.labelSet;
+    distancefunc = src.distancefunc;
+}
+
 RegKNN::~RegKNN()
 {
 }

+ 4 - 1
regression/npregression/RegKNN.h

@@ -36,7 +36,10 @@ class RegKNN : public RegressionAlgorithm
   
   public:
     /** simple constructor */
-    RegKNN( const NICE::Config *conf, NICE::VectorDistance<double> *distancefunc = NULL );
+    RegKNN ( const NICE::Config *conf, NICE::VectorDistance<double> *distancefunc = NULL );
+    
+    /** copy constructor */
+    RegKNN ( const RegKNN & src );
     
     /** simple destructor */
     virtual ~RegKNN();

+ 1 - 0
regression/npregression/libdepend.inc

@@ -0,0 +1 @@
+$(call PKG_DEPEND_INT,vislearning/regression/regressionbase)