SHOGUN
v3.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
classifier
GaussianProcessBinaryClassification.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
* Written (W) 2013 Roman Votyakov
8
*/
9
10
#include <
shogun/lib/config.h
>
11
12
#ifdef HAVE_EIGEN3
13
14
#include <
shogun/classifier/GaussianProcessBinaryClassification.h
>
15
16
using namespace
shogun;
17
18
CGaussianProcessBinaryClassification::CGaussianProcessBinaryClassification
()
19
:
CGaussianProcessMachine
()
20
{
21
}
22
23
CGaussianProcessBinaryClassification::CGaussianProcessBinaryClassification
(
24
CInferenceMethod
* method) :
CGaussianProcessMachine
(method)
25
{
26
// set labels
27
m_labels
=method->
get_labels
();
28
}
29
30
CGaussianProcessBinaryClassification::~CGaussianProcessBinaryClassification
()
31
{
32
}
33
34
CBinaryLabels
*
CGaussianProcessBinaryClassification::apply_binary
(
35
CFeatures
* data)
36
{
37
// check whether given combination of inference method and likelihood
38
// function supports classification
39
REQUIRE
(
m_method
,
"Inference method should not be NULL\n"
)
40
CLikelihoodModel
* lik=
m_method
->
get_model
();
41
REQUIRE
(
m_method
->
supports_binary
(),
"%s with %s doesn't support "
42
"binary classification\n"
,
m_method
->
get_name
(), lik->
get_name
())
43
SG_UNREF
(lik);
44
45
// if regression data equals to NULL, then apply classification on training
46
// features
47
if
(!data)
48
data=
m_method
->
get_features
();
49
else
50
SG_REF
(data);
51
52
CBinaryLabels
* result=
new
CBinaryLabels
(
get_mean_vector
(data));
53
SG_UNREF
(data);
54
55
return
result;
56
}
57
58
bool
CGaussianProcessBinaryClassification::train_machine
(
CFeatures
* data)
59
{
60
// check whether given combination of inference method and likelihood
61
// function supports classification
62
REQUIRE
(
m_method
,
"Inference method should not be NULL\n"
)
63
CLikelihoodModel
* lik=
m_method
->get_model();
64
REQUIRE
(
m_method
->supports_binary(), "%s with %s doesn't support "
65
"binary classification\n",
m_method
->
get_name
(), lik->get_name())
66
SG_UNREF
(lik);
67
68
if (data)
69
m_method
->set_features(data);
70
71
// perform inference
72
m_method
->
update
();
73
74
return true;
75
}
76
77
SGVector
<
float64_t
>
CGaussianProcessBinaryClassification
::
get_mean_vector
(
78
CFeatures
* data)
79
{
80
// check whether given combination of inference method and likelihood
81
// function supports classification
82
REQUIRE
(
m_method
,
"Inference method should not be NULL\n"
)
83
CLikelihoodModel
* lik=
m_method
->
get_model
();
84
REQUIRE
(
m_method
->
supports_binary
(),
"%s with %s doesn't support "
85
"binary classification\n"
,
m_method
->
get_name
(), lik->
get_name
())
86
87
SG_REF
(data);
88
SGVector<float64_t>
mu=
get_posterior_means
(data);
89
SGVector<float64_t>
s2=
get_posterior_variances
(data);
90
SG_UNREF
(data);
91
92
// evaluate mean
93
mu=lik->
get_predictive_means
(mu, s2);
94
SG_UNREF
(lik);
95
96
return
mu;
97
}
98
99
SGVector<float64_t>
CGaussianProcessBinaryClassification::get_variance_vector
(
100
CFeatures
* data)
101
{
102
// check whether given combination of inference method and
103
// likelihood function supports classification
104
REQUIRE
(
m_method
,
"Inference method should not be NULL\n"
)
105
CLikelihoodModel
* lik=
m_method
->
get_model
();
106
REQUIRE
(
m_method
->
supports_binary
(),
"%s with %s doesn't support "
107
"binary classification\n"
,
m_method
->
get_name
(), lik->
get_name
())
108
109
SG_REF
(data);
110
SGVector<float64_t>
mu=
get_posterior_means
(data);
111
SGVector<float64_t>
s2=
get_posterior_variances
(data);
112
SG_UNREF
(data);
113
114
// evaluate variance
115
s2=lik->
get_predictive_variances
(mu, s2);
116
SG_UNREF
(lik);
117
118
return
s2;
119
}
120
121
SGVector<float64_t>
CGaussianProcessBinaryClassification::get_probabilities(
122
CFeatures
* data)
123
{
124
// check whether given combination of inference method and likelihood
125
// function supports classification
126
REQUIRE
(
m_method
,
"Inference method should not be NULL\n"
)
127
CLikelihoodModel
* lik=
m_method
->get_model();
128
REQUIRE
(
m_method
->supports_binary(), "%s with %s doesn't support "
129
"binary classification\n",
m_method
->
get_name
(), lik->get_name())
130
131
SG_REF
(data);
132
SGVector
<
float64_t
> mu=
get_posterior_means
(data);
133
SGVector
<
float64_t
> s2=
get_posterior_variances
(data);
134
SG_UNREF
(data);
135
136
// evaluate log probabilities
137
SGVector
<
float64_t
> p=lik->get_predictive_log_probabilities(mu, s2);
138
SG_UNREF
(lik);
139
140
// evaluate probabilities
141
p.exp();
142
143
return p;
144
}
145
146
#endif
/* HAVE_EIGEN3 */
SHOGUN
Machine Learning Toolbox - Documentation