Skip to content

Commit

Permalink
Added apply_to_string_features method to kPCA
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Aug 20, 2011
1 parent 8d8cfd8 commit 0bf8b18
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/shogun/preprocessor/KernelPCA.cpp
Expand Up @@ -76,9 +76,6 @@ bool CKernelPCA::init(CFeatures* features)
SG_REF(features);
m_init_features = features;

ASSERT(features->get_feature_class()==C_SIMPLE);
ASSERT(features->get_feature_type()==F_DREAL);

m_kernel->init(features,features);
SGMatrix<float64_t> kernel_matrix = m_kernel->get_kernel_matrix();
m_kernel->cleanup();
Expand Down Expand Up @@ -189,4 +186,36 @@ SGVector<float64_t> CKernelPCA::apply_to_feature_vector(SGVector<float64_t> vect
m_kernel->cleanup();
return result;
}

CSimpleFeatures<float64_t>* CKernelPCA::apply_to_string_features(CFeatures* features)
{
ASSERT(m_initialized);

int32_t num_vectors = features->get_num_vectors();
int32_t i,j,k;
int32_t n = m_transformation_matrix.num_cols;

m_kernel->init(features,m_init_features);

float64_t* new_feature_matrix = SG_MALLOC(float64_t, m_target_dim*num_vectors);

for (i=0; i<num_vectors; i++)
{
for (j=0; j<m_target_dim; j++)
new_feature_matrix[i*m_target_dim+j] = m_bias_vector.vector[j];

for (j=0; j<n; j++)
{
float64_t kij = m_kernel->kernel(i,j);

for (k=0; k<m_target_dim; k++)
new_feature_matrix[k+i*m_target_dim] += kij*m_transformation_matrix.matrix[(n-k-1)*n+j];
}
}

m_kernel->cleanup();

return new CSimpleFeatures<float64_t>(SGMatrix<float64_t>(new_feature_matrix,m_target_dim,num_vectors));
}

#endif
5 changes: 5 additions & 0 deletions src/shogun/preprocessor/KernelPCA.h
Expand Up @@ -56,6 +56,11 @@ class CKernelPCA: public CDimensionReductionPreprocessor
/// result in feature matrix
virtual SGVector<float64_t> apply_to_feature_vector(SGVector<float64_t> vector);

/** apply to string features
* @param features
*/
virtual CSimpleFeatures<float64_t>* apply_to_string_features(CFeatures* features);

/** get kernel */
CKernel* get_kernel() const
{
Expand Down

0 comments on commit 0bf8b18

Please sign in to comment.