SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MatrixFeatures.cpp
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) 2012 Fernando Jose Iglesias Garcia
8  * Copyright (C) 2012 Fernando Jose Iglesias Garcia
9  */
10 
12 
13 namespace shogun {
14 
16 : CFeatures(0)
17 {
18  init();
19 }
20 
22  int32_t num_vecs,
23  int32_t num_feats)
24 : CFeatures(0)
25 {
26  init();
27  m_features = SGMatrixList< ST >(num_vecs);
28  m_num_vectors = num_vecs;
29  m_num_features = num_feats;
30 }
31 
33  SGMatrixList< ST > feats, int32_t num_feats)
34 : CFeatures(0)
35 {
36  init();
37  set_features(feats, num_feats);
38 }
39 
41  SGMatrix< ST > feats, int32_t feat_length, int32_t num_vecs)
42 : CFeatures(0)
43 {
44  REQUIRE(feats.num_cols == feat_length*num_vecs, "The number of columns of feats "
45  "must be equal to feat_length times num_vecs\n");
46  init();
47  SGMatrixList< ST > feats_list = SGMatrixList< ST >::split(feats, num_vecs);
48  set_features(feats_list, feats.num_rows);
49 }
50 
51 /* TODO */
52 template< class ST > CFeatures* CMatrixFeatures< ST >::duplicate() const
53 {
54  return NULL;
55 }
56 
58 {
59  cleanup();
60 }
61 
62 /* TODO */
64 {
65  return F_UNKNOWN;
66 }
67 
69 {
70  return C_MATRIX;
71 }
72 
74  int32_t num) const
75 {
76  if ( num < 0 || num >= get_num_vectors() )
77  {
78  SG_ERROR("The index of the feature vector to get must be between "
79  "0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1);
80  }
81 
82  return m_features[num];
83 }
84 
86  SGVector< ST > out,
87  int32_t num,
88  int32_t col) const
89 {
90  if ( num < 0 || num >= get_num_vectors() )
91  {
92  SG_ERROR("The index of the feature vector to get must be between "
93  "0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1);
94  }
95 
96  // Shorthands for the dimensions of the feature vector to get
97  int32_t num_cols = m_features[num].num_cols;
98  int32_t num_rows = m_features[num].num_rows;
99 
100  if ( col < 0 || col >= num_cols )
101  {
102  SG_ERROR("The index of the column to get must be between "
103  "0 and %d (#columns of the feature vector)\n", num_cols);
104  }
105 
106  if ( out.vlen < get_num_features() )
107  {
108  SG_ERROR("The vector out must have space to hold at least "
109  "%d (get_num_features()) elements\n", get_num_features());
110  }
111 
112  int32_t start = col*num_rows;
113  for ( int32_t i = 0 ; i < get_num_features(); ++i )
114  {
115  out[i] = m_features[num][start + i];
116  }
117 }
118 
120  SGMatrix< ST > const vec,
121  int32_t num)
122 {
123  if ( num < 0 || num >= get_num_vectors() )
124  {
125  SG_ERROR("The index of the feature vector to set must be between "
126  "0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1);
127  }
128 
129  if ( get_num_features() != 0 && vec.num_rows != get_num_features() )
130  {
131  SG_ERROR("The feature vector to set must have the same features "
132  "as the rest of the MatrixFeatures, %d "
133  "(get_num_features())\n", get_num_features());
134  }
135 
136  m_features.set_matrix(num, vec);
137 }
138 
139 template< class ST > void CMatrixFeatures< ST >::set_features(
140  SGMatrixList< ST > features, int32_t num_feats)
141 {
142  m_features = features;
143  m_num_vectors = features.num_matrices;
144  m_num_features = num_feats;
145 }
146 
147 template< class ST > void CMatrixFeatures< ST >::init()
148 {
149  SG_ADD(&m_num_vectors, "m_num_vectors", "Number of feature vectors",
151  SG_ADD(&m_num_features, "m_num_features",
152  "Number of features per vector (optional)", MS_NOT_AVAILABLE);
153  //TODO add SG_ADD for SGMatrixList
154  //SG_ADD(&m_features, "m_features", "Matrix features", MS_NOT_AVAILABLE);
155 
156  m_num_vectors = 0;
157  m_num_features = 0;
158 
159  set_generic<ST>();
160 }
161 
162 template< class ST > void CMatrixFeatures< ST >::cleanup()
163 {
164  m_features = SGMatrixList< ST >();
165  m_num_vectors = 0;
166  m_num_features = 0;
167 }
168 
170 {
171  REQUIRE(base_features->get_feature_class() == C_MATRIX,
172  "base_features must be of dynamic type CMatrixFeatures\n")
173 
174  return (CMatrixFeatures< ST >*) base_features;
175 }
176 
177 template class CMatrixFeatures<bool>;
178 template class CMatrixFeatures<char>;
179 template class CMatrixFeatures<int8_t>;
180 template class CMatrixFeatures<uint8_t>;
181 template class CMatrixFeatures<int16_t>;
182 template class CMatrixFeatures<uint16_t>;
183 template class CMatrixFeatures<int32_t>;
184 template class CMatrixFeatures<uint32_t>;
185 template class CMatrixFeatures<int64_t>;
186 template class CMatrixFeatures<uint64_t>;
187 template class CMatrixFeatures<float32_t>;
188 template class CMatrixFeatures<float64_t>;
189 template class CMatrixFeatures<floatmax_t>;
190 
191 } /* namespace shogun */
void get_feature_vector_col(SGVector< ST > out, int32_t num, int32_t col) const
void set_features(SGMatrixList< ST > features, int32_t num_feats)
static SGMatrixList< T > split(SGMatrix< T > matrix, int32_t num_components)
virtual CFeatures * duplicate() const
static CMatrixFeatures * obtain_from_generic(CFeatures *const base_features)
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_cols
Definition: SGMatrix.h:378
index_t num_rows
Definition: SGMatrix.h:376
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
index_t vlen
Definition: SGVector.h:494
shogun vector
SGMatrix< ST > get_feature_vector(int32_t num) const
virtual EFeatureClass get_feature_class() const =0
virtual EFeatureClass get_feature_class() const
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Class CMatrixFeatures used to represent data whose feature vectors are better represented with matric...
The class Features is the base class of all feature objects.
Definition: Features.h:68
#define SG_ADD(...)
Definition: SGObject.h:81
virtual EFeatureType get_feature_type() const
void set_feature_vector(SGMatrix< ST > const vec, int32_t num)

SHOGUN Machine Learning Toolbox - Documentation