SHOGUN
v2.0.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2012 Heiko Strathmann 00008 */ 00009 00010 #include <shogun/lib/config.h> 00011 00012 #include <shogun/features/DataGenerator.h> 00013 #include <shogun/mathematics/Math.h> 00014 #include <shogun/distributions/Gaussian.h> 00015 00016 using namespace shogun; 00017 00018 CDataGenerator::CDataGenerator() : CSGObject() 00019 { 00020 init(); 00021 } 00022 00023 CDataGenerator::~CDataGenerator() 00024 { 00025 00026 } 00027 00028 void CDataGenerator::init() 00029 { 00030 } 00031 00032 SGMatrix<float64_t> CDataGenerator::generate_mean_data(index_t m, 00033 index_t dim, float64_t mean_shift, 00034 SGMatrix<float64_t> target) 00035 { 00036 /* evtl. allocate space */ 00037 SGMatrix<float64_t> result=SGMatrix<float64_t>::get_allocated_matrix( 00038 dim, 2*m, target); 00039 00040 /* fill matrix with normal data */ 00041 for (index_t i=0; i<2*m; ++i) 00042 { 00043 for (index_t j=0; j<dim; ++j) 00044 result(j,i)=CMath::randn_double(); 00045 00046 /* mean shift for second half */ 00047 if (i>=m) 00048 result(0,i)+=mean_shift; 00049 } 00050 00051 return result; 00052 } 00053 00054 SGMatrix<float64_t> CDataGenerator::generate_sym_mix_gauss(index_t m, 00055 float64_t d, float64_t angle, SGMatrix<float64_t> target) 00056 { 00057 /* evtl. allocate space */ 00058 SGMatrix<float64_t> result=SGMatrix<float64_t>::get_allocated_matrix( 00059 2, m, target); 00060 00061 /* rotation matrix */ 00062 SGMatrix<float64_t> rot=SGMatrix<float64_t>(2,2); 00063 rot(0, 0)=CMath::cos(angle); 00064 rot(0, 1)=-CMath::sin(angle); 00065 rot(1, 0)=CMath::sin(angle); 00066 rot(1, 1)=CMath::cos(angle); 00067 00068 /* generate signal in each dimension which is an equal mixture of two 00069 * Gaussians */ 00070 for (index_t i=0; i<m; ++i) 00071 { 00072 result(0,i)=CMath::randn_double() + (CMath::random(0, 1) ? d : -d); 00073 result(1,i)=CMath::randn_double() + (CMath::random(0, 1) ? d : -d); 00074 } 00075 00076 /* rotate result */ 00077 if (angle) 00078 result=SGMatrix<float64_t>::matrix_multiply(rot, result); 00079 00080 return result; 00081 } 00082 #ifdef HAVE_LAPACK 00083 SGMatrix<float64_t> CDataGenerator::generate_gaussians(index_t m, index_t n, index_t dim) 00084 { 00085 /* evtl. allocate space */ 00086 SGMatrix<float64_t> result = 00087 SGMatrix<float64_t>::get_allocated_matrix(dim, n*m); 00088 00089 float64_t grid_distance = 5.0; 00090 for (index_t i = 0; i < n; ++i) 00091 { 00092 SGVector<float64_t> mean(dim); 00093 SGMatrix<float64_t> cov = SGMatrix<float64_t>::create_identity_matrix(dim, 1.0); 00094 00095 mean.zero(); 00096 for (index_t k = 0; k < dim; ++k) 00097 { 00098 mean[k] = (i+1)*grid_distance; 00099 if (k % (i+1) == 0) 00100 mean[k] *= -1; 00101 } 00102 CGaussian* g = new CGaussian(mean, cov, DIAG); 00103 for (index_t j = 0; j < m; ++j) 00104 { 00105 SGVector<float64_t> v = g->sample(); 00106 memcpy((result.matrix+j*result.num_rows+i*m*dim), v.vector, dim*sizeof(float64_t)); 00107 } 00108 00109 SG_UNREF(g); 00110 } 00111 00112 return result; 00113 } 00114 #endif /* HAVE_LAPACK */