Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
Algorithms
Trainers
FisherLDA.h
Go to the documentation of this file.
1
//===========================================================================
2
/*!
3
*
4
*
5
* \brief FisherLDA
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
* <https://shark-ml.github.io/Shark/>
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_FISHERLDA_H
35
#define SHARK_ALGORITHMS_TRAINERS_FISHERLDA_H
36
37
#include <
shark/Core/DLLSupport.h
>
38
#include <
shark/Models/LinearModel.h
>
39
#include <
shark/Algorithms/Trainers/AbstractTrainer.h
>
40
41
namespace
shark
{
42
43
44
/// \brief Fisher's Linear Discriminant Analysis for data compression
45
///
46
/// Similar to PCA, \em Fisher's \em Linear \em Discriminant \em Analysis is a
47
/// method for reducing the datas dimensionality. In contrast to PCA it also uses
48
/// class information.
49
///
50
/// Consider the data's covariance matrix \f$ S \f$ and a unit vector \f$ u \f$
51
/// which defines a one-dimensional subspace of the data. Then, PCA would
52
/// maximmize the objective \f$ J(u) = u^T S u \f$, namely the datas variance in
53
/// the subspace. Fisher-LDA, however, maximizes
54
/// \f[
55
/// J(u) = ( u^T S_W u )^{-1} ( u^T S_B u ),
56
/// \f]
57
/// where \f$ S_B \f$ is the covariance matrix of the class-means and \f$ S_W \f$
58
/// is the average covariance matrix of all classes (in both cases, each class'
59
/// influence is weighted by it's size). As a result, Fisher-LDA finds a subspace
60
/// in which the class means are wide-spread while (in average) the variance of
61
/// each class becomes small. This leads to good lower-dimensional
62
/// representations of the data in cases where the classes are linearly
63
/// separable.
64
///
65
/// If a subspace with more than one dimension is requested, the above step is
66
/// executed consecutively to find the next optimal subspace-dimension
67
/// orthogonally to the others.
68
///
69
///
70
/// \b Note: the max. dimensionality for the subspace is \#NumOfClasses-1.
71
///
72
/// It is possible to choose how many dimnsions are used by setting the appropriate value
73
/// by calling setSubspaceDImension or in the constructor.
74
/// Also optionally whitening can be applied.
75
/// For more detailed information about Fisher-LDA, see \e Bishop, \e Pattern
76
/// \e Recognition \e and \e Machine \e Learning.
77
/// \ingroup supervised_trainer
78
class
FisherLDA
:
public
AbstractTrainer
<LinearModel<>, unsigned int>
79
{
80
public
:
81
/// Constructor
82
SHARK_EXPORT_SYMBOL
FisherLDA
(
bool
whitening
=
false
, std::size_t subspaceDimension = 0);
83
84
/// \brief From INameable: return the class name.
85
std::string
name
()
const
86
{
return
"Fisher-LDA"
; }
87
88
void
setSubspaceDimensions
(std::size_t dimensions){
89
m_subspaceDimensions
= dimensions;
90
}
91
92
std::size_t
subspaceDimensions
()
const
{
93
return
m_subspaceDimensions
;
94
}
95
96
/// check whether whitening mode is on
97
bool
whitening
()
const
{
98
return
m_whitening
;
99
}
100
101
/// if active, the model whitenes the inputs
102
void
setWhitening
(
bool
newWhitening){
103
m_whitening
= newWhitening;
104
}
105
106
/// Compute the FisherLDA solution for a multi-class problem.
107
SHARK_EXPORT_SYMBOL
void
train
(
LinearModel<>
& model,
LabeledData<RealVector, unsigned int>
const
& dataset);
108
109
protected
:
110
SHARK_EXPORT_SYMBOL
void
meanAndScatter
(
LabeledData<RealVector, unsigned int>
const
& dataset, RealVector&
mean
, RealMatrix& scatter);
111
bool
m_whitening
;
112
std::size_t
m_subspaceDimensions
;
113
};
114
115
116
}
117
#endif