39 :
CDotFeatures(orig), sparse_feature_matrix(orig.sparse_feature_matrix),
40 feature_cache(orig.feature_cache)
67 REQUIRE(index>=0 && index<get_num_features(),
68 "get_feature(num=%d,index=%d): index exceeds [0;%d]\n",
69 num, index, get_num_features()-1);
74 free_sparse_feature_vector(num);
82 free_sparse_feature_vector(num);
90 free_sparse_feature_vector(num);
96 REQUIRE(num>=0 && num<get_num_vectors(),
97 "get_sparse_feature_vector(num=%d): num exceeds [0;%d]\n",
98 num, get_num_vectors()-1);
99 index_t real_num=m_subset_stack->subset_idx_conversion(num);
101 if (sparse_feature_matrix.sparse_matrix)
103 return sparse_feature_matrix[real_num];
110 result.
features=feature_cache->lock_entry(num);
116 result.
features=feature_cache->set_entry(num);
123 result.
features=compute_sparse_feature_vector(num,
127 if (get_num_preprocessors())
133 for (int32_t i=0; i<get_num_preprocessors(); i++)
138 SG_FREE(tmp_feat_before);
139 tmp_feat_before=tmp_feat_after;
144 memcpy(result.
features, tmp_feat_after,
147 SG_FREE(tmp_feat_after);
159 ST result = sv.
dense_dot(alpha,vec,dim,b);
160 free_sparse_feature_vector(num);
166 REQUIRE(vec,
"add_to_dense_vec(num=%d,dim=%d): vec must not be NULL\n",
168 REQUIRE(dim>=get_num_features(),
169 "add_to_dense_vec(num=%d,dim=%d): dim should contain number of features %d\n",
170 num, dim, get_num_features());
194 free_sparse_feature_vector(num);
199 int32_t num,
float64_t* vec, int32_t dim,
bool abs_val)
207 feature_cache->unlock_entry(m_subset_stack->subset_idx_conversion(num));
214 if (m_subset_stack->has_subsets())
215 SG_ERROR(
"Not allowed with subset\n");
217 return sparse_feature_matrix;
222 if (m_subset_stack->has_subsets())
223 SG_ERROR(
"Not allowed with subset\n");
230 if (m_subset_stack->has_subsets())
231 SG_ERROR(
"Not allowed with subset\n");
233 sparse_feature_matrix=sm;
236 for (int32_t j=0; j<get_num_vectors(); j++) {
239 "sparse_matrix[%d] check failed (matrix features %d >= vector dimension %d)\n",
246 SGMatrix<ST> full(get_num_features(), get_num_vectors());
249 SG_INFO(
"converting sparse features to full feature matrix of %d x %d"
250 " entries\n", sparse_feature_matrix.num_vectors, get_num_features())
252 for (int32_t v=0; v<full.
num_cols; v++)
254 int32_t idx=m_subset_stack->subset_idx_conversion(v);
259 int64_t offs=(v*get_num_features())
271 free_sparse_feature_matrix();
282 remove_all_subsets();
283 free_sparse_feature_matrix();
284 sparse_feature_matrix.from_dense(full);
289 SG_INFO(
"force: %d\n", force_preprocessing)
291 if (sparse_feature_matrix.sparse_matrix && get_num_preprocessors())
293 for (int32_t i=0; i<get_num_preprocessors(); i++)
295 if (!is_preprocessed(i) || force_preprocessing)
314 SG_WARNING(
"no sparse feature matrix available or features already preprocessed - skipping.\n")
323 set_full_feature_matrix(fm);
333 return m_subset_stack->has_subsets() ? m_subset_stack->get_size() : sparse_feature_matrix.num_vectors;
338 return sparse_feature_matrix.num_features;
343 int32_t n=get_num_features();
345 sparse_feature_matrix.num_features=num;
346 return sparse_feature_matrix.num_features;
357 feature_cache->unlock_entry(m_subset_stack->subset_idx_conversion(num));
365 index_t num_vec=get_num_vectors();
366 for (int32_t i=0; i<num_vec; i++)
367 num+=sparse_feature_matrix[m_subset_stack->subset_idx_conversion(i)].num_feat_entries;
376 index_t num_vec=get_num_vectors();
377 for (int32_t i=0; i<num_vec; i++)
385 free_feature_vector(i);
410 float64_t result=sq_lhs[idx_a]+sq_rhs[idx_b];
459 return get_num_features();
474 free_sparse_feature_vector(vec_idx1);
475 sf->free_sparse_feature_vector(vec_idx2);
489 REQUIRE(vec2,
"dense_dot(vec_idx1=%d,vec2_len=%d): vec2 must not be NULL\n",
491 REQUIRE(vec2_len>=get_num_features(),
492 "dense_dot(vec_idx1=%d,vec2_len=%d): vec2_len should contain number of features %d %d\n",
493 vec_idx1, vec2_len, get_num_features());
501 "sparse_matrix[%d] check failed (matrix features %d >= vector dimension %d)\n",
505 "sparse_matrix[%d] check failed (dense vector dimension %d >= vector dimension %d)\n",
512 free_sparse_feature_vector(vec_idx1);
526 if (vector_index>=get_num_vectors())
528 SG_ERROR(
"Index out of bounds (number of vectors %d, you "
529 "requested %d)\n", get_num_vectors(), vector_index);
532 if (!sparse_feature_matrix.sparse_matrix)
533 SG_ERROR(
"Requires a in-memory feature matrix\n")
535 sparse_feature_iterator* it=
new sparse_feature_iterator();
536 it->sv=get_sparse_feature_vector(vector_index);
538 it->vector_index=vector_index;
545 sparse_feature_iterator* it=(sparse_feature_iterator*) iterator;
546 if (!it || it->index>=it->sv.num_feat_entries)
549 int32_t i=it->index++;
551 index=it->sv.features[i].feat_index;
552 value=(
float64_t) it->sv.features[i].entry;
569 delete ((sparse_feature_iterator*) iterator);
581 index_t real_index=m_subset_stack->subset_idx_conversion(index);
587 free_sparse_feature_vector(index);
605 sparse_feature_matrix.sort_features();
612 m_parameters->add_vector(&sparse_feature_matrix.sparse_matrix, &sparse_feature_matrix.num_vectors,
613 "sparse_feature_matrix",
614 "Array of sparse vectors.");
615 m_parameters->add(&sparse_feature_matrix.num_features,
"sparse_feature_matrix.num_features",
616 "Total number of features.");
619 #define GET_FEATURE_TYPE(sg_type, f_type) \
620 template<> EFeatureType CSparseFeatures<sg_type>::get_feature_type() const \
638 #undef GET_FEATURE_TYPE
642 remove_all_subsets();
644 free_sparse_feature_matrix();
645 sparse_feature_matrix.load(loader);
650 remove_all_subsets();
652 free_sparse_feature_matrix();
653 return sparse_feature_matrix.load_with_labels(loader);
658 if (m_subset_stack->has_subsets())
659 SG_ERROR(
"Not allowed with subset\n");
661 sparse_feature_matrix.save(writer);
666 if (m_subset_stack->has_subsets())
667 SG_ERROR(
"Not allowed with subset\n");
669 sparse_feature_matrix.save_with_labels(writer, labels);
CSparseFeatures(int32_t size=0)
CSubsetStack * m_subset_stack
std::complex< float64_t > complex128_t
T sparse_dot(const SGSparseVector< T > &v)
The class DenseFeatures implements dense feature matrices.
ST dense_dot(ST alpha, int32_t num, ST *vec, int32_t dim, ST b)
int32_t set_num_features(int32_t num)
SGMatrix< ST > get_feature_matrix()
Template class SparseFeatures implements sparse matrices.
#define SG_NOTIMPLEMENTED
#define GET_FEATURE_TYPE(sg_type, f_type)
virtual CFeatures * duplicate() const
SGVector< T > get_dense(int32_t dimension)
virtual ~CSparseFeatures()
int64_t get_num_nonzero_entries()
Features that support dot products among other operations.
EFeatureClass
shogun feature class
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)
ST get_feature(int32_t num, int32_t index)
SGSparseMatrix< ST > get_sparse_feature_matrix()
void set_sparse_feature_matrix(SGSparseMatrix< ST > sm)
virtual SGSparseVectorEntry< ST > * compute_sparse_feature_vector(int32_t num, int32_t &len, SGSparseVectorEntry< ST > *target=NULL)
void free_sparse_features()
CSparseFeatures< ST > * get_transposed()
int32_t get_num_features() const
virtual bool get_next_feature(int32_t &index, float64_t &value, void *iterator)
virtual EFeatureClass get_feature_class() const
SGVector< ST > get_full_feature_vector(int32_t num)
virtual void free_feature_iterator(void *iterator)
A File access base class.
virtual void set_full_feature_matrix(SGMatrix< ST > full)
void obtain_from_simple(CDenseFeatures< ST > *sf)
void save_with_labels(CLibSVMFile *writer, SGVector< float64_t > labels)
virtual void * get_feature_iterator(int32_t vector_index)
virtual EFeatureClass get_feature_class() const =0
SGSparseVector< T > * sparse_matrix
array of sparse vectors of size num_vectors
void free_feature_vector(int32_t num)
SGSparseVectorEntry< T > * features
T dense_dot(T alpha, T *vec, int32_t dim, T b)
void free_sparse_feature_vector(int32_t num)
SGSparseVector< ST > get_sparse_feature_vector(int32_t num)
virtual bool apply_preprocessor(bool force_preprocessing=false)
virtual int32_t get_dim_feature_space() const
all of classes and functions are contained in the shogun namespace
virtual float64_t dot(int32_t vec_idx1, CDotFeatures *df, int32_t vec_idx2)
read sparse real valued features in svm light format e.g. -1 1:10.0 2:100.2 1000:1.3 with -1 == (optional) label and dim 1 - value 10.0 dim 2 - value 100.2 dim 1000 - value 1.3
The class Features is the base class of all feature objects.
virtual SGSparseVector< ST > * apply_to_sparse_feature_matrix(CSparseFeatures< ST > *f)=0
T get_feature(int32_t index)
float64_t * compute_squared(float64_t *sq)
virtual const char * get_name() const
return the name of the preprocessor
void add_to_dense_vec(float64_t alpha, int32_t num, float64_t *vec, int32_t dim, bool abs_val=false)
virtual CFeatures * copy_subset(SGVector< index_t > indices)
SGVector< float64_t > load_with_labels(CLibSVMFile *loader)
int32_t get_num_dimensions()
Template class SparsePreprocessor, base class for preprocessors (cf. CPreprocessor) that apply to CSp...
SGMatrix< ST > get_full_feature_matrix()
virtual int32_t get_num_vectors() const
void free_sparse_feature_matrix()
virtual int32_t get_nnz_features_for_vector(int32_t num)
virtual EFeatureType get_feature_type() const =0