jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
eigen_feature_mapper.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2013 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #include "eigen_feature_mapper.hpp"
18 
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 using std::vector;
24 using std::pair;
25 
26 namespace jubatus {
27 namespace core {
28 namespace clustering {
29 
31  map_.clear();
32  rmap_.clear();
33  d_ = 0;
34 }
35 
37  return d_;
38 }
39 
41  const common::sfv_t& src,
42  bool update_map) {
43  eigen_svec_t ret(d_);
44  for (common::sfv_t::const_iterator it = src.begin(); it != src.end(); ++it) {
45  insert(*it, update_map, ret);
46  }
47  return ret;
48 }
49 
51  eigen_svec_t ret(d_);
52  for (common::sfv_t::const_iterator it = src.begin(); it != src.end(); ++it) {
53  insertc(*it, ret);
54  }
55  return ret;
56 }
57 
59  const weighted_point& src,
60  bool update_map) {
61  eigen_wsvec_t ret;
62  ret.data = convert(src.data, update_map);
63  ret.weight = src.weight;
64  return ret;
65 }
66 
68  const vector<common::sfv_t>& src,
69  bool update_map) {
70  eigen_svec_list_t ret(src.size());
71  eigen_svec_list_t::iterator ob = ret.begin();
72  vector<common::sfv_t>::const_iterator ib = src.begin();
73  while (ib != src.end()) {
74  *ob = convert(*ib, update_map);
75  ++ob;
76  ++ib;
77  }
78  for (ob = ret.begin(); ob != ret.end(); ++ob) {
79  ob->resize(d_);
80  }
81  return ret;
82 }
83 
85  const wplist& src,
86  bool update_map) {
87  eigen_wsvec_list_t ret(src.size());
88  eigen_wsvec_list_t::iterator ob = ret.begin();
89  wplist::const_iterator ib = src.begin();
90  while (ib != src.end()) {
91  *ob = convert(*ib, update_map);
92  ++ob;
93  ++ib;
94  }
95  for (ob = ret.begin(); ob != ret.end(); ++ob) {
96  eigen_svec_t v(d_);
97  for (eigen_svec_t::InnerIterator it(ob->data); it; ++it) {
98  v.coeffRef(it.index()) = it.value();
99  }
100  ob->data = v;
101  }
102  return ret;
103 }
104 
106  common::sfv_t ret;
107  for (eigen_svec_t::InnerIterator it(src); it; ++it) {
108  rinsert(std::make_pair(it.row(), static_cast<float>(it.value())), ret);
109  }
110  return ret;
111 }
112 
114  const eigen_wsvec_t& src) const {
115  weighted_point ret;
116  ret.weight = src.weight;
117  ret.data = revert(src.data);
118  return ret;
119 }
120 
121 std::vector<common::sfv_t> eigen_feature_mapper::revert(
122  const eigen_svec_list_t& src) const {
123  std::vector<common::sfv_t> ret(src.size());
124  eigen_svec_list_t::const_iterator ib = src.begin();
125  std::vector<common::sfv_t>::iterator ob = ret.begin();
126  while (ib != src.end()) {
127  *ob = revert(*ib);
128  ++ob;
129  ++ib;
130  }
131  return ret;
132 }
133 
135  const eigen_wsvec_list_t& src) const {
136  wplist ret(src.size());
137  eigen_wsvec_list_t::const_iterator ib = src.begin();
138  wplist::iterator ob = ret.begin();
139  while (ib != src.end()) {
140  *ob = revert(*ib);
141  ++ob;
142  ++ib;
143  }
144  return ret;
145 }
146 
148  const pair<std::string, float>& item,
149  bool update_map,
150  eigen_svec_t& dst) {
151  if (dst.rows() < d_) {
152  dst.resize(d_);
153  }
154  if (map_.find(item.first) != map_.end()) {
155  dst.coeffRef(map_[item.first]) = item.second;
156  } else if (update_map) {
157  rmap_[d_] = item.first;
158  map_[item.first] = d_++;
159  insert(item, update_map, dst);
160  }
161 }
162 
164  const pair<std::string, float>& item,
165  eigen_svec_t& dst) const {
166  if (dst.rows() < d_) {
167  dst.resize(d_);
168  }
169  if (map_.find(item.first) != map_.end()) {
170  dst.coeffRef((map_.find(item.first))->second) = item.second;
171  }
172 }
173 
175  const pair<int, float>& item,
176  common::sfv_t& dst) const {
177  if (rmap_.find(item.first) != rmap_.end()) {
178  dst.push_back(
179  make_pair((rmap_.find(item.first))->second, item.second));
180  }
181 }
182 
183 } // namespace clustering
184 } // namespace core
185 } // namespace jubatus
eigen_svec_t convertc(const common::sfv_t &src) const
void insert(const std::pair< std::string, float > &item, bool update_map, eigen_svec_t &dst)
wplist revert(const eigen_wsvec_list_t &src) const
void insertc(const std::pair< std::string, float > &, eigen_svec_t &dst) const
std::vector< eigen_svec_t > eigen_svec_list_t
Definition: gmm_types.hpp:32
std::vector< T > v(size)
void rinsert(const std::pair< int, float > &, common::sfv_t &dst) const
jubatus::util::data::unordered_map< std::string, size_t > map_
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
std::vector< eigen_wsvec_t > eigen_wsvec_list_t
Definition: gmm_types.hpp:42
eigen_wsvec_list_t convert(const wplist &src, bool update_map=true)
std::vector< weighted_point > wplist
Definition: types.hpp:55
Eigen::SparseVector< double > eigen_svec_t
Definition: gmm_types.hpp:29
jubatus::util::data::unordered_map< size_t, std::string > rmap_