SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
List of all members | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
CSparseFeatures< ST > Class Template Reference

Detailed Description

template<class ST>
class shogun::CSparseFeatures< ST >

Template class SparseFeatures implements sparse matrices.

Features are an array of SGSparseVector, sorted w.r.t. vec_index (increasing) and withing same vec_index w.r.t. feat_index (increasing);

Sparse feature vectors can be accessed via get_sparse_feature_vector() and should be freed (this operation is a NOP in most cases) via free_sparse_feature_vector().

As this is a template class it can directly be used for different data types like sparse matrices of real valued, integer, byte etc type.

(Partly) subset access is supported for this feature type. Simple use the (inherited) set_subset(), remove_subset() functions. If done, all calls that work with features are translated to the subset. See comments to find out whether it is supported for that method

Definition at line 52 of file SparseFeatures.h.

Inheritance diagram for CSparseFeatures< ST >:
Inheritance graph
[legend]

Public Member Functions

 CSparseFeatures (int32_t size=0)
 CSparseFeatures (SGSparseVector< ST > *src, int32_t num_feat, int32_t num_vec, bool copy=false)
 CSparseFeatures (SGSparseMatrix< ST > sparse)
 CSparseFeatures (SGMatrix< ST > dense)
 CSparseFeatures (const CSparseFeatures &orig)
 CSparseFeatures (CFile *loader)
virtual ~CSparseFeatures ()
void free_sparse_feature_matrix ()
void free_sparse_features ()
virtual CFeaturesduplicate () const
ST get_feature (int32_t num, int32_t index)
ST * get_full_feature_vector (int32_t num, int32_t &len)
SGVector< ST > get_full_feature_vector (int32_t num)
virtual int32_t get_nnz_features_for_vector (int32_t num)
SGSparseVector< ST > get_sparse_feature_vector (int32_t num)
ST dense_dot (ST alpha, int32_t num, ST *vec, int32_t dim, ST b)
void add_to_dense_vec (float64_t alpha, int32_t num, float64_t *vec, int32_t dim, bool abs_val=false)
void free_sparse_feature_vector (SGSparseVector< ST > vec, int32_t num)
SGSparseVector< ST > * get_sparse_feature_matrix (int32_t &num_feat, int32_t &num_vec)
SGSparseMatrix< ST > get_sparse_feature_matrix ()
CSparseFeatures< ST > * get_transposed ()
SGSparseVector< ST > * get_transposed (int32_t &num_feat, int32_t &num_vec)
void set_sparse_feature_matrix (SGSparseMatrix< ST > sm)
SGMatrix< ST > get_full_feature_matrix ()
virtual bool set_full_feature_matrix (SGMatrix< ST > full)
virtual bool apply_preprocessor (bool force_preprocessing=false)
virtual int32_t get_size ()
bool obtain_from_simple (CSimpleFeatures< ST > *sf)
virtual int32_t get_num_vectors () const
int32_t get_num_features ()
int32_t set_num_features (int32_t num)
virtual EFeatureClass get_feature_class ()
virtual EFeatureType get_feature_type ()
void free_feature_vector (SGSparseVector< ST > vec, int32_t num)
int64_t get_num_nonzero_entries ()
float64_tcompute_squared (float64_t *sq)
float64_t compute_squared_norm (CSparseFeatures< float64_t > *lhs, float64_t *sq_lhs, int32_t idx_a, CSparseFeatures< float64_t > *rhs, float64_t *sq_rhs, int32_t idx_b)
void load (CFile *loader)
void save (CFile *writer)
CLabelsload_svmlight_file (char *fname, bool do_sort_features=true)
void sort_features ()
bool write_svmlight_file (char *fname, CLabels *label)
virtual int32_t get_dim_feature_space () const
virtual float64_t dot (int32_t vec_idx1, CDotFeatures *df, int32_t vec_idx2)
virtual float64_t dense_dot (int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)
virtual void * get_feature_iterator (int32_t vector_index)
virtual bool get_next_feature (int32_t &index, float64_t &value, void *iterator)
virtual void free_feature_iterator (void *iterator)
virtual CFeaturescopy_subset (SGVector< index_t > indices)
virtual const char * get_name () const
- Public Member Functions inherited from CDotFeatures
 CDotFeatures (int32_t size=0)
 CDotFeatures (const CDotFeatures &orig)
 CDotFeatures (CFile *loader)
