jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
storage_base.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2011 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 "storage_base.hpp"
18 #include <string>
19 #include "jubatus/util/text/json.h"
20 
21 using std::string;
22 
23 namespace jubatus {
24 namespace core {
25 namespace storage {
26 
28  const string& feature,
29  const string& inc_class,
30  const string& dec_class, const val1_t& w) {
31  feature_val1_t row;
32  get(feature, row);
33  float inc_class_val = w;
34  float dec_class_val = -w;
35  for (size_t i = 0; i < row.size(); ++i) {
36  const string& label = row[i].first;
37  if (label == inc_class) {
38  inc_class_val += row[i].second;
39  } else if (label == dec_class) {
40  dec_class_val += row[i].second;
41  }
42  }
43  set(feature, inc_class, inc_class_val);
44  set(feature, dec_class, dec_class_val);
45 }
46 
48  const common::sfv_t& sfv,
49  float step_width,
50  const std::string& inc_class,
51  const std::string& dec_class) {
52  for (common::sfv_t::const_iterator it = sfv.begin(); it != sfv.end(); ++it) {
53  const string& feature = it->first;
54  float val = it->second;
55  if (dec_class != "") {
56  update(feature, inc_class, dec_class, step_width * val);
57  } else {
58  feature_val1_t ret;
59  get(feature, ret);
60  float pos_val = 0.f;
61  for (size_t i = 0; i < ret.size(); ++i) {
62  if (ret[i].first == inc_class) {
63  pos_val = ret[i].second;
64  break;
65  }
66  }
67  set(feature, inc_class, pos_val + step_width * val);
68  }
69  }
70 }
71 
73  v.diff.clear();
74 }
75 
77  return true;
78 }
79 
80 } // namespace storage
81 } // namespace core
82 } // namespace jubatus
virtual bool set_average_and_clear_diff(const diff_t &)
virtual void update(const std::string &feature, const std::string &inc_class, const std::string &dec_class, const val1_t &w)
virtual void get_diff(diff_t &) const
virtual void bulk_update(const common::sfv_t &sfv, float step_width, const std::string &inc_class, const std::string &dec_class)
std::vector< std::pair< std::string, val1_t > > feature_val1_t
std::vector< T > v(size)
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
virtual void set(const std::string &feature, const std::string &klass, const val1_t &w)=0