Skip to content

Commit

Permalink
Merge pull request #225 from karlnapf/master
Browse files Browse the repository at this point in the history
implementation of copy_subset
  • Loading branch information
Soeren Sonnenburg committed Jul 22, 2011
2 parents f8239e9 + 68f6f71 commit abdbf53
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/undocumented/libshogun/Makefile
Expand Up @@ -24,7 +24,8 @@ TARGETS = basic_minimal classifier_libsvm classifier_minimal_svm \
modelselection_grid_search_simple features_subset_labels \
features_subset_simple_features \
features_subset_sparse_features \
mathematics_confidence_intervals
mathematics_confidence_intervals \
features_copy_subset_simple_features


all: $(TARGETS)
Expand Down
@@ -0,0 +1,80 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011 Heiko Strathmann
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/

#include <shogun/base/init.h>
#include <shogun/features/SimpleFeatures.h>
#include <shogun/features/Subset.h>

using namespace shogun;

void print_message(FILE* target, const char* str)
{
fprintf(target, "%s", str);
}

int main(int argc, char **argv)
{
init_shogun(&print_message, &print_message, &print_message);

SGMatrix<float64_t> data(3, 10);
CSimpleFeatures<float64_t>* f=new CSimpleFeatures<float64_t>(data);
CMath::range_fill_vector(data.matrix, data.num_cols*data.num_rows, 1.0);
CMath::display_matrix(data.matrix, data.num_rows, data.num_cols,
"original feature data");

index_t offset_subset=1;
SGVector<index_t> feature_subset(8);
CMath::range_fill_vector(feature_subset.vector, feature_subset.vlen,
offset_subset);
CMath::display_vector(feature_subset.vector, feature_subset.vlen,
"feature subset");

f->set_subset(new CSubset(feature_subset));
SG_SPRINT("feature vectors after setting subset on original data:\n");
for (index_t i=0; i<f->get_num_vectors(); ++i)
{
SGVector<float64_t> vec=f->get_feature_vector(i);
SG_SPRINT("%i: ", i);
CMath::display_vector(vec.vector, vec.vlen);
f->free_feature_vector(vec, i);
}

index_t offset_copy=2;
SGVector<index_t> feature_copy_subset(4);
CMath::range_fill_vector(feature_copy_subset.vector,
feature_copy_subset.vlen, offset_copy);
CMath::display_vector(feature_copy_subset.vector, feature_copy_subset.vlen,
"indices that are to be copied");

CSimpleFeatures<float64_t>* subset_copy=
(CSimpleFeatures<float64_t>*)f->copy_subset(feature_copy_subset);

SGMatrix<float64_t> subset_copy_matrix=subset_copy->get_feature_matrix();
CMath::display_matrix(subset_copy_matrix.matrix,
subset_copy_matrix.num_rows, subset_copy_matrix.num_cols,
"copy matrix");

index_t num_its=subset_copy_matrix.num_rows*subset_copy_matrix.num_cols;
for (index_t i=0; i<num_its; ++i)
{
index_t idx=i+(offset_copy+offset_subset)*subset_copy_matrix.num_rows;
ASSERT(subset_copy_matrix.matrix[i]==data.matrix[idx]);
}

SG_UNREF(f);
SG_UNREF(subset_copy);
delete[] feature_copy_subset.vector;

SG_SPRINT("\nEND\n");
exit_shogun();

return 0;
}

22 changes: 22 additions & 0 deletions src/shogun/features/SimpleFeatures.h
Expand Up @@ -918,6 +918,27 @@ template<class ST> class CSimpleFeatures: public CDotFeatures {
delete[] it;
}

/** Creates a new CFeatures instance containing copies of the elements
* which are specified by the provided indices.
*
* @param indices indices of feature elements to copy
* @return new CFeatures instance with copies of feature data
*/
virtual CFeatures* copy_subset(SGVector<index_t> indices) const
{
SGMatrix<ST> feature_matrix_copy(num_features, indices.vlen);

for (index_t i=0; i<indices.vlen; ++i)
{
index_t real_idx=subset_idx_conversion(indices.vector[i]);
memcpy(&feature_matrix_copy.matrix[i*num_features],
&feature_matrix[real_idx*num_features],
num_features*sizeof(ST));
}

return new CSimpleFeatures(feature_matrix_copy);
}

/** @return object name */
inline virtual const char* get_name() const {
return "SimpleFeatures";
Expand All @@ -937,6 +958,7 @@ template<class ST> class CSimpleFeatures: public CDotFeatures {
*/
virtual ST* compute_feature_vector(int32_t num, int32_t& len, ST* target =
NULL) {
SG_NOTIMPLEMENTED;
len = 0;
return NULL;
}
Expand Down

0 comments on commit abdbf53

Please sign in to comment.