virtual ~CDotFeatures ()
virtual void dense_dot_range (float64_t *output, int32_t start, int32_t stop, float64_t *alphas, float64_t *vec, int32_t dim, float64_t b)
virtual void dense_dot_range_subset (int32_t *sub_index, int32_t num, float64_t *output, float64_t *alphas, float64_t *vec, int32_t dim, float64_t b)
float64_t get_combined_feature_weight ()
void set_combined_feature_weight (float64_t nw)
SGMatrix< float64_tget_computed_dot_feature_matrix ()
SGVector< float64_tget_computed_dot_feature_vector (int32_t num)
void benchmark_add_to_dense_vector (int32_t repeats=5)
void benchmark_dense_dot_range (int32_t repeats=5)
virtual SGVector< float64_tget_mean ()
virtual SGMatrix< float64_tget_cov ()
- Public Member Functions inherited from CFeatures
 CFeatures (int32_t size=0)
 CFeatures (const CFeatures &orig)
 CFeatures (CFile *loader)
virtual ~CFeatures ()
virtual int32_t add_preprocessor (CPreprocessor *p)
 set preprocessor
virtual CPreprocessordel_preprocessor (int32_t num)
 del current preprocessor
CPreprocessorget_preprocessor (int32_t num)
 get current preprocessor
void set_preprocessed (int32_t num)
bool is_preprocessed (int32_t num)
int32_t get_num_preprocessed ()
 get whether specified preprocessor (or all if num=1) was/were already applied
int32_t get_num_preprocessors () const
void clean_preprocessors ()
int32_t get_cache_size ()
virtual bool reshape (int32_t num_features, int32_t num_vectors)
void list_feature_obj ()
bool check_feature_compatibility (CFeatures *f)
bool has_property (EFeatureProperty p)
void set_property (EFeatureProperty p)
void unset_property (EFeatureProperty p)
virtual void set_subset (CSubset *subset)
virtual void remove_subset ()
virtual void subset_changed_post ()
index_t subset_idx_conversion (index_t idx) const
bool has_subset () const
- Public Member Functions inherited from CSGObject
 CSGObject ()
 CSGObject (const CSGObject &orig)
virtual ~CSGObject ()
virtual bool is_generic (EPrimitiveType *generic) const
template<class T >
void set_generic ()
void unset_generic ()
virtual void print_serializable (const char *prefix="")
virtual bool save_serializable (CSerializableFile *file, const char *prefix="")
virtual bool load_serializable (CSerializableFile *file, const char *prefix="")
void set_global_io (SGIO *io)
SGIOget_global_io ()
void set_global_parallel (Parallel *parallel)
Parallelget_global_parallel ()
void set_global_version (Version *version)
Versionget_global_version ()
SGVector< char * > get_modelsel_names ()
char * get_modsel_param_descr (const char *param_name)
index_t get_modsel_param_index (const char *param_name)

Static Public Member Functions

static ST sparse_dot (ST alpha, SGSparseVectorEntry< ST > *avec, int32_t alen, SGSparseVectorEntry< ST > *bvec, int32_t blen)
static void clean_tsparse (SGSparseVector< ST > *sfm, int32_t num_vec)
- Static Public Member Functions inherited from CDotFeatures
static void * dense_dot_range_helper (void *p)

Protected Member Functions

virtual SGSparseVectorEntry< ST > * compute_sparse_feature_vector (int32_t num, int32_t &len, SGSparseVectorEntry< ST > *target=NULL)
- Protected Member Functions inherited from CDotFeatures
void display_progress (int32_t start, int32_t stop, int32_t v)

Protected Attributes

int32_t num_vectors
 total number of vectors
