SHOGUN
v2.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
features
DataGenerator.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) 2012 Heiko Strathmann
8
*/
9
10
#include <
shogun/lib/config.h
>
11
12
#include <
shogun/features/DataGenerator.h
>
13
#include <
shogun/mathematics/Math.h
>
14
#include <
shogun/distributions/Gaussian.h
>
15
16
using namespace
shogun;
17
18
CDataGenerator::CDataGenerator
() :
CSGObject
()
19
{
20
init();
21
}
22
23
CDataGenerator::~CDataGenerator
()
24
{
25
26
}
27
28
void
CDataGenerator::init()
29
{
30
}
31
32
SGMatrix<float64_t>
CDataGenerator::generate_mean_data
(
index_t
m,
33
index_t
dim,
float64_t
mean_shift,
34
SGMatrix<float64_t>
target)
35
{
36
/* evtl. allocate space */
37
SGMatrix<float64_t>
result=
SGMatrix<float64_t>::get_allocated_matrix
(
38
dim, 2*m, target);
39
40
/* fill matrix with normal data */
41
for
(
index_t
i=0; i<2*m; ++i)
42
{
43
for
(
index_t
j=0; j<dim; ++j)
44
result(j,i)=
CMath::randn_double
();
45
46
/* mean shift for second half */
47
if
(i>=m)
48
result(0,i)+=mean_shift;
49
}
50
51
return
result;
52
}
53
54
SGMatrix<float64_t>
CDataGenerator::generate_sym_mix_gauss
(
index_t
m,
55
float64_t
d,
float64_t
angle,
SGMatrix<float64_t>
target)
56
{
57
/* evtl. allocate space */
58
SGMatrix<float64_t>
result=
SGMatrix<float64_t>::get_allocated_matrix
(
59
2, m, target);
60
61
/* rotation matrix */
62
SGMatrix<float64_t>
rot=
SGMatrix<float64_t>
(2,2);
63
rot(0, 0)=
CMath::cos
(angle);
64
rot(0, 1)=-
CMath::sin
(angle);
65
rot(1, 0)=
CMath::sin
(angle);
66
rot(1, 1)=
CMath::cos
(angle);
67
68
/* generate signal in each dimension which is an equal mixture of two
69
* Gaussians */
70
for
(
index_t
i=0; i<m; ++i)
71
{
72
result(0,i)=
CMath::randn_double
() + (
CMath::random
(0, 1) ? d : -d);
73
result(1,i)=
CMath::randn_double
() + (
CMath::random
(0, 1) ? d : -d);
74
}
75
76
/* rotate result */
77
if
(angle)
78
result=
SGMatrix<float64_t>::matrix_multiply
(rot, result);
79
80
return
result;
81
}
82
#ifdef HAVE_LAPACK
83
SGMatrix<float64_t>
CDataGenerator::generate_gaussians
(
index_t
m,
index_t
n,
index_t
dim)
84
{
85
/* evtl. allocate space */
86
SGMatrix<float64_t>
result =
87
SGMatrix<float64_t>::get_allocated_matrix
(dim, n*m);
88
89
float64_t
grid_distance = 5.0;
90
for
(
index_t
i = 0; i < n; ++i)
91
{
92
SGVector<float64_t>
mean(dim);
93
SGMatrix<float64_t>
cov =
SGMatrix<float64_t>::create_identity_matrix
(dim, 1.0);
94
95
mean.
zero
();
96
for
(
index_t
k = 0; k < dim; ++k)
97
{
98
mean[k] = (i+1)*grid_distance;
99
if
(k % (i+1) == 0)
100
mean[k] *= -1;
101
}
102
CGaussian
* g =
new
CGaussian
(mean, cov,
DIAG
);
103
for
(
index_t
j = 0; j < m; ++j)
104
{
105
SGVector<float64_t>
v = g->
sample
();
106
memcpy((result.
matrix
+j*result.
num_rows
+i*m*dim), v.
vector
, dim*
sizeof
(
float64_t
));
107
}
108
109
SG_UNREF
(g);
110
}
111
112
return
result;
113
}
114
#endif
/* HAVE_LAPACK */
SHOGUN
Machine Learning Toolbox - Documentation