SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
15 
16 using namespace shogun;
17 
19 {
20  init();
21 }
22 
24 {
25 
26 }
27 
28 void CDataGenerator::init()
29 {
30 }
31 
33  int32_t dim, int32_t num_points, float64_t overlap)
34 {
35  int32_t points_per_class = num_points / num_classes;
36 
37  int32_t grid_size = (int32_t ) CMath::ceil(CMath::sqrt((float64_t ) num_classes));
38  float64_t cell_size = (float64_t ) 1 / grid_size;
39  SGVector<float64_t> grid_idx(dim);
40  for (index_t i=0; i<dim; i++)
41  grid_idx[i] = 0;
42 
43  SGMatrix<float64_t> points(dim+1, num_points);
44  int32_t points_idx = 0;
45  for (int32_t class_idx=0; class_idx<num_classes; class_idx++)
46  {
47  SGVector<float64_t> class_dim_centers(dim);
48  for (index_t i=0; i<dim; i++)
49  class_dim_centers[i] = (grid_idx[i] * cell_size + (grid_idx[i] + 1) * cell_size) / 2;
50 
51  for (index_t p=points_idx; p<points_per_class+points_idx; p++)
52  {
53  for (index_t i=0; i<dim; i++)
54  {
55  do
56  {
57  points(i, p) = CMath::normal_random(class_dim_centers[i], cell_size*0.5);
58  if ((points(i, p)>(grid_idx[i]+1)*cell_size) ||
59  (points(i, p)<grid_idx[i]*cell_size))
60  {
61  if (!(CMath::random(0.0, 1.0)<overlap))
62  continue;
63  }
64  break;
65  } while (true);
66  }
67  points(dim, p) = class_idx;
68  }
69  points_idx += points_per_class;
70  for (index_t i=dim-1; i>=0; i--)
71  {
72  grid_idx[i]++;
73  if (grid_idx[i]>=grid_size)
74  grid_idx[i] = 0;
75  else
76  break;
77  }
78  }
79  return points;
80 }
81 
83  index_t dim, float64_t mean_shift,
84  SGMatrix<float64_t> target)
85 {
86  /* evtl. allocate space */
88  dim, 2*m, target);
89 
90  /* fill matrix with normal data */
91  for (index_t i=0; i<2*m; ++i)
92  {
93  for (index_t j=0; j<dim; ++j)
94  result(j,i)=CMath::randn_double();
95 
96  /* mean shift for second half */
97  if (i>=m)
98  result(0,i)+=mean_shift;
99  }
100 
101  return result;
102 }
103 
105  float64_t d, float64_t angle, SGMatrix<float64_t> target)
106 {
107  /* evtl. allocate space */
109  2, m, target);
110 
111  /* rotation matrix */
113  rot(0, 0)=CMath::cos(angle);
114  rot(0, 1)=-CMath::sin(angle);
115  rot(1, 0)=CMath::sin(angle);
116  rot(1, 1)=CMath::cos(angle);
117 
118  /* generate signal in each dimension which is an equal mixture of two
119  * Gaussians */
120  for (index_t i=0; i<m; ++i)
121  {
122  result(0,i)=CMath::randn_double() + (CMath::random(0, 1) ? d : -d);
123  result(1,i)=CMath::randn_double() + (CMath::random(0, 1) ? d : -d);
124  }
125 
126  /* rotate result */
127  if (angle)
128  result=SGMatrix<float64_t>::matrix_multiply(rot, result);
129 
130  return result;
131 }
132 #ifdef HAVE_LAPACK
134 {
135  /* evtl. allocate space */
136  SGMatrix<float64_t> result =
138 
139  float64_t grid_distance = 5.0;
140  for (index_t i = 0; i < n; ++i)
141  {
142  SGVector<float64_t> mean(dim);
144 
145  mean.zero();
146  for (index_t k = 0; k < dim; ++k)
147  {
148  mean[k] = (i+1)*grid_distance;
149  if (k % (i+1) == 0)
150  mean[k] *= -1;
151  }
152  CGaussian* g = new CGaussian(mean, cov, DIAG);
153  for (index_t j = 0; j < m; ++j)
154  {
155  SGVector<float64_t> v = g->sample();
156  memcpy((result.matrix+j*result.num_rows+i*m*dim), v.vector, dim*sizeof(float64_t));
157  SG_FREE(v.vector);
158  }
159 
160  SG_UNREF(g);
161  }
162 
163  return result;
164 }
165 #endif /* HAVE_LAPACK */
static float64_t sin(float64_t x)
tanh(x), x being a complex128_t
Definition: Math.h:820
SGVector< float64_t > sample()
Definition: Gaussian.cpp:361
static SGMatrix< float64_t > generate_gaussians(index_t m, index_t n, index_t dim)
int32_t index_t
Definition: common.h:62
static float64_t ceil(float64_t d)
Definition: Math.h:416
static float32_t normal_random(float32_t mean, float32_t std_dev)
Definition: Math.h:1095
Gaussian distribution interface.
Definition: Gaussian.h:50
static float64_t randn_double()
Definition: Math.h:1132
static SGMatrix< float64_t > generate_sym_mix_gauss(index_t m, float64_t d, float64_t angle, SGMatrix< float64_t > target=SGMatrix< float64_t >())
static SGMatrix< float64_t > generate_mean_data(index_t m, index_t dim, float64_t mean_shift, SGMatrix< float64_t > target=SGMatrix< float64_t >())
static SGMatrix< T > get_allocated_matrix(index_t num_rows, index_t num_cols, SGMatrix< T > pre_allocated=SGMatrix< T >())
Definition: SGMatrix.cpp:997
index_t num_rows
Definition: SGMatrix.h:376
static uint64_t random()
Definition: Math.h:1019
static SGMatrix< float64_t > matrix_multiply(SGMatrix< float64_t > A, SGMatrix< float64_t > B, bool transpose_A=false, bool transpose_B=false, float64_t scale=1.0)
Definition: SGMatrix.cpp:947
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
static SGMatrix< float64_t > generate_checkboard_data(int32_t num_classes, int32_t dim, int32_t num_points, float64_t overlap)
double float64_t
Definition: common.h:50
static float64_t cos(float64_t x)
sinh(x), x being a complex128_t
Definition: Math.h:856
diagonal covariance
Definition: Gaussian.h:38
#define SG_UNREF(x)
Definition: SGObject.h:52
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
static float32_t sqrt(float32_t x)
Definition: Math.h:459
static SGMatrix< T > create_identity_matrix(index_t size, T scale)

SHOGUN Machine Learning Toolbox - Documentation