jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
nearest_neighbor_classifier.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2014 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 #ifndef JUBATUS_CORE_CLASSIFIER_NEAREST_NEIGHBOR_CLASSIFIER_HPP_
18 #define JUBATUS_CORE_CLASSIFIER_NEAREST_NEIGHBOR_CLASSIFIER_HPP_
19 
20 #include <stdint.h>
21 
22 #include <map>
23 #include <string>
24 #include <vector>
25 #include "jubatus/util/math/random.h"
26 #include "jubatus/util/data/unordered_set.h"
27 #include "jubatus/util/concurrent/lock.h"
28 #include "jubatus/util/concurrent/mutex.h"
29 
30 #include "../common/type.hpp"
31 #include "../nearest_neighbor/nearest_neighbor_base.hpp"
32 #include "../unlearner/unlearner_base.hpp"
33 #include "classifier_type.hpp"
34 #include "classifier_base.hpp"
35 
36 namespace jubatus {
37 namespace core {
38 namespace classifier {
39 
41  public:
43  jubatus::util::lang::shared_ptr<nearest_neighbor::nearest_neighbor_base>
44  nearest_neighbor_engine,
45  size_t k, float alpha);
46 
47  void train(const common::sfv_t& fv, const std::string& label);
48 
50  jubatus::util::lang::shared_ptr<unlearner::unlearner_base>
51  label_unlearner);
52 
53  std::string classify(const common::sfv_t& fv) const;
54  void classify_with_scores(const common::sfv_t& fv,
55  classify_result& scores) const;
56  bool delete_label(const std::string& label);
57  void clear();
58 
59  std::vector<std::string> get_labels() const;
60  bool set_label(const std::string& label);
61 
62  std::string name() const;
63 
64  void get_status(std::map<std::string, std::string>& status) const;
65 
66  void pack(framework::packer& pk) const;
67  void unpack(msgpack::object o);
68 
70 
71  private:
72  jubatus::util::lang::shared_ptr<nearest_neighbor::nearest_neighbor_base>
74  jubatus::util::data::unordered_set<std::string> labels_;
75  size_t k_;
76  float alpha_;
77  jubatus::util::concurrent::mutex unlearner_mutex_;
78  jubatus::util::lang::shared_ptr<unlearner::unlearner_base> unlearner_;
79  jubatus::util::concurrent::mutex rand_mutex_;
80  jubatus::util::math::random::mtrand rand_;
81 
82  class unlearning_callback;
83  void unlearn_id(const std::string& id);
84 };
85 
86 } // namespace classifier
87 } // namespace core
88 } // namespace jubatus
89 
90 #endif // JUBATUS_CORE_CLASSIFIER_NEAREST_NEIGHBOR_CLASSIFIER_HPP_
std::vector< classify_result_elem > classify_result
jubatus::util::lang::shared_ptr< unlearner::unlearner_base > unlearner_
nearest_neighbor_classifier(jubatus::util::lang::shared_ptr< nearest_neighbor::nearest_neighbor_base > nearest_neighbor_engine, size_t k, float alpha)
void get_status(std::map< std::string, std::string > &status) const
void train(const common::sfv_t &fv, const std::string &label)
void set_label_unlearner(jubatus::util::lang::shared_ptr< unlearner::unlearner_base > label_unlearner)
jubatus::util::data::unordered_set< std::string > labels_
void classify_with_scores(const common::sfv_t &fv, classify_result &scores) const
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
jubatus::util::lang::shared_ptr< nearest_neighbor::nearest_neighbor_base > nearest_neighbor_engine_