SHOGUN  3.2.1
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
SGSparseMatrix.h
浏览该文件的文档.
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 José Iglesias García
8  * Written (W) 2010,2012 Soeren Sonnenburg
9  * Copyright (C) 2010 Berlin Institute of Technology
10  * Copyright (C) 2012 Soeren Sonnenburg
11  */
12 
13 #ifndef __SGSPARSEMATRIX_H__
14 #define __SGSPARSEMATRIX_H__
15 
16 #include <shogun/lib/common.h>
17 #include <shogun/lib/DataType.h>
20 #include <shogun/io/LibSVMFile.h>
21 
22 namespace shogun
23 {
24 
25 template <class T> class SGSparseVector;
26 template<class T> class SGMatrix;
27 class CFile;
28 class CLibSVMFile;
29 class CRegressionLabels;
30 
32 template <class T> class SGSparseMatrix : public SGReferencedData
33 {
34  public:
37 
39  SGSparseMatrix(SGSparseVector<T>* vecs, index_t num_feat,
40  index_t num_vec, bool ref_counting=true);
41 
43  SGSparseMatrix(index_t num_feat, index_t num_vec, bool ref_counting=true);
44 
49  SGSparseMatrix(SGMatrix<T> dense);
50 
52  SGSparseMatrix(const SGSparseMatrix &orig);
53 
55  virtual ~SGSparseMatrix();
56 
58  inline const SGSparseVector<T>& operator[](index_t index) const
59  {
60  return sparse_matrix[index];
61  }
62 
65  {
66  return sparse_matrix[index];
67  }
68 
74  inline SGSparseMatrix<T> get()
75  {
76  return *this;
77  }
78 
84  {
85  SGVector<T> result(num_vectors);
87  "Dimension mismatch! %d vs %d\n",
88  v.vlen, num_features);
89  for (index_t i=0; i<num_vectors; ++i)
90  result[i]=sparse_matrix[i].dense_dot(1.0, v.vector, v.vlen, 0.0);
91 
92  return result;
93  }
94 
99  template<class ST> const SGVector<T> operator*(SGVector<ST> v) const;
100 
105  inline const T operator()(index_t i_row, index_t i_col) const
106  {
107  REQUIRE(i_row>=0, "index %d negative!\n", i_row);
108  REQUIRE(i_col>=0, "index %d negative!\n", i_col);
109  REQUIRE(i_row<num_vectors, "index should be less than %d, %d provided!\n",
110  num_vectors, i_row);
111  REQUIRE(i_col<num_features, "index should be less than %d, %d provided!\n",
112  num_features, i_col);
113 
114  for (index_t i=0; i<sparse_matrix[i_row].num_feat_entries; ++i)
115  {
116  if (i_col==sparse_matrix[i_row].features[i].feat_index)
117  return sparse_matrix[i_row].features[i].entry;
118  }
119  return 0;
120  }
121 
126  inline T& operator()(index_t i_row, index_t i_col)
127  {
128  REQUIRE(i_row>=0, "index %d negative!\n", i_row);
129  REQUIRE(i_col>=0, "index %d negative!\n", i_col);
130  REQUIRE(i_row<num_vectors, "index should be less than %d, %d provided!\n",
131  num_vectors, i_row);
132  REQUIRE(i_col<num_features, "index should be less than %d, %d provided!\n",
133  num_features, i_col);
134 
135  for (index_t i=0; i<sparse_matrix[i_row].num_feat_entries; ++i)
136  {
137  if (i_col==sparse_matrix[i_row].features[i].feat_index)
138  return sparse_matrix[i_row].features[i].entry;
139  }
141  sparse_matrix[i_row].num_feat_entries=j+1;
143  sparse_matrix[i_row].features, j, j+1);
144  sparse_matrix[i_row].features[j].feat_index=i_col;
145  sparse_matrix[i_row].features[j].entry=static_cast<T>(0);
146 
147  return sparse_matrix[i_row].features[j].entry;
148  }
149 
154  void load(CFile* loader);
155 
163  SGVector<float64_t> load_with_labels(CLibSVMFile* libsvm_file, bool do_sort_features=true);
164 
169  void save(CFile* saver);
170 
176  void save_with_labels(CLibSVMFile* saver, SGVector<float64_t> labels);
177 
180 
185  void from_dense(SGMatrix<T> full);
186 
188  void sort_features();
189 
190 protected:
191 
193  virtual void copy_data(const SGReferencedData& orig);
194 
196  virtual void init_data();
197 
199  virtual void free_data();
200 
201 public:
202 
205 
208 
211 
212 };
213 }
214 #endif // __SGSPARSEMATRIX_H__
int32_t index_t
Definition: common.h:60
void save_with_labels(CLibSVMFile *saver, SGVector< float64_t > labels)
SGSparseVector< T > * sparse_matrix
array of sparse vectors of size num_vectors
#define REQUIRE(x,...)
Definition: SGIO.h:208
index_t num_vectors
total number of vectors
SGSparseVector< T > & operator[](index_t index)
virtual void copy_data(const SGReferencedData &orig)
const SGSparseVector< T > & operator[](index_t index) const
shogun reference count managed data
const SGVector< T > operator*(SGVector< T > v) const
A File access base class.
Definition: File.h:34
index_t num_features
total number of features
void from_dense(SGMatrix< T > full)
T & operator()(index_t i_row, index_t i_col)
const T operator()(index_t i_row, index_t i_col) const
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
Definition: LibSVMFile.h:30
void load(CFile *loader)
void save(CFile *saver)
SGSparseMatrix< T > get_transposed()
SGVector< float64_t > load_with_labels(CLibSVMFile *libsvm_file, bool do_sort_features=true)
index_t vlen
Definition: SGVector.h:706
SGSparseVectorEntry< T > * features

SHOGUN 机器学习工具包 - 项目文档