int32_t num_features
 total number of features
SGSparseVector< ST > * sparse_feature_matrix
 array of sparse vectors of size num_vectors
CCache< SGSparseVectorEntry
< ST > > * 
feature_cache
- Protected Attributes inherited from CDotFeatures
float64_t combined_weight
 feature weighting in combined dot features
- Protected Attributes inherited from CFeatures
CSubsetm_subset

Additional Inherited Members

- Public Attributes inherited from CSGObject
SGIOio
Parallelparallel
Versionversion
Parameterm_parameters
Parameterm_model_selection_parameters

Constructor & Destructor Documentation

CSparseFeatures ( int32_t  size = 0)

constructor

Parameters
sizecache size

Definition at line 13 of file SparseFeatures.cpp.

CSparseFeatures ( SGSparseVector< ST > *  src,
int32_t  num_feat,
int32_t  num_vec,
bool  copy = false 
)

convenience constructor that creates sparse features from the ones passed as argument

Parameters
srcdense feature matrix
num_featnumber of features
num_vecnumber of vectors
copytrue to copy feature matrix

Definition at line 20 of file SparseFeatures.cpp.

CSparseFeatures ( SGSparseMatrix< ST >  sparse)

convenience constructor that creates sparse features from sparse features

Parameters
sparsesparse matrix

Definition at line 42 of file SparseFeatures.cpp.

CSparseFeatures ( SGMatrix< ST >  dense)

convenience constructor that creates sparse features from dense features

Parameters
densedense feature matrix

Definition at line 51 of file SparseFeatures.cpp.

CSparseFeatures ( const CSparseFeatures< ST > &  orig)

copy constructor

Definition at line 60 of file SparseFeatures.cpp.

CSparseFeatures ( CFile loader)

constructor loading features from file

Parameters
loaderFile object to load data from

Definition at line 83 of file SparseFeatures.cpp.

~CSparseFeatures ( )
virtual

default destructor

Definition at line 92 of file SparseFeatures.cpp.

Member Function Documentation

void add_to_dense_vec ( float64_t  alpha,
int32_t  num,
float64_t vec,
int32_t  dim,
bool  abs_val = false 
)
virtual

add a sparse feature vector onto a dense one dense+=alpha*sparse

possible with subset

Parameters
alphascalar to multiply with
numindex of feature vector
vecdense vector
dimlength of the dense vector
abs_valif true, do dense+=alpha*abs(sparse)

Implements CDotFeatures.

Definition at line 331 of file SparseFeatures.cpp.

bool apply_preprocessor ( bool  force_preprocessing = false)
virtual

apply preprocessor

possible with subset

Parameters
force_preprocessingif preprocssing shall be forced
Returns
if applying was successful

Definition at line 596 of file SparseFeatures.cpp.

void clean_tsparse ( SGSparseVector< ST > *  sfm,
int32_t  num_vec 
)
static

clean SGSparseVector

Parameters
sfmsparse feature matrix
num_vecnumber of vectors in matrix

Definition at line 394 of file SparseFeatures.cpp.

SGSparseVectorEntry< ST > * compute_sparse_feature_vector ( int32_t  num,
int32_t &  len,
SGSparseVectorEntry< ST > *  target = NULL 
)
protectedvirtual

compute feature vector for sample num if target is set the vector is written to target len is returned by reference

NOT IMPLEMENTED!

Parameters
numnum
lenlen
targettarget

Definition at line 1117 of file SparseFeatures.cpp.

float64_t * compute_squared ( float64_t sq)

compute a^2 on all feature vectors

possible with subset

Parameters
sqthe square for each vector is stored in here
Returns
the square for each vector

Definition at line 676 of file SparseFeatures.cpp.

float64_t compute_squared_norm ( CSparseFeatures< float64_t > *  lhs,
float64_t sq_lhs,
int32_t  idx_a,
CSparseFeatures< float64_t > *  rhs,
float64_t sq_rhs,
int32_t  idx_b 
)

