CSparseFeatures< ST > Class Template Reference


Detailed Description

template<class ST>
class CSparseFeatures< ST >

Template class SparseFeatures implements sparse matrices.

Features are an array of TSparse, 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.

Definition at line 67 of file SparseFeatures.h.

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

List of all members.

Public Member Functions

 CSparseFeatures (int32_t size=0)
 CSparseFeatures (TSparse< ST > *src, int32_t num_feat, int32_t num_vec)
 CSparseFeatures (ST *src, int32_t num_feat, int32_t num_vec)
 CSparseFeatures (const CSparseFeatures &orig)
 CSparseFeatures (char *fname)
virtual ~CSparseFeatures ()
void free_sparse_feature_matrix ()
void free_sparse_features ()
virtual CFeaturesduplicate () const
ST * get_full_feature_vector (int32_t num, int32_t &len)
void get_full_feature_vector (ST **dst, int32_t *len, int32_t num)
virtual int32_t get_nnz_features_for_vector (int32_t num)
TSparseEntry< ST > * get_sparse_feature_vector (int32_t num, int32_t &len, bool &vfree)
ST sparse_dot (ST alpha, TSparseEntry< ST > *avec, int32_t alen, TSparseEntry< ST > *bvec, int32_t blen)
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 (TSparseEntry< ST > *feat_vec, int32_t num, bool free)
TSparse< ST > * get_sparse_feature_matrix (int32_t &num_feat, int32_t &num_vec)
void get_sparse_feature_matrix (TSparse< ST > **dst, int32_t *num_feat, int32_t *num_vec, int64_t *nnz)
void clean_tsparse (TSparse< ST > *sfm, int32_t num_vec)
TSparse< ST > * get_transposed (int32_t &num_feat, int32_t &num_vec)
virtual void set_sparse_feature_matrix (TSparse< ST > *src, int32_t num_feat, int32_t num_vec)
ST * get_full_feature_matrix (int32_t &num_feat, int32_t &num_vec)
void get_full_feature_matrix (ST **dst, int32_t *num_feat, int32_t *num_vec)
virtual bool set_full_feature_matrix (ST *src, int32_t num_feat, int32_t num_vec)
virtual bool apply_preproc (bool force_preprocessing=false)
virtual int32_t get_size ()
bool obtain_from_simple (CSimpleFeatures< ST > *sf)
virtual int32_t get_num_vectors ()
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 (TSparseEntry< ST > *feat_vec, int32_t num, bool free)
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)
CLabelsload_svmlight_file (char *fname)
bool write_svmlight_file (char *fname, CLabels *label)
virtual int32_t get_dim_feature_space ()
virtual float64_t dot (int32_t vec_idx1, int32_t vec_idx2)
virtual float64_t dense_dot (int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)
virtual const char * get_name () const
template<>
EFeatureType get_feature_type ()
template<>
EFeatureType get_feature_type ()
template<>
EFeatureType get_feature_type ()
template<>
EFeatureType get_feature_type ()
template<>
EFeatureType get_feature_type ()
template<>
EFeatureType get_feature_type ()
template<>
EFeatureType get_feature_type ()
template<>
EFeatureType get_feature_type ()
template<>
EFeatureType get_feature_type ()
template<>
EFeatureType get_feature_type ()
template<>
EFeatureType get_feature_type ()
template<>
EFeatureType get_feature_type ()

Protected Member Functions

virtual TSparseEntry< ST > * compute_sparse_feature_vector (int32_t num, int32_t &len, TSparseEntry< ST > *target=NULL)

Protected Attributes

int32_t num_vectors
 total number of vectors
int32_t num_features
 total number of features
TSparse< ST > * sparse_feature_matrix
 array of sparse vectors of size num_vectors
CCache< TSparseEntry< ST > > * feature_cache

Constructor & Destructor Documentation

template<class ST>
CSparseFeatures< ST >::CSparseFeatures ( int32_t  size = 0  ) 

constructor

Parameters:
size cache size

Definition at line 74 of file SparseFeatures.h.

