SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PluginEstimate.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/common.h>
12 #include <shogun/io/SGIO.h>
14 #include <shogun/labels/Labels.h>
19 
20 using namespace shogun;
21 
23 : CMachine(), m_pos_pseudo(1e-10), m_neg_pseudo(1e-10),
24  pos_model(NULL), neg_model(NULL), features(NULL)
25 {
27  "pos_pseudo","pseudo count for positive class");
29  "neg_pseudo", "pseudo count for negative class");
30 
32  "pos_model", "LinearHMM modelling positive class.");
34  "neg_model", "LinearHMM modelling negative class.");
35 
37  "features", "String Features.");
38 }
39 
41 {
44 
46 }
47 
49 {
52  if (data)
53  {
54  if (data->get_feature_class() != C_STRING ||
55  data->get_feature_type() != F_WORD)
56  {
57  SG_ERROR("Features not of class string type word\n")
58  }
59 
61  }
63 
66 
69 
72 
73  int32_t* pos_indizes=SG_MALLOC(int32_t, ((CStringFeatures<uint16_t>*) features)->get_num_vectors());
74  int32_t* neg_indizes=SG_MALLOC(int32_t, ((CStringFeatures<uint16_t>*) features)->get_num_vectors());
75 
77 
78  int32_t pos_idx=0;
79  int32_t neg_idx=0;
80 
81  for (int32_t i=0; i<m_labels->get_num_labels(); i++)
82  {
83  if (((CBinaryLabels*) m_labels)->get_label(i) > 0)
84  pos_indizes[pos_idx++]=i;
85  else
86  neg_indizes[neg_idx++]=i;
87  }
88 
89  SG_INFO("training using pseudos %f and %f\n", m_pos_pseudo, m_neg_pseudo)
90  pos_model->train(pos_indizes, pos_idx, m_pos_pseudo);
91  neg_model->train(neg_indizes, neg_idx, m_neg_pseudo);
92 
93  SG_FREE(pos_indizes);
94  SG_FREE(neg_indizes);
95 
96  return true;
97 }
98 
100 {
101  if (data)
102  {
103  if (data->get_feature_class() != C_STRING ||
104  data->get_feature_type() != F_WORD)
105  {
106  SG_ERROR("Features not of class string type word\n")
107  }
108 
110  }
111 
114 
115  for (int32_t vec=0; vec<features->get_num_vectors(); vec++)
116  result[vec] = apply_one(vec);
117 
118  return new CBinaryLabels(result);
119 }
120 
122 {
124 
125  int32_t len;
126  bool free_vec;
127  uint16_t* vector=features->get_feature_vector(vec_idx, len, free_vec);
128 
129  if ((!pos_model) || (!neg_model))
130  SG_ERROR("model(s) not assigned\n")
131 
133  features->free_feature_vector(vector, vec_idx, free_vec);
134  return result;
135 }
SGVector< ST > get_feature_vector(int32_t num)
#define SG_INFO(...)
Definition: SGIO.h:118
virtual ELabelType get_label_type() const =0
binary labels +1/-1
Definition: LabelTypes.h:18
float64_t apply_one(int32_t vec_idx)
classify the test feature vector indexed by vec_idx
virtual int32_t get_num_labels() const =0
virtual CBinaryLabels * apply_binary(CFeatures *data=NULL)
virtual int32_t get_num_vectors() const
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
Parameter * m_parameters
Definition: SGObject.h:505
void free_feature_vector(ST *feat_vec, int32_t num, bool dofree)
#define SG_REF(x)
Definition: SGObject.h:51
A generic learning machine interface.
Definition: Machine.h:143
float64_t get_log_likelihood_example(uint16_t *vector, int32_t len)
Definition: LinearHMM.cpp:186
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:37
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
double float64_t
Definition: common.h:50
virtual void set_features(CStringFeatures< uint16_t > *feat)
CPluginEstimate(float64_t pos_pseudo=1e-10, float64_t neg_pseudo=1e-10)
virtual EFeatureClass get_feature_class() const =0
#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
virtual bool train_machine(CFeatures *data=NULL)
virtual bool train(CFeatures *data=NULL)
Definition: LinearHMM.cpp:54
CStringFeatures< uint16_t > * features
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
The class LinearHMM is for learning Higher Order Markov chains.
Definition: LinearHMM.h:41
virtual EFeatureType get_feature_type() const =0

SHOGUN Machine Learning Toolbox - Documentation