compute (a-b)^2 (== a^2+b^2-2ab) usually called by kernels'/distances' compute functions works on two feature vectors, although it is a member of a single feature: can either be called by lhs or rhs.

possible wiht subsets on lhs or rhs

Parameters
lhsleft-hand side features
sq_lhssquared values of left-hand side
idx_aindex of left-hand side's vector to compute
rhsright-hand side features
sq_rhssquared values of right-hand side
idx_bindex of right-hand side's vector to compute

Definition at line 695 of file SparseFeatures.cpp.

CFeatures * copy_subset ( SGVector< index_t indices)
virtual

Creates a new CFeatures instance containing copies of the elements which are specified by the provided indices.

Parameters
indicesindices of feature elements to copy
Returns
new CFeatures instance with copies of feature data

Reimplemented from CFeatures.

Definition at line 1092 of file SparseFeatures.cpp.

ST dense_dot ( ST  alpha,
int32_t  num,
ST *  vec,
int32_t  dim,
ST  b 
)

compute the dot product between dense weights and a sparse feature vector alpha * sparse^T * w + b

possible with subset

Parameters
alphascalar to multiply with
numindex of feature vector
vecdense vector to compute dot product with
dimlength of the dense vector
bbias
Returns
dot product between dense weights and a sparse feature vector

Definition at line 310 of file SparseFeatures.cpp.

float64_t dense_dot ( int32_t  vec_idx1,
const float64_t vec2,
int32_t  vec2_len 
)
virtual

compute dot product between vector1 and a dense vector

possible with subset

Parameters
vec_idx1index of first vector
vec2pointer to real valued vector
vec2_lenlength of real valued vector

Implements CDotFeatures.

Definition at line 1027 of file SparseFeatures.cpp.

float64_t dot ( int32_t  vec_idx1,
CDotFeatures df,
int32_t  vec_idx2 
)
virtual

compute dot product between vector1 and vector2, appointed by their indices

possible with subset of this instance and of DotFeatures

Parameters
vec_idx1index of first vector
dfDotFeatures (of same kind) to compute dot product with
vec_idx2index of second vector

Implements CDotFeatures.

Definition at line 1008 of file SparseFeatures.cpp.

CFeatures * duplicate ( ) const
virtual

duplicate feature object

Returns
feature object

Implements CFeatures.

Definition at line 110 of file SparseFeatures.cpp.

void free_feature_iterator ( void *  iterator)
virtual

clean up iterator call this function with the iterator returned by get_first_feature

Parameters
iteratoras returned by get_first_feature

Implements CDotFeatures.

Definition at line 1082 of file SparseFeatures.cpp.

void free_feature_vector ( SGSparseVector< ST >  vec,
int32_t  num 
)

free feature vector

possible with subset

Parameters
vecfeature vector to free
numindex of vector in cache

Definition at line 658 of file SparseFeatures.cpp.

void free_sparse_feature_matrix ( )

free sparse feature matrix

any subset is removed

Definition at line 96 of file SparseFeatures.cpp.

void free_sparse_feature_vector ( SGSparseVector< ST >  vec,
int32_t  num 
)

free sparse feature vector

possible with subset

Parameters
vecfeature vector to free
numindex of this vector in the cache

Definition at line 365 of file SparseFeatures.cpp.

void free_sparse_features ( )

free sparse feature matrix and cache

any subset is removed

Definition at line 104 of file SparseFeatures.cpp.

int32_t get_dim_feature_space ( ) const
virtual

obtain the dimensionality of the feature space

(not mix this up with the dimensionality of the input space, usually obtained via get_num_features())

Returns
dimensionality

Implements CDotFeatures.

Definition at line 1003 of file SparseFeatures.cpp.

ST get_feature ( int32_t  num,
int32_t  index 
)

get a single feature

possible with subset

Parameters
numnumber of feature vector to retrieve
indexindex of feature in this vector
Returns
sum of features that match dimension index and 0 if none is found

Definition at line 115 of file SparseFeatures.cpp.

EFeatureClass get_feature_class ( )
virtual

get feature class

