jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
recommender_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 <algorithm>
18 #include <cmath>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 #include "recommender_base.hpp"
23 #include "../common/vector_util.hpp"
24 
25 using std::make_pair;
26 using std::pair;
27 using std::string;
28 using std::sort;
29 using std::vector;
30 
31 namespace jubatus {
32 namespace core {
33 namespace recommender {
34 
36 
38 }
39 
41 }
42 
44  const std::string& id, std::vector<std::pair<std::string, float> >& ids,
45  size_t ret_num) const {
46  ids.clear();
47  common::sfv_t sfv;
48  orig_.get_row(id, sfv);
49  similar_row(sfv, ids, ret_num);
50 }
51 
53  const string& id,
54  vector<pair<string, float> >& ids,
55  size_t ret_num) const {
56  ids.clear();
57  common::sfv_t sfv;
58  orig_.get_row(id, sfv);
59  neighbor_row(sfv, ids, ret_num);
60 }
61 
62 void recommender_base::decode_row(const std::string& id,
63  common::sfv_t& ret) const {
64  ret.clear();
65  orig_.get_row(id, ret);
66 }
67 
68 void recommender_base::complete_row(const std::string& id,
69  common::sfv_t& ret) const {
70  ret.clear();
71  common::sfv_t sfv;
72  orig_.get_row(id, sfv);
73  complete_row(sfv, ret);
74 }
75 
77  common::sfv_t& ret) const {
78  ret.clear();
79  vector<pair<string, float> > ids;
81  if (ids.size() == 0) {
82  return;
83  }
84 
85  size_t exist_row_num = 0;
86  for (size_t i = 0; i < ids.size(); ++i) {
87  common::sfv_t row;
88  orig_.get_row(ids[i].first, row);
89  if (row.size() == 0) {
90  continue;
91  } else {
92  ++exist_row_num;
93  }
94  for (size_t j = 0; j < row.size(); ++j) {
95  ret.push_back(make_pair(row[j].first, row[j].second));
96  }
97  }
98 
99  if (exist_row_num == 0) {
100  return;
101  }
103  for (size_t i = 0; i < ret.size(); ++i) {
104  ret[i].second /= exist_row_num;
105  }
106 }
107 
109  float q1_norm = calc_l2norm(q1);
110  float q2_norm = calc_l2norm(q2);
111  if (q1_norm == 0.f || q2_norm == 0.f) {
112  return 0.f;
113  }
114  sort(q1.begin(), q1.end());
115  sort(q2.begin(), q2.end());
116 
117  size_t i1 = 0;
118  size_t i2 = 0;
119  float ret = 0.f;
120  while (i1 < q1.size() && i2 < q2.size()) {
121  const string& ind1 = q1[i1].first;
122  const string& ind2 = q2[i2].first;
123  if (ind1 < ind2) {
124  ++i1;
125  } else if (ind1 > ind2) {
126  ++i2;
127  } else {
128  ret += q1[i1].second * q2[i2].second;
129  ++i1;
130  ++i2;
131  }
132  }
133 
134  return ret / q1_norm / q2_norm;
135 }
136 
138  float ret = 0.f;
139  for (size_t i = 0; i < query.size(); ++i) {
140  ret += query[i].second * query[i].second;
141  }
142  return std::sqrt(ret);
143 }
144 
145 } // namespace recommender
146 } // namespace core
147 } // namespace jubatus
static float calc_l2norm(const common::sfv_t &query)
void get_row(const std::string &row, std::vector< std::pair< std::string, float > > &columns) const
void decode_row(const std::string &id, common::sfv_t &ret) const
virtual void similar_row(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, size_t ret_num) const =0
void complete_row(const std::string &id, common::sfv_t &ret) const
virtual void neighbor_row(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, size_t ret_num) const =0
core::storage::sparse_matrix_storage orig_
void sort_and_merge(sfv_t &sfv)
Definition: vector_util.cpp:28
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
static float calc_similality(common::sfv_t &q1, common::sfv_t &q2)