SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
DenseFeatures.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2010 Soeren Sonnenburg
8  * Written (W) 1999-2008 Gunnar Raetsch
9  * Written (W) 2011-2013 Heiko Strathmann
10  * Written (W) 2014-2017 Soumyajit De
11  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
12  * Copyright (C) 2010 Berlin Institute of Technology
13  */
14 
15 #ifndef _DENSEFEATURES__H__
16 #define _DENSEFEATURES__H__
17 
18 #include <shogun/lib/config.h>
19 
20 #include <shogun/lib/common.h>
21 #include <shogun/lib/Cache.h>
22 #include <shogun/io/File.h>
25 #include <shogun/lib/DataType.h>
26 
27 #include <shogun/lib/SGMatrix.h>
28 
29 namespace shogun {
30 template<class ST> class CStringFeatures;
31 template<class ST> class CDenseFeatures;
32 template<class ST> class SGMatrix;
33 class CDotFeatures;
34 
70 template<class ST> class CDenseFeatures: public CDotFeatures
71 {
72 public:
77  CDenseFeatures(int32_t size = 0);
78 
80  CDenseFeatures(const CDenseFeatures & orig);
81 
86  CDenseFeatures(SGMatrix<ST> matrix);
87 
94  CDenseFeatures(ST* src, int32_t num_feat, int32_t num_vec);
95 
100  CDenseFeatures(CFile* loader);
101 
106  virtual CFeatures* duplicate() const;
107 
108  virtual ~CDenseFeatures();
109 
114  void free_feature_matrix();
115 
120  void free_features();
121 
133  ST* get_feature_vector(int32_t num, int32_t& len, bool& dofree);
134 
142  void set_feature_vector(SGVector<ST> vector, int32_t num);
143 
151  SGVector<ST> get_feature_vector(int32_t num);
152 
161  void free_feature_vector(ST* feat_vec, int32_t num, bool dofree);
162 
170  void free_feature_vector(SGVector<ST> vec, int32_t num);
171 
185  void vector_subset(int32_t* idx, int32_t idx_len);
186 
200  void feature_subset(int32_t* idx, int32_t idx_len);
201 
209  SGMatrix<ST> get_feature_matrix();
210 
217  SGMatrix<ST> steal_feature_matrix();
218 
230  void set_feature_matrix(SGMatrix<ST> matrix);
231 
241  ST* get_feature_matrix(int32_t &num_feat, int32_t &num_vec);
242 
249  CDenseFeatures<ST>* get_transposed();
250 
262  ST* get_transposed(int32_t &num_feat, int32_t &num_vec);
263 
270  void obtain_from_dot(CDotFeatures* df);
271 
282  virtual bool apply_preprocessor(bool force_preprocessing = false);
283 
288  virtual int32_t get_num_vectors() const;
289 
294  int32_t get_num_features() const;
295 
300  void set_num_features(int32_t num);
301 
308  void set_num_vectors(int32_t num);
309 
314  void initialize_cache();
315 
320  virtual EFeatureClass get_feature_class() const;
321 
326  virtual EFeatureType get_feature_type() const;
327 
336  virtual bool reshape(int32_t p_num_features, int32_t p_num_vectors);
337 
345  virtual int32_t get_dim_feature_space() const;
346 
356  virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df,
357  int32_t vec_idx2);
358 
367  virtual float64_t dense_dot(int32_t vec_idx1, const float64_t* vec2,
368  int32_t vec2_len);
369 
380  virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1,
381  float64_t* vec2, int32_t vec2_len, bool abs_val = false);
382 
388  virtual int32_t get_nnz_features_for_vector(int32_t num);
389 
394  virtual void load(CFile* loader);
395 
400  virtual void save(CFile* saver);
401 
402 #ifndef DOXYGEN_SHOULD_SKIP_THIS
403 
404  struct dense_feature_iterator
405  {
407  ST* vec;
409  int32_t vidx;
411  int32_t vlen;
413  bool vfree;
414 
416  int32_t index;
417  };
418 #endif
419 
431  virtual void* get_feature_iterator(int32_t vector_index);
432 
445  virtual bool get_next_feature(int32_t& index, float64_t& value,
446  void* iterator);
447 
453  virtual void free_feature_iterator(void* iterator);
454 
463  virtual CFeatures* copy_subset(SGVector<index_t> indices);
464 
475  virtual CFeatures* copy_dimension_subset(SGVector<index_t> dims);
476 
483  virtual bool is_equal(CDenseFeatures* rhs);
484 
494  CFeatures* create_merged_copy(CList* other);
495 
503 
507  static CDenseFeatures* obtain_from_generic(CFeatures* const base_features);
508 
509 #ifndef SWIG // SWIG should skip this part
510  virtual CFeatures* shallow_subset_copy();
511 #endif
512 
514  virtual const char* get_name() const { return "DenseFeatures"; }
515 
516 protected:
528  virtual ST* compute_feature_vector(int32_t num, int32_t& len,
529  ST* target = NULL);
530 
531 private:
532  void init();
533 
534 protected:
535  /*
536  * Helper method which copies the working feature matrix into the pre-allocated
537  * target matrix passed to this method. If the size of the pre-allocated matrix
538  * is not sufficient to copy all the feature vectors, considering the column
539  * offset, it throws an error. It then copies into the target matrix, starting
540  * from base + (colum_offset * num_features) location.
541  */
542  void copy_feature_matrix(SGMatrix<ST> target, index_t column_offset=0) const;
543 
545  int32_t num_vectors;
546 
548  int32_t num_features;
549 
555 
558 };
559 }
560 #endif // _DENSEFEATURES__H__
void set_feature_vector(SGVector< ST > vector, int32_t num)
virtual int32_t get_dim_feature_space() const
virtual bool get_next_feature(int32_t &index, float64_t &value, void *iterator)
virtual void load(CFile *loader)
ST * get_feature_vector(int32_t num, int32_t &len, bool &dofree)
int32_t get_num_features() const
virtual void free_feature_iterator(void *iterator)
int32_t index_t
Definition: common.h:72
CDenseFeatures(int32_t size=0)
CCache< ST > * feature_cache
virtual CFeatures * duplicate() const
CDotFeatures(int32_t size=0)
Definition: DotFeatures.cpp:26
SGMatrix< ST > get_feature_matrix()
void set_feature_matrix(SGMatrix< ST > matrix)
CFeatures(int32_t size=0)
Definition: Features.cpp:23
void vector_subset(int32_t *idx, int32_t idx_len)
virtual bool is_equal(CDenseFeatures *rhs)
virtual int32_t get_nnz_features_for_vector(int32_t num)
virtual float64_t dense_dot(int32_t vec_idx1, const float64_t *vec2, int32_t vec2_len)
int32_t num_features
number of features in cache
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
virtual void * get_feature_iterator(int32_t vector_index)
virtual void save(CFile *saver)
virtual int32_t get_num_vectors() const
virtual ST * compute_feature_vector(int32_t num, int32_t &len, ST *target=NULL)
double float64_t
Definition: common.h:60
void set_num_vectors(int32_t num)
virtual float64_t dot(int32_t vec_idx1, CDotFeatures *df, int32_t vec_idx2)
CDenseFeatures< ST > * get_transposed()
void copy_feature_matrix(SGMatrix< ST > target, index_t column_offset=0) const
virtual CFeatures * shallow_subset_copy()
void set_num_features(int32_t num)
SGMatrix< ST > feature_matrix
CFeatures * create_merged_copy(CList *other)
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
virtual CFeatures * copy_dimension_subset(SGVector< index_t > dims)
void feature_subset(int32_t *idx, int32_t idx_len)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void free_feature_vector(ST *feat_vec, int32_t num, bool dofree)
virtual bool reshape(int32_t p_num_features, int32_t p_num_vectors)
virtual EFeatureClass get_feature_class() const
void obtain_from_dot(CDotFeatures *df)
int32_t num_vectors
number of vectors in cache
static CDenseFeatures * obtain_from_generic(CFeatures *const base_features)
virtual EFeatureType get_feature_type() const
virtual bool apply_preprocessor(bool force_preprocessing=false)
SGMatrix< ST > steal_feature_matrix()
virtual const char * get_name() const
virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t *vec2, int32_t vec2_len, bool abs_val=false)
virtual CFeatures * copy_subset(SGVector< index_t > indices)

SHOGUN Machine Learning Toolbox - Documentation