template<class ST>
CSparseFeatures< ST >::CSparseFeatures ( TSparse< ST > *  src,
int32_t  num_feat,
int32_t  num_vec 
)

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

Parameters:
src dense feature matrix
num_feat number of features
num_vec number of vectors

Definition at line 86 of file SparseFeatures.h.

template<class ST>
CSparseFeatures< ST >::CSparseFeatures ( ST *  src,
int32_t  num_feat,
int32_t  num_vec 
)

convenience constructor that creates sparse features from dense features

Parameters:
src dense feature matrix
num_feat number of features
num_vec number of vectors

Definition at line 100 of file SparseFeatures.h.

template<class ST>
CSparseFeatures< ST >::CSparseFeatures ( const CSparseFeatures< ST > &  orig  ) 

copy constructor

Definition at line 108 of file SparseFeatures.h.

template<class ST>
CSparseFeatures< ST >::CSparseFeatures ( char *  fname  ) 

constructor

Parameters:
fname filename to load features from

Definition at line 132 of file SparseFeatures.h.

template<class ST>
virtual CSparseFeatures< ST >::~CSparseFeatures (  )  [virtual]

Definition at line 137 of file SparseFeatures.h.


Member Function Documentation

template<class ST>
void CSparseFeatures< ST >::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

Parameters:
alpha scalar to multiply with
num index of feature vector
vec dense vector
dim length of the dense vector
abs_val if true, do dense+=alpha*abs(sparse)

Implements CDotFeatures.

Definition at line 418 of file SparseFeatures.h.

template<class ST>
virtual bool CSparseFeatures< ST >::apply_preproc ( bool  force_preprocessing = false  )  [virtual]

apply preprocessor

Parameters:
force_preprocessing if preprocssing shall be forced
Returns:
if applying was successful

Definition at line 754 of file SparseFeatures.h.

template<class ST>
void CSparseFeatures< ST >::clean_tsparse ( TSparse< ST > *  sfm,
int32_t  num_vec 
)

clean TSparse

Parameters:
sfm sparse feature matrix
num_vec number of vectors in matrix

Definition at line 496 of file SparseFeatures.h.

template<class ST>
virtual TSparseEntry<ST>* CSparseFeatures< ST >::compute_sparse_feature_vector ( int32_t  num,
int32_t &  len,
TSparseEntry< ST > *  target = NULL 
) [protected, virtual]

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

NOT IMPLEMENTED!

Parameters:
num num
len len
target target

Definition at line 1246 of file SparseFeatures.h.

template<class ST>
float64_t* CSparseFeatures< ST >::compute_squared ( float64_t sq  ) 

compute a^2 on all feature vectors

Parameters:
sq the square for each vector is stored in here
Returns:
the square for each vector

Definition at line 877 of file SparseFeatures.h.

template<class ST>
float64_t CSparseFeatures< ST >::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.

Parameters:
lhs left-hand side features
sq_lhs squared values of left-hand side
idx_a index of left-hand side's vector to compute
rhs right-hand side features
sq_rhs squared values of right-hand side
idx_b index of right-hand side's vector to compute

Definition at line 910 of file SparseFeatures.h.

template<class ST>
virtual float64_t CSparseFeatures< ST >::dense_dot ( int32_t  vec_idx1,
const float64_t vec2,
int32_t  vec2_len 
) [virtual]

compute dot product between vector1 and a dense vector

Parameters:
vec_idx1 index of first vector
vec2 pointer to real valued vector
vec2_len length of real valued vector

Implements CDotFeatures.

Definition at line 1211 of file SparseFeatures.h.

template<class ST>
ST CSparseFeatures< 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

Parameters:
alpha scalar to multiply with
num index of feature vector
vec dense vector to compute dot product with
dim length of the dense vector
b bias
Returns:
dot product between dense weights and a sparse feature vector

Definition at line 389 of file SparseFeatures.h.

template<class ST>
virtual float64_t CSparseFeatures< ST >::dot ( int32_t  vec_idx1,
int32_t  vec_idx2 
) [virtual]

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

Parameters:
vec_idx1 index of first vector
vec_idx2 index of second vector

