diff --git a/src/shogun/preprocessor/KernelPCA.cpp b/src/shogun/preprocessor/KernelPCA.cpp index 728603b9185..0752b2ffb90 100644 --- a/src/shogun/preprocessor/KernelPCA.cpp +++ b/src/shogun/preprocessor/KernelPCA.cpp @@ -18,16 +18,15 @@ #include #include #include -#include using namespace shogun; -CKernelPCA::CKernelPCA() : CDimensionReductionPreprocessor() +CKernelPCA::CKernelPCA() : CPreprocessor() { init(); } -CKernelPCA::CKernelPCA(CKernel* k) : CDimensionReductionPreprocessor() +CKernelPCA::CKernelPCA(CKernel* k) : CPreprocessor() { init(); set_kernel(k); @@ -121,6 +120,30 @@ void CKernelPCA::fit(CFeatures* features) SG_INFO("Done\n") } +CFeatures* CKernelPCA::apply(CFeatures* features, bool inplace) +{ + REQUIRE(m_initialized, "Transformer not fitted.\n"); + + if (!inplace) + features = dynamic_cast(features->clone()); + + if (dynamic_cast*>(features)) + { + auto feature_matrix = apply_to_feature_matrix(features); + features->as>()->set_feature_matrix( + feature_matrix); + return features; + } + + if (features->get_feature_class() == C_STRING) + { + return apply_to_string_features(features); + } + + SG_ERROR("Feature type %d not supported\n", features->get_feature_type()); + return NULL; +} + SGMatrix CKernelPCA::apply_to_feature_matrix(CFeatures* features) { ASSERT(m_initialized) @@ -165,7 +188,7 @@ CDenseFeatures* CKernelPCA::apply_to_string_features(CFeatures* featu m_kernel->init(features,m_init_features); - float64_t* new_feature_matrix = SG_MALLOC(float64_t, m_target_dim*num_vectors); + SGMatrix new_feature_matrix(m_target_dim * num_vectors); for (i=0; i* CKernelPCA::apply_to_string_features(CFeatures* featu return new CDenseFeatures(SGMatrix(new_feature_matrix,m_target_dim,num_vectors)); } + +EFeatureClass CKernelPCA::get_feature_class() +{ + return C_ANY; +} + +EFeatureType CKernelPCA::get_feature_type() +{ + return F_ANY; +} + +void CKernelPCA::set_target_dim(int32_t dim) +{ + ASSERT(dim > 0) + m_target_dim = dim; +} + +int32_t CKernelPCA::get_target_dim() const +{ + return m_target_dim; +} + +void CKernelPCA::set_kernel(CKernel* kernel) +{ + SG_REF(kernel); + SG_UNREF(m_kernel); + m_kernel = kernel; +} + +CKernel* CKernelPCA::get_kernel() const +{ + SG_REF(m_kernel); + return m_kernel; +} diff --git a/src/shogun/preprocessor/KernelPCA.h b/src/shogun/preprocessor/KernelPCA.h index a3514268bb6..e56acef94bc 100644 --- a/src/shogun/preprocessor/KernelPCA.h +++ b/src/shogun/preprocessor/KernelPCA.h @@ -9,10 +9,10 @@ #define KERNELPCA_H__ #include -#include #include #include #include +#include namespace shogun { @@ -28,7 +28,7 @@ class CKernel; * Retrieved from http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.32.8744 * */ -class CKernelPCA: public CDimensionReductionPreprocessor +class CKernelPCA : public CPreprocessor { public: /** default constructor @@ -44,12 +44,11 @@ class CKernelPCA: public CDimensionReductionPreprocessor virtual void fit(CFeatures* features); + virtual CFeatures* apply(CFeatures* features, bool inplace); + /// cleanup virtual void cleanup(); - /// apply preproc on feature matrix - /// result in feature matrix - /// return pointer to feature_matrix, i.e. f->get_feature_matrix(); virtual SGMatrix apply_to_feature_matrix(CFeatures* features); /// apply preproc on single feature vector @@ -77,12 +76,36 @@ class CKernelPCA: public CDimensionReductionPreprocessor return m_bias_vector; } + virtual EFeatureClass get_feature_class(); + + virtual EFeatureType get_feature_type(); + /** @return object name */ virtual const char* get_name() const { return "KernelPCA"; } /** @return the type of preprocessor */ virtual EPreprocessorType get_type() const { return P_KERNELPCA; } + /** setter for target dimension + * @param dim target dimension + */ + void set_target_dim(int32_t dim); + + /** getter for target dimension + * @return target dimension + */ + int32_t get_target_dim() const; + + /** setter for kernel + * @param kernel kernel to set + */ + void set_kernel(CKernel* kernel); + + /** getter for kernel + * @return kernel + */ + CKernel* get_kernel() const; + protected: /** default init */ @@ -102,6 +125,11 @@ class CKernelPCA: public CDimensionReductionPreprocessor /** true when already initialized */ bool m_initialized; + /** target dimension */ + int32_t m_target_dim; + + /** kernel to be used */ + CKernel* m_kernel; }; } #endif