Returns
feature class SPARSE

Implements CFeatures.

Definition at line 653 of file SparseFeatures.cpp.

void * get_feature_iterator ( int32_t  vector_index)
virtual

iterate over the non-zero features

call get_feature_iterator first, followed by get_next_feature and free_feature_iterator to cleanup

possible with subset

Parameters
vector_indexthe index of the vector over whose components to iterate over
Returns
feature iterator (to be passed to get_next_feature)

Implements CDotFeatures.

Definition at line 1050 of file SparseFeatures.cpp.

virtual EFeatureType get_feature_type ( )
virtual

get feature type

Returns
templated feature type

Implements CFeatures.

SGMatrix< ST > get_full_feature_matrix ( )

gets a copy of a full feature matrix

possible with subset

Returns
full dense feature matrix

Definition at line 477 of file SparseFeatures.cpp.

ST * get_full_feature_vector ( int32_t  num,
int32_t &  len 
)

converts a sparse feature vector into a dense one preprocessed compute_feature_vector caller cleans up

Parameters
numindex of feature vector
lenlength is returned by reference
Returns
dense feature vector

Definition at line 136 of file SparseFeatures.cpp.

SGVector< ST > get_full_feature_vector ( int32_t  num)

get the fully expanded dense feature vector num

Returns
dense feature vector
Parameters
numindex of feature vector

Definition at line 160 of file SparseFeatures.cpp.

virtual const char* get_name ( ) const
virtual
Returns
object name

Implements CSGObject.

Definition at line 526 of file SparseFeatures.h.

bool get_next_feature ( int32_t &  index,
float64_t value,
void *  iterator 
)
virtual

iterate over the non-zero features

call this function with the iterator returned by get_first_feature and call free_feature_iterator to cleanup

Parameters
indexis returned by reference (-1 when not available)
valueis returned by reference
iteratoras returned by get_first_feature
Returns
true if a new non-zero feature got returned

Implements CDotFeatures.

Definition at line 1068 of file SparseFeatures.cpp.

int32_t get_nnz_features_for_vector ( int32_t  num)
virtual

get number of non-zero features in vector

Parameters
numwhich vector
Returns
number of non-zero features in vector

Implements CDotFeatures.

Definition at line 190 of file SparseFeatures.cpp.

int32_t get_num_features ( )

get number of features

Returns
number of features

Definition at line 640 of file SparseFeatures.cpp.

int64_t get_num_nonzero_entries ( )

get number of non-zero entries in sparse feature matrix

Returns
number of non-zero entries in sparse feature matrix

Definition at line 666 of file SparseFeatures.cpp.

int32_t get_num_vectors ( ) const
virtual

get number of feature vectors, possibly of subset

Returns
number of feature vectors

Implements CFeatures.

Definition at line 635 of file SparseFeatures.cpp.

int32_t get_size ( )
virtual

get memory footprint of one feature

Returns
memory footprint of one feature

Implements CFeatures.

Definition at line 622 of file SparseFeatures.cpp.

SGSparseVector< ST > * get_sparse_feature_matrix ( int32_t &  num_feat,
int32_t &  num_vec 
)

get the pointer to the sparse feature matrix num_feat,num_vectors are returned by reference

not possible with subset

Parameters
num_featnumber of features in matrix
num_vecnumber of vectors in matrix
Returns
feature matrix

Definition at line 373 of file SparseFeatures.cpp.

SGSparseMatrix< ST > get_sparse_feature_matrix ( )

get the sparse feature matrix

not possible with subset

Returns
sparse matrix

Definition at line 384 of file SparseFeatures.cpp.

SGSparseVector< ST > get_sparse_feature_vector ( int32_t  num)

get sparse feature vector for sample num from the matrix as it is if matrix is initialized, else return preprocessed compute_feature_vector

possible with subset

Parameters
numindex of feature vector
Returns
sparse feature vector

Definition at line 198 of file SparseFeatures.cpp.

CSparseFeatures< ST > * get_transposed ( )

get a transposed copy of the features

