SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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

SHOGUN Machine Learning Toolbox - Documentation