Shark machine learning library
About Shark
News!
Contribute
Credits and copyright
Downloads
Getting Started
Installation
Using the docs
Documentation
Tutorials
Quick references
Class list
Global functions
FAQ
Showroom
include
shark
Algorithms
Trainers
LDA.h
Go to the documentation of this file.
1
//===========================================================================
2
/*!
3
*
4
*
5
* \brief LDA
6
*
7
*
8
*
9
* \author O. Krause
10
* \date 2010
11
*
12
*
13
* \par Copyright 1995-2017 Shark Development Team
14
*
15
* <BR><HR>
16
* This file is part of Shark.
17
* <http://shark-ml.org/>
18
*
19
* Shark is free software: you can redistribute it and/or modify
20
* it under the terms of the GNU Lesser General Public License as published
21
* by the Free Software Foundation, either version 3 of the License, or
22
* (at your option) any later version.
23
*
24
* Shark is distributed in the hope that it will be useful,
25
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
* GNU Lesser General Public License for more details.
28
*
29
* You should have received a copy of the GNU Lesser General Public License
30
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
31
*
32
*/
33
//===========================================================================
34
#ifndef SHARK_ALGORITHMS_TRAINERS_LDA_H
35
#define SHARK_ALGORITHMS_TRAINERS_LDA_H
36
37
#include <
shark/Core/DLLSupport.h
>
38
#include <
shark/Core/IParameterizable.h
>
39
#include <
shark/Models/LinearModel.h
>
40
#include <
shark/Algorithms/Trainers/AbstractWeightedTrainer.h
>
41
42
namespace
shark
{
43
44
45
//!
46
//! \brief Linear Discriminant Analysis (LDA)
47
//!
48
//! This classes implements the well known linear discriminant analysis. LDA assumes that
49
//! every point is drawn from a multivariate normal distributions. Every class has its own mean
50
//! but all classes have the same covariance.
51
//!
52
//! An arbitrary number of classes is supported. The resulting model is of the form
53
//! \f[ \arg \max_c \log(p(x|c)*P(c)) \f]
54
//! where \f$ p(x|c) = \exp(-(x-m_c)^T(C+\alpha I)(x-m_c)) \f$.
55
//! \f$ m_c\f$ are the means of class c, \f$ C \f$ is the covariance matrix formed by all data points.
56
//! The regularization paramter \f$ \alpha \f$ is by default 0. The trainer is implemented such, that
57
//! it still works when C is singular, in this case the singular directions are ignored.
58
class
LDA
:
public
AbstractWeightedTrainer
<LinearClassifier<>, unsigned int>,
public
IParameterizable
<>
59
{
60
public
:
61
/// constructor
62
LDA
(
double
regularization
= 0.0){
63
setRegularization
(
regularization
);
64
}
65
66
/// \brief From INameable: return the class name.
67
std::string
name
()
const
68
{
return
"Linear Discriminant Analysis (LDA)"
; }
69
70
/// return the regularization constant
71
double
regularization
()
const
{
72
return
m_regularization
;
73
}
74
75
/// set the regularization constant. 0 means no regularization.
76
void
setRegularization
(
double
regularization
) {
77
RANGE_CHECK
(regularization >= 0.0);
78
m_regularization
=
regularization
;
79
}
80
81
/// inherited from IParameterizable; read the regularization parameter
82
RealVector
parameterVector
()
const
{
83
RealVector param(1);
84
param(0) =
m_regularization
;
85
return
param;
86
}
87
/// inherited from IParameterizable; set the regularization parameter
88
void
setParameterVector
(RealVector
const
& param) {
89
SIZE_CHECK
(param.size() == 1);
90
m_regularization
= param(0);
91
}
92
/// inherited from IParameterizable
93
size_t
numberOfParameters
()
const
{
94
return
1;
95
}
96
97
//! Compute the LDA solution for a multi-class problem.
98
SHARK_EXPORT_SYMBOL
void
train
(
LinearClassifier<>
& model,
LabeledData<RealVector, unsigned int>
const
& dataset);
99
//! Compute the LDA solution for a weighted multi-class problem.
100
SHARK_EXPORT_SYMBOL
void
train
(
LinearClassifier<>
& model,
WeightedLabeledData<RealVector, unsigned int>
const
& dataset);
101
102
protected
:
103
//!The regularization parameter \f$ \lambda \f$ adds
104
//! \f$ - \lambda I \f$ to the second moment matrix, where
105
//! \f$ I \f$ is the identity matrix
106
double
m_regularization
;
107
};
108
109
}
110
#endif
111