Skip to content

Commit

Permalink
Implement apply in kernel pca
Browse files Browse the repository at this point in the history
  • Loading branch information
vinx13 authored and vigsterkr committed Jun 11, 2018
1 parent c50338d commit e394405
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 9 deletions.
65 changes: 61 additions & 4 deletions src/shogun/preprocessor/KernelPCA.cpp
Expand Up @@ -18,16 +18,15 @@
#include <shogun/kernel/Kernel.h>
#include <shogun/lib/common.h>
#include <shogun/mathematics/linalg/LinalgNamespace.h>
#include <shogun/preprocessor/DimensionReductionPreprocessor.h>

using namespace shogun;

CKernelPCA::CKernelPCA() : CDimensionReductionPreprocessor()
CKernelPCA::CKernelPCA() : CPreprocessor()
{
init();
}

CKernelPCA::CKernelPCA(CKernel* k) : CDimensionReductionPreprocessor()
CKernelPCA::CKernelPCA(CKernel* k) : CPreprocessor()
{
init();
set_kernel(k);
Expand Down Expand Up @@ -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<CFeatures*>(features->clone());

if (dynamic_cast<CDenseFeatures<float64_t>*>(features))
{
auto feature_matrix = apply_to_feature_matrix(features);
features->as<CDenseFeatures<float64_t>>()->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<float64_t> CKernelPCA::apply_to_feature_matrix(CFeatures* features)
{
ASSERT(m_initialized)
Expand Down Expand Up @@ -165,7 +188,7 @@ CDenseFeatures<float64_t>* 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<float64_t> new_feature_matrix(m_target_dim * num_vectors);

for (i=0; i<num_vectors; i++)
{
Expand All @@ -185,3 +208,37 @@ CDenseFeatures<float64_t>* CKernelPCA::apply_to_string_features(CFeatures* featu

return new CDenseFeatures<float64_t>(SGMatrix<float64_t>(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;
}
38 changes: 33 additions & 5 deletions src/shogun/preprocessor/KernelPCA.h
Expand Up @@ -9,10 +9,10 @@
#define KERNELPCA_H__
#include <shogun/lib/config.h>

#include <shogun/preprocessor/DimensionReductionPreprocessor.h>
#include <shogun/features/Features.h>
#include <shogun/kernel/Kernel.h>
#include <shogun/lib/common.h>
#include <shogun/preprocessor/DensePreprocessor.h>

namespace shogun
{
Expand All @@ -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
Expand All @@ -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<float64_t> apply_to_feature_matrix(CFeatures* features);

/// apply preproc on single feature vector
Expand Down Expand Up @@ -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 */
Expand All @@ -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

0 comments on commit e394405

Please sign in to comment.