SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 */

SHOGUN Machine Learning Toolbox - Documentation