Implements CDotFeatures.

Definition at line 1190 of file SparseFeatures.h.

template<class ST>
virtual CFeatures* CSparseFeatures< ST >::duplicate (  )  const [virtual]

duplicate feature object

Returns:
feature object

Implements CFeatures.

Definition at line 167 of file SparseFeatures.h.

template<class ST>
void CSparseFeatures< ST >::free_feature_vector ( TSparseEntry< ST > *  feat_vec,
int32_t  num,
bool  free 
)

free feature vector

Parameters:
feat_vec feature vector to free
num index of vector in cache
free if vector really should be deleted

Definition at line 850 of file SparseFeatures.h.

template<class ST>
void CSparseFeatures< ST >::free_sparse_feature_matrix (  ) 

free sparse feature matrix

Definition at line 145 of file SparseFeatures.h.

template<class ST>
void CSparseFeatures< ST >::free_sparse_feature_vector ( TSparseEntry< ST > *  feat_vec,
int32_t  num,
bool  free 
)

free sparse feature vector

Parameters:
feat_vec feature vector to free
num index of this vector in the cache
free if vector should be really deleted

Definition at line 450 of file SparseFeatures.h.

template<class ST>
void CSparseFeatures< ST >::free_sparse_features (  ) 

free sparse feature matrix and cache

Definition at line 156 of file SparseFeatures.h.