possible with subset

Returns
transposed copy

Definition at line 405 of file SparseFeatures.cpp.

SGSparseVector< ST > * get_transposed ( int32_t &  num_feat,
int32_t &  num_vec 
)

compute and return the transpose of the sparse feature matrix which will be prepocessed. num_feat, num_vectors are returned by reference caller has to clean up

possible with subset

Parameters
num_featnumber of features in matrix
num_vecnumber of vectors in matrix
Returns
transposed sparse feature matrix

Definition at line 413 of file SparseFeatures.cpp.

void load ( CFile loader)
virtual

load features from file

any subset is removed before

Parameters
loaderFile object to load data from

Reimplemented from CFeatures.

CLabels * load_svmlight_file ( char *  fname,
bool  do_sort_features = true 
)

load features from file

any subset is removed before

Parameters
fnamefilename to load from
do_sort_featuresif true features will be sorted to ensure they are in ascending order
Returns
label object with corresponding labels

Definition at line 755 of file SparseFeatures.cpp.

bool obtain_from_simple ( CSimpleFeatures< ST > *  sf)

obtain sparse features from simple features

subset on input is ignored, subset of this instance is removed

Parameters
sfsimple features
Returns
if obtaining was successful

Definition at line 627 of file SparseFeatures.cpp.

void save ( CFile writer)
virtual

save features to file

not possible with subset

Parameters
writerFile object to write data to

Reimplemented from CFeatures.

bool set_full_feature_matrix ( SGMatrix< ST >  full)
virtual

creates a sparse feature matrix from a full dense feature matrix necessary to set feature_matrix, num_features and num_vectors where num_features is the column offset, and columns are linear in memory see above for definition of sparse_feature_matrix

any subset is removed before

Parameters
fullfull feature matrix

Definition at line 506 of file SparseFeatures.cpp.

int32_t set_num_features ( int32_t  num)

set number of features

Sometimes when loading sparse features not all possible dimensions are used. This may pose a problem to classifiers when being applied to higher dimensional test-data. This function allows to artificially explode the feature space

Parameters
numthe number of features, must be larger than the current number of features
Returns
previous number of features

Definition at line 645 of file SparseFeatures.cpp.

void set_sparse_feature_matrix ( SGSparseMatrix< ST >  sm)

set sparse feature matrix

not possible with subset

Parameters
smsparse feature matrix

Definition at line 463 of file SparseFeatures.cpp.

void sort_features ( )

ensure that features occur in ascending order, only call when no preprocessors are attached

not possiblwe with subset

Definition at line 921 of file SparseFeatures.cpp.

ST sparse_dot ( ST  alpha,
SGSparseVectorEntry< ST > *  avec,
int32_t  alen,
SGSparseVectorEntry< ST > *  bvec,
int32_t  blen 
)
static

compute the dot product between two sparse feature vectors alpha * vec^T * vec

Parameters
alphascalar to multiply with
avecfirst sparse feature vector
alenavec's length
bvecsecond sparse feature vector
blenbvec's length
Returns
dot product between the two sparse feature vectors

Definition at line 262 of file SparseFeatures.cpp.

bool write_svmlight_file ( char *  fname,
CLabels label 
)

write features to file using svm light format

not possible with subset

Parameters
fnamefilename to write to
labelLabel object (number of labels must correspond to number of features)
Returns
true if successful

Definition at line 966 of file SparseFeatures.cpp.

Member Data Documentation

CCache< SGSparseVectorEntry<ST> >* feature_cache
protected

feature cache

Definition at line 557 of file SparseFeatures.h.

int32_t num_features
protected

total number of features

Definition at line 551 of file SparseFeatures.h.

int32_t num_vectors
protected

total number of vectors

Definition at line 548 of file SparseFeatures.h.

SGSparseVector<ST>* sparse_feature_matrix
protected

array of sparse vectors of size num_vectors

Definition at line 554 of file SparseFeatures.h.


The documentation for this class was generated from the following files:

SHOGUN Machine Learning Toolbox - Documentation