SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
KernelDensity.cpp
浏览该文件的文档.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Parijat Mazumdar
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  */
30 
35 
36 using namespace shogun;
37 
38 CKernelDensity::CKernelDensity(float64_t bandwidth, EKernelType kernel, EDistanceType dist, EEvaluationMode eval, int32_t leaf_size, float64_t atol, float64_t rtol)
39 : CDistribution()
40 {
41  init();
42 
43  m_bandwidth=bandwidth;
44  m_eval=eval;
45  m_leaf_size=leaf_size;
46  m_atol=atol;
47  m_rtol=rtol;
48  m_dist=dist;
49  m_kernel_type=kernel;
50 }
51 
53 {
54  SG_UNREF(tree);
55 }
56 
58 {
59  REQUIRE(data,"Data not supplied\n")
61 
62  SG_UNREF(tree);
63  switch (m_eval)
64  {
65  case EM_KDTREE_SINGLE:
66  {
67  tree=new CKDTree(m_leaf_size,m_dist);
68  break;
69  }
70  case EM_BALLTREE_SINGLE:
71  {
72  tree=new CBallTree(m_leaf_size,m_dist);
73  break;
74  }
75  case EM_KDTREE_DUAL:
76  {
77  tree=new CKDTree(m_leaf_size,m_dist);
78  break;
79  }
80  case EM_BALLTREE_DUAL:
81  {
82  tree=new CBallTree(m_leaf_size,m_dist);
83  break;
84  }
85  default:
86  {
87  SG_ERROR("Evaluation mode not recognized\n");
88  }
89  }
90 
91  tree->build_tree(dense_data);
92  return true;
93 }
94 
96 {
97  REQUIRE(test,"data not supplied\n")
98 
99  if ((m_eval==EM_KDTREE_SINGLE) || (m_eval==EM_BALLTREE_SINGLE))
100  return tree->log_kernel_density(test->get_feature_matrix(),m_kernel_type,m_bandwidth,m_atol,m_rtol);
101 
102  CNbodyTree* query_tree=NULL;
103  if (m_eval==EM_KDTREE_DUAL)
104  query_tree=new CKDTree(leaf_size,m_dist);
105  else if (m_eval==EM_BALLTREE_DUAL)
106  query_tree=new CBallTree(leaf_size,m_dist);
107  else
108  SG_ERROR("Evaluation mode not identified\n");
109 
110  query_tree->build_tree(test);
112  CTreeMachineNode<NbodyTreeNodeData>* root=query_tree->get_root();
113  if (root)
114  qroot=dynamic_cast<CBinaryTreeMachineNode<NbodyTreeNodeData>*>(root);
115  else
116  SG_ERROR("Query tree root not found!\n")
117 
119  SGVector<float64_t> ret=tree->log_kernel_density_dual(test->get_feature_matrix(),qid,qroot,m_kernel_type,m_bandwidth,m_atol,m_rtol);
120 
121  SG_UNREF(root);
122  SG_UNREF(query_tree);
123 
124  return ret;
125 }
126 
128 {
130  return 0;
131 }
132 
134 {
136  return 0;
137 }
138 
139 float64_t CKernelDensity::get_log_derivative(int32_t num_param, int32_t num_example)
140 {
142  return 0;
143 }
144 
146 {
148  return 0;
149 }
150 
151 void CKernelDensity::init()
152 {
153  m_bandwidth=1.0;
154  m_eval=EM_KDTREE_SINGLE;
155  m_kernel_type=K_GAUSSIAN;
156  m_dist=D_EUCLIDEAN;
157  m_leaf_size=1;
158  m_atol=0;
159  m_rtol=0;
160  tree=NULL;
161 
162  SG_ADD(&m_bandwidth,"m_bandwidth","bandwidth",MS_NOT_AVAILABLE);
163  SG_ADD(&m_leaf_size,"m_leaf_size","leaf size",MS_NOT_AVAILABLE);
164  SG_ADD(&m_atol,"m_atol","absolute tolerance",MS_NOT_AVAILABLE);
165  SG_ADD(&m_rtol,"m_rtol","relative tolerance",MS_NOT_AVAILABLE);
166  SG_ADD((CSGObject**) &tree,"tree","tree",MS_NOT_AVAILABLE);
167 }
SGVector< float64_t > get_log_density(CDenseFeatures< float64_t > *test, int32_t leaf_size=1)
The node of the tree structure forming a TreeMachine The node contains pointer to its parent and poin...
virtual float64_t get_log_likelihood_example(int32_t num_example)
EKernelType
Definition: Kernel.h:57
SGVector< index_t > get_rearranged_vector_ids() const
Definition: NbodyTree.h:72
SGMatrix< ST > get_feature_matrix()
This class implements Ball tree. The ball tree is contructed using the top-down approach. cf. ftp://ftp.icsi.berkeley.edu/pub/techreports/1989/tr-89-063.pdf.
Definition: BallTree.h:45
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
CTreeMachineNode< T > * get_root()
Definition: TreeMachine.h:88
virtual float64_t get_log_derivative(int32_t num_param, int32_t num_example)
Base class Distribution from which all methods implementing a distribution are derived.
Definition: Distribution.h:44
void build_tree(CDenseFeatures< float64_t > *data)
Definition: NbodyTree.cpp:45
EDistanceType
Definition: Distance.h:32
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
CKernelDensity(float64_t bandwidth=1.0, EKernelType kernel_type=K_GAUSSIAN, EDistanceType dist=D_EUCLIDEAN, EEvaluationMode eval=EM_BALLTREE_SINGLE, int32_t leaf_size=1, float64_t atol=0, float64_t rtol=0)
SGVector< float64_t > log_kernel_density_dual(SGMatrix< float64_t > test, SGVector< index_t > qid, bnode_t *qroot, EKernelType kernel, float64_t h, float64_t atol, float64_t rtol)
Definition: NbodyTree.cpp:116
double float64_t
Definition: common.h:50
SGVector< float64_t > log_kernel_density(SGMatrix< float64_t > test, EKernelType kernel, float64_t h, float64_t atol, float64_t rtol)
Definition: NbodyTree.cpp:86
This class implements genaralized tree for N-body problems like k-NN, kernel density estimation...
Definition: NbodyTree.h:50
This class implements KD-Tree. cf. http://www.autonlab.org/autonweb/14665/version/2/part/5/data/moore...
Definition: KDTree.h:45
virtual bool train(CFeatures *data=NULL)
virtual int32_t get_num_model_parameters()
#define SG_UNREF(x)
Definition: SGObject.h:52
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
static CDenseFeatures * obtain_from_generic(CFeatures *const base_features)
#define SG_ADD(...)
Definition: SGObject.h:81
virtual float64_t get_log_model_parameter(int32_t num_param)

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