SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
10 #include <shogun/lib/config.h>
11 
12 #ifdef HAVE_LAPACK
17 
18 using namespace shogun;
19 
22 {
23  init();
24 }
25 
28 {
29  init();
30 
31  m_tau=tau;
32  set_labels(lab);
33  set_features(data);
34 }
35 
36 void CLinearRidgeRegression::init()
37 {
38  m_tau=1e-6;
39 
40  SG_ADD(&m_tau, "tau", "Regularization parameter", MS_AVAILABLE);
41 }
42 
44 {
45  if (!m_labels)
46  SG_ERROR("No labels set\n")
47 
48  if (!data)
49  data=features;
50 
51  if (!data)
52  SG_ERROR("No features set\n")
53 
54  if (m_labels->get_num_labels() != data->get_num_vectors())
55  SG_ERROR("Number of training vectors does not match number of labels\n")
56 
57  if (data->get_feature_class() != C_DENSE)
58  SG_ERROR("Expected Dense Features\n")
59 
60  if (data->get_feature_type() != F_DREAL)
61  SG_ERROR("Expected Real Features\n")
62 
64  int32_t num_feat=feats->get_num_features();
65  int32_t num_vec=feats->get_num_vectors();
66 
67  // Get kernel matrix
68  SGMatrix<float64_t> kernel_matrix(num_feat,num_feat);
69  SGVector<float64_t> y(num_feat);
70 
71  // init
72  kernel_matrix.zero();
73  y.zero();
74 
75  for (int32_t i=0; i<num_feat; i++)
76  kernel_matrix.matrix[i+i*num_feat]+=m_tau;
77 
78  for (int32_t i=0; i<num_vec; i++)
79  {
81  ASSERT(v.vlen==num_feat)
82 
83  cblas_dger(CblasColMajor, num_feat,num_feat, 1.0, v.vector,1,
84  v.vector,1, kernel_matrix.matrix, num_feat);
85 
86  cblas_daxpy(num_feat, ((CRegressionLabels*) m_labels)->get_label(i), v.vector, 1, y.vector, 1);
87 
88  feats->free_feature_vector(v, i);
89  }
90 
91  clapack_dposv(CblasRowMajor,CblasUpper, num_feat, 1, kernel_matrix.matrix, num_feat,
92  y.vector, num_feat);
93 
94  set_w(y);
95 
96  return true;
97 }
98 
99 bool CLinearRidgeRegression::load(FILE* srcfile)
100 {
103  return false;
104 }
105 
106 bool CLinearRidgeRegression::save(FILE* dstfile)
107 {
110  return false;
111 }
112 #endif
#define SG_RESET_LOCALE
Definition: SGIO.h:86
Real Labels are real-valued labels.
ST * get_feature_vector(int32_t num, int32_t &len, bool &dofree)
int32_t get_num_features() const
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
virtual int32_t get_num_vectors() const =0
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
#define SG_SET_LOCALE_C
Definition: SGIO.h:85
index_t vlen
Definition: SGVector.h:494
#define ASSERT(x)
Definition: SGIO.h:201
virtual int32_t get_num_vectors() const
double float64_t
Definition: common.h:50
virtual bool save(FILE *dstfile)
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
void free_feature_vector(ST *feat_vec, int32_t num, bool dofree)
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual bool load(FILE *srcfile)
#define SG_ADD(...)
Definition: SGObject.h:81
virtual bool train_machine(CFeatures *data=NULL)
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:73
virtual EFeatureType get_feature_type() const =0

SHOGUN Machine Learning Toolbox - Documentation