template<class ST>
virtual int32_t CSparseFeatures< ST >::get_dim_feature_space (  )  [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 1179 of file SparseFeatures.h.

template<class ST>
virtual EFeatureClass CSparseFeatures< ST >::get_feature_class (  )  [virtual]

get feature class

Returns:
feature class SPARSE

Implements CFeatures.

Definition at line 836 of file SparseFeatures.h.

template<>
EFeatureType CSparseFeatures< floatmax_t >::get_feature_type (  )  [virtual]

get feature type the LONGREAL feature can deal with

Returns:
feature type LONGREAL

Implements CFeatures.

Definition at line 1370 of file SparseFeatures.h.

template<>
EFeatureType CSparseFeatures< float64_t >::get_feature_type (  )  [virtual]

get feature type the DREAL feature can deal with

Returns:
feature type DREAL

Implements CFeatures.

Definition at line 1361 of file SparseFeatures.h.

template<>
EFeatureType CSparseFeatures< float32_t >::get_feature_type (  )  [virtual]

get feature type the SHORTREAL feature can deal with

Returns:
feature type SHORTREAL

Implements CFeatures.

Definition at line 1352 of file SparseFeatures.h.

template<>
EFeatureType CSparseFeatures< uint64_t >::get_feature_type (  )  [virtual]

get feature type the ULONG feature can deal with

Returns:
feature type ULONG

Implements CFeatures.

Definition at line 1343 of file SparseFeatures.h.

template<>
EFeatureType CSparseFeatures< int64_t >::get_feature_type (  )  [virtual]

get feature type the LONG feature can deal with

Returns:
feature type LONG

Implements CFeatures.

Definition at line 1334 of file SparseFeatures.h.

template<>
EFeatureType CSparseFeatures< uint32_t >::get_feature_type (  )  [virtual]

get feature type the UINT feature can deal with

Returns:
feature type UINT

Implements CFeatures.

Definition at line 1325 of file SparseFeatures.h.

template<>
EFeatureType CSparseFeatures< int32_t >::get_feature_type (  )  [virtual]

get feature type the INT feature can deal with

Returns:
feature type INT

Implements CFeatures.

Definition at line 1316 of file SparseFeatures.h.

template<>
EFeatureType CSparseFeatures< uint16_t >::get_feature_type (  )  [virtual]

get feature type the WORD feature can deal with

Returns:
feature type WORD

Implements CFeatures.

Definition at line 1307 of file SparseFeatures.h.

template<>
EFeatureType CSparseFeatures< int16_t >::get_feature_type (  )  [virtual]

get feature type the SHORT feature can deal with

Returns:
feature type SHORT

Implements CFeatures.

Definition at line 1298 of file SparseFeatures.h.

template<>
EFeatureType CSparseFeatures< uint8_t >::get_feature_type (  )  [virtual]

get feature type the BYTE feature can deal with

Returns:
feature type BYTE

Implements CFeatures.

Definition at line 1289 of file SparseFeatures.h.

template<>
EFeatureType CSparseFeatures< char >::get_feature_type (  )  [virtual]

get feature type the CHAR feature can deal with

Returns:
feature type CHAR

Implements CFeatures.

Definition at line 1280 of file SparseFeatures.h.

template<>
EFeatureType CSparseFeatures< bool >::get_feature_type (  )  [virtual]

get feature type the BOOL feature can deal with

Returns:
feature type BOOL

Implements CFeatures.

Definition at line 1271 of file SparseFeatures.h.

template<class ST>
virtual EFeatureType CSparseFeatures< ST >::get_feature_type (  )  [virtual]

get feature type

Returns:
templated feature type

Implements CFeatures.

template<class ST>
void CSparseFeatures< ST >::get_full_feature_matrix ( ST **  dst,
int32_t *  num_feat,
int32_t *  num_vec 
)

gets a copy of a full feature matrix (swig compatible) num_feat,num_vectors are returned by reference

Parameters:
dst full feature matrix
num_feat number of features in matrix
num_vec number of vectors in matrix

Definition at line 630 of file SparseFeatures.h.

template<class ST>
ST* CSparseFeatures< ST >::get_full_feature_matrix ( int32_t &  num_feat,
int32_t &  num_vec 
)

gets a copy of a full feature matrix num_feat,num_vectors are returned by reference

Parameters:
num_feat number of features in matrix
num_vec number of vectors in matrix
Returns:
full feature matrix

Definition at line 595 of file SparseFeatures.h.

template<class ST>
void CSparseFeatures< ST >::get_full_feature_vector ( ST **  dst,
int32_t *  len,
int32_t  num 
)

get the fully expanded dense feature vector num

Parameters:
dst feature vector
len length is returned by reference
num index of feature vector

Definition at line 212 of file SparseFeatures.h.

template<class ST>
ST* CSparseFeatures< 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:
num index of feature vector
len length is returned by reference
Returns:
dense feature vector

Definition at line 180 of file SparseFeatures.h.

template<class ST>
virtual const char* CSparseFeatures< ST >::get_name (  )  const [virtual]
Returns:
object name

Implements CSGObject.

Definition at line 1233 of file SparseFeatures.h.

template<class ST>
virtual int32_t CSparseFeatures< ST >::get_nnz_features_for_vector ( int32_t  num  )  [virtual]

get number of non-zero features in vector

Parameters:
num which vector
Returns:
number of non-zero features in vector

Implements CDotFeatures.

Definition at line 244 of file SparseFeatures.h.

template<class ST>
int32_t CSparseFeatures< ST >::get_num_features (  ) 

get number of features

Returns:
number of features

Definition at line 811 of file SparseFeatures.h.

template<class ST>
int64_t CSparseFeatures< ST >::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 863 of file SparseFeatures.h.

template<class ST>
virtual int32_t CSparseFeatures< ST >::get_num_vectors (  )  [virtual]

get number of feature vectors

Returns:
number of feature vectors

Implements CFeatures.

Definition at line 805 of file SparseFeatures.h.

template<class ST>
virtual int32_t CSparseFeatures< ST >::get_size (  )  [virtual]

get memory footprint of one feature

Returns:
memory footprint of one feature

Implements CFeatures.

Definition at line 784 of file SparseFeatures.h.

template<class ST>
void CSparseFeatures< ST >::get_sparse_feature_matrix ( TSparse< ST > **  dst,
int32_t *  num_feat,
int32_t *  num_vec,
int64_t *  nnz 
)

get the pointer to the sparse feature matrix (swig compatible) num_feat,num_vectors are returned by reference

Parameters:
dst feature matrix
num_feat number of features in matrix
num_vec number of vectors in matrix
nnz number of nonzero elements

Definition at line 482 of file SparseFeatures.h.

template<class ST>
TSparse<ST>* CSparseFeatures< 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

Parameters:
num_feat number of features in matrix
num_vec number of vectors in matrix
Returns:
feature matrix

Definition at line 466 of file SparseFeatures.h.

template<class ST>
TSparseEntry<ST>* CSparseFeatures< ST >::get_sparse_feature_vector ( int32_t  num,
int32_t &  len,
bool &  vfree 
)

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

Parameters:
num index of feature vector
len number of sparse entries is returned by reference
vfree whether returned vector must be freed by caller via free_sparse_feature_vector
Returns:
sparse feature vector

Definition at line 263 of file SparseFeatures.h.

template<class ST>
TSparse<ST>* CSparseFeatures< 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

Parameters:
num_feat number of features in matrix
num_vec number of vectors in matrix
Returns:
transposed sparse feature matrix

Definition at line 516 of file SparseFeatures.h.

template<class ST>
CLabels* CSparseFeatures< ST >::load_svmlight_file ( char *  fname  ) 

load features from file

Parameters:
fname filename to load from
Returns:
label object with corresponding labels

Definition at line 971 of file SparseFeatures.h.

template<class ST>
bool CSparseFeatures< ST >::obtain_from_simple ( CSimpleFeatures< ST > *  sf  ) 

obtain sparse features from simple features

Parameters:
sf simple features
Returns:
if obtaining was successful

Definition at line 791 of file SparseFeatures.h.

template<class ST>
virtual bool CSparseFeatures< ST >::set_full_feature_matrix ( ST *  src,
int32_t  num_feat,
int32_t  num_vec 
) [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

Parameters:
src full feature matrix
num_feat number of features in matrix
num_vec number of vectors in matrix

Definition at line 665 of file SparseFeatures.h.

template<class ST>
int32_t CSparseFeatures< ST >::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:
num the number of features, must be larger than the current number of features
Returns:
previous number of features

Definition at line 824 of file SparseFeatures.h.

template<class ST>
virtual void CSparseFeatures< ST >::set_sparse_feature_matrix ( TSparse< ST > *  src,
int32_t  num_feat,
int32_t  num_vec 
) [virtual]

set feature matrix necessary to set feature_matrix, num_features, num_vectors, where num_features is the column offset, and columns are linear in memory see below for definition of feature_matrix

Parameters:
src new sparse feature matrix
num_feat number of features in matrix
num_vec number of vectors in matrix

Definition at line 579 of file SparseFeatures.h.

template<class ST>
ST CSparseFeatures< ST >::sparse_dot ( ST  alpha,
TSparseEntry< ST > *  avec,
int32_t  alen,
TSparseEntry< ST > *  bvec,
int32_t  blen 
)

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

Parameters:
alpha scalar to multiply with
avec first sparse feature vector
alen avec's length
bvec second sparse feature vector
blen bvec's length
Returns:
dot product between the two sparse feature vectors

Definition at line 331 of file SparseFeatures.h.

template<class ST>
bool CSparseFeatures< ST >::write_svmlight_file ( char *  fname,
CLabels label 
)

write features to file using svm light format

Parameters:
fname filename to write to
label Label object (number of labels must correspond to number of features)
Returns:
true if successful

Definition at line 1139 of file SparseFeatures.h.


Member Data Documentation

template<class ST>
CCache< TSparseEntry<ST> >* CSparseFeatures< ST >::feature_cache [protected]

feature cache

Definition at line 1264 of file SparseFeatures.h.

template<class ST>
int32_t CSparseFeatures< ST >::num_features [protected]

total number of features

Definition at line 1258 of file SparseFeatures.h.

template<class ST>
int32_t CSparseFeatures< ST >::num_vectors [protected]

total number of vectors

Definition at line 1255 of file SparseFeatures.h.

template<class ST>
TSparse<ST>* CSparseFeatures< ST >::sparse_feature_matrix [protected]

array of sparse vectors of size num_vectors

Definition at line 1261 of file SparseFeatures.h.


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

SHOGUN Machine Learning Toolbox - Documentation