SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
LinearRidgeRegression.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Copyright (C) 2012 Soeren Sonnenburg
8  */
9 #include <shogun/lib/config.h>
10 
11 #ifdef HAVE_LAPACK
17 
18 using namespace shogun;
19 using namespace Eigen;
20 
23 {
24  init();
25 }
26 
29 {
30  init();
31 
32  m_tau=tau;
33  set_labels(lab);
34  set_features(data);
35 }
36 
37 void CLinearRidgeRegression::init()
38 {
39  m_tau=1e-6;
40 
41  SG_ADD(&m_tau, "tau", "Regularization parameter", MS_AVAILABLE);
42 }
43 
45 {
46  REQUIRE(m_labels,"No labels set\n");
47 
48  if (!data)
49  data=features;
50 
51  REQUIRE(data,"No features provided and no featured previously set\n");
52 
54  "Number of training vectors (%d) does not match number of labels (%d)\n",
56 
58  "Expected Dense Features (%d) but got (%d)\n",
59  C_DENSE, data->get_feature_class());
60 
61  REQUIRE(data->get_feature_type() == F_DREAL,
62  "Expected Real Features (%d) but got (%d)\n",
63  F_DREAL, data->get_feature_type());
64 
66  int32_t num_feat=feats->get_num_features();
67  int32_t num_vec=feats->get_num_vectors();
68 
69  SGMatrix<float64_t> kernel_matrix(num_feat,num_feat);
70  SGMatrix<float64_t> feats_matrix(feats->get_feature_matrix());
71  SGVector<float64_t> y(num_feat);
72  SGVector<float64_t> tau_vector(num_feat);
73 
74  tau_vector.zero();
75  tau_vector.add(m_tau);
76 
77  Map<MatrixXd> eigen_kernel_matrix(kernel_matrix.matrix, num_feat,num_feat);
78  Map<MatrixXd> eigen_feats_matrix(feats_matrix.matrix, num_feat,num_vec);
79  Map<VectorXd> eigen_y(y.vector, num_feat);
80  Map<VectorXd> eigen_labels(((CRegressionLabels*)m_labels)->get_labels(),num_vec);
81  Map<VectorXd> eigen_tau(tau_vector.vector, num_feat);
82 
83  eigen_kernel_matrix = eigen_feats_matrix*eigen_feats_matrix.transpose();
84 
85  eigen_kernel_matrix.diagonal() += eigen_tau;
86 
87  eigen_y = eigen_feats_matrix*eigen_labels ;
88 
89  LLT<MatrixXd> llt;
90  llt.compute(eigen_kernel_matrix);
91  if(llt.info() != Eigen::Success)
92  {
93  SG_WARNING("Features covariance matrix was not positive definite\n");
94  return false;
95  }
96  eigen_y = llt.solve(eigen_y);
97 
98  set_w(y);
99  return true;
100 }
101 
102 bool CLinearRidgeRegression::load(FILE* srcfile)
103 {
106  return false;
107 }
108 
109 bool CLinearRidgeRegression::save(FILE* dstfile)
110 {
113  return false;
114 }
115 #endif
#define SG_RESET_LOCALE
Definition: SGIO.h:86
Real Labels are real-valued labels.
virtual void set_w(const SGVector< float64_t > src_w)
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual int32_t get_num_labels() const =0
Definition: SGMatrix.h:20
virtual int32_t get_num_vectors() const =0
CLabels * m_labels
Definition: Machine.h:361
#define REQUIRE(x,...)
Definition: SGIO.h:206
#define SG_SET_LOCALE_C
Definition: SGIO.h:85
double float64_t
Definition: common.h:50
virtual bool save(FILE *dstfile)
virtual CLabels * get_labels()
Definition: Machine.cpp:76
virtual void set_features(CDotFeatures *feat)
virtual EFeatureClass get_feature_class() const =0
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
CDotFeatures * features
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual bool load(FILE *srcfile)
#define SG_WARNING(...)
Definition: SGIO.h:128
#define SG_ADD(...)
Definition: SGObject.h:84
virtual bool train_machine(CFeatures *data=NULL)
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:65
virtual EFeatureType get_feature_type() const =0
void add(const SGVector< T > x)
Definition: SGVector.cpp:279

SHOGUN Machine Learning Toolbox - Documentation