SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 1999-2010 Soeren Sonnenburg 00008 * Written (W) 1999-2008 Gunnar Raetsch 00009 * Written (W) 2011-2013 Heiko Strathmann 00010 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00011 * Copyright (C) 2010 Berlin Institute of Technology 00012 */ 00013 00014 #ifndef _DENSEFEATURES__H__ 00015 #define _DENSEFEATURES__H__ 00016 00017 #include <shogun/lib/common.h> 00018 #include <shogun/lib/Cache.h> 00019 #include <shogun/io/File.h> 00020 #include <shogun/features/DotFeatures.h> 00021 #include <shogun/features/StringFeatures.h> 00022 #include <shogun/lib/DataType.h> 00023 00024 namespace shogun { 00025 template<class ST> class CStringFeatures; 00026 template<class ST> class CDenseFeatures; 00027 template<class ST> class SGMatrix; 00028 class CDotFeatures; 00029 00065 template<class ST> class CDenseFeatures: public CDotFeatures 00066 { 00067 public: 00072 CDenseFeatures(int32_t size = 0); 00073 00075 CDenseFeatures(const CDenseFeatures & orig); 00076 00081 CDenseFeatures(SGMatrix<ST> matrix); 00082 00089 CDenseFeatures(ST* src, int32_t num_feat, int32_t num_vec); 00090 00095 CDenseFeatures(CFile* loader); 00096 00101 virtual CFeatures* duplicate() const; 00102 00103 virtual ~CDenseFeatures(); 00104 00109 void free_feature_matrix(); 00110 00115 void free_features(); 00116 00128 ST* get_feature_vector(int32_t num, int32_t& len, bool& dofree); 00129 00137 void set_feature_vector(SGVector<ST> vector, int32_t num); 00138 00146 SGVector<ST> get_feature_vector(int32_t num); 00147 00156 void free_feature_vector(ST* feat_vec, int32_t num, bool dofree); 00157 00165 void free_feature_vector(SGVector<ST> vec, int32_t num); 00166 00180 void vector_subset(int32_t* idx, int32_t idx_len); 00181 00195 void feature_subset(int32_t* idx, int32_t idx_len); 00196 00204 SGMatrix<ST> get_feature_matrix(); 00205 00212 SGMatrix<ST> steal_feature_matrix(); 00213 00225 void set_feature_matrix(SGMatrix<ST> matrix); 00226 00236 ST* get_feature_matrix(int32_t &num_feat, int32_t &num_vec); 00237 00244 CDenseFeatures<ST>* get_transposed(); 00245 00257 ST* get_transposed(int32_t &num_feat, int32_t &num_vec); 00258 00268 virtual void copy_feature_matrix(SGMatrix<ST> src); 00269 00276 void obtain_from_dot(CDotFeatures* df); 00277 00288 virtual bool apply_preprocessor(bool force_preprocessing = false); 00289 00294 virtual int32_t get_num_vectors() const; 00295 00300 int32_t get_num_features() const; 00301 00306 void set_num_features(int32_t num); 00307 00314 void set_num_vectors(int32_t num); 00315 00320 void initialize_cache(); 00321 00326 virtual EFeatureClass get_feature_class() const; 00327 00332 virtual EFeatureType get_feature_type() const; 00333 00342 virtual bool reshape(int32_t p_num_features, int32_t p_num_vectors); 00343 00351 virtual int32_t get_dim_feature_space() const; 00352 00362 virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, 00363 int32_t vec_idx2); 00364 00373 virtual float64_t dense_dot(int32_t vec_idx1, float64_t* vec2, 00374 int32_t vec2_len); 00375 00386 virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, 00387 float64_t* vec2, int32_t vec2_len, bool abs_val = false); 00388 00394 virtual int32_t get_nnz_features_for_vector(int32_t num); 00395 00400 virtual void load(CFile* loader); 00401 00406 virtual void save(CFile* saver); 00407 00408 #ifndef DOXYGEN_SHOULD_SKIP_THIS 00409 00410 struct dense_feature_iterator 00411 { 00413 ST* vec; 00415 int32_t vidx; 00417 int32_t vlen; 00419 bool vfree; 00420 00422 int32_t index; 00423 }; 00424 #endif 00425 00437 virtual void* get_feature_iterator(int32_t vector_index); 00438 00451 virtual bool get_next_feature(int32_t& index, float64_t& value, 00452 void* iterator); 00453 00459 virtual void free_feature_iterator(void* iterator); 00460 00469 virtual CFeatures* copy_subset(SGVector<index_t> indices); 00470 00477 virtual bool is_equal(CDenseFeatures* rhs); 00478 00487 CFeatures* create_merged_copy(CList* other); 00488 00495 CFeatures* create_merged_copy(CFeatures* other); 00496 00500 static CDenseFeatures* obtain_from_generic(CFeatures* const base_features); 00501 00503 virtual const char* get_name() const { return "DenseFeatures"; } 00504 00505 protected: 00517 virtual ST* compute_feature_vector(int32_t num, int32_t& len, 00518 ST* target = NULL); 00519 00520 private: 00521 void init(); 00522 00523 protected: 00525 int32_t num_vectors; 00526 00528 int32_t num_features; 00529 00534 SGMatrix<ST> feature_matrix; 00535 00537 CCache<ST>* feature_cache; 00538 }; 00539 } 00540 #endif // _DENSEFEATURES__H__