SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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/features/Labels.h>
17 
18 using namespace shogun;
19 
21 : CMachine(), m_pos_pseudo(1e-10), m_neg_pseudo(1e-10),
22  pos_model(NULL), neg_model(NULL), features(NULL)
23 {
25  "pos_pseudo","pseudo count for positive class");
27  "neg_pseudo", "pseudo count for negative class");
28 
30  "pos_model", "LinearHMM modelling positive class.");
32  "neg_model", "LinearHMM modelling negative class.");
33 
35  "features", "String Features.");
36 }
37 
39 {
42 
44 }
45 
47 {
48  ASSERT(labels);
49  if (data)
50  {
51  if (data->get_feature_class() != C_STRING ||
52  data->get_feature_type() != F_WORD)
53  {
54  SG_ERROR("Features not of class string type word\n");
55  }
56 
58  }
60 
63 
66 
69 
70  int32_t* pos_indizes=SG_MALLOC(int32_t, ((CStringFeatures<uint16_t>*) features)->get_num_vectors());
71  int32_t* neg_indizes=SG_MALLOC(int32_t, ((CStringFeatures<uint16_t>*) features)->get_num_vectors());
72 
74 
75  int32_t pos_idx=0;
76  int32_t neg_idx=0;
77 
78  for (int32_t i=0; i<labels->get_num_labels(); i++)
79  {
80  if (labels->get_label(i) > 0)
81  pos_indizes[pos_idx++]=i;
82  else
83  neg_indizes[neg_idx++]=i;
84  }
85 
86  SG_INFO( "training using pseudos %f and %f\n", m_pos_pseudo, m_neg_pseudo);
87  pos_model->train(pos_indizes, pos_idx, m_pos_pseudo);
88  neg_model->train(neg_indizes, neg_idx, m_neg_pseudo);
89 
90  SG_FREE(pos_indizes);
91  SG_FREE(neg_indizes);
92 
93  return true;
94 }
95 
97 {
99  CLabels* result=new CLabels(features->get_num_vectors());
101 
102  for (int32_t vec=0; vec<features->get_num_vectors(); vec++)
103  result->set_label(vec, apply(vec));
104 
105  return result;
106 }
107 
109 {
110  if (!data)
111  SG_ERROR("No features specified\n");
112 
113  if (data->get_feature_class() != C_STRING ||
114  data->get_feature_type() != F_WORD)
115  {
116  SG_ERROR("Features not of class string type word\n");
117  }
118 
120  return apply();
121 }
122 
124 {
125  ASSERT(features);
126 
127  int32_t len;
128  bool free_vec;
129  uint16_t* vector=features->get_feature_vector(vec_idx, len, free_vec);
130 
131  if ((!pos_model) || (!neg_model))
132  SG_ERROR( "model(s) not assigned\n");
133 
135  features->free_feature_vector(vector, vec_idx, free_vec);
136  return result;
137 }
#define SG_INFO(...)
Definition: SGIO.h:73
virtual EFeatureType get_feature_type()=0
bool set_label(int32_t idx, float64_t label)
Definition: Labels.cpp:199
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:35
int32_t get_num_labels()
Definition: Labels.cpp:240
#define SG_ERROR(...)
Definition: SGIO.h:75
Parameter * m_parameters
Definition: SGObject.h:297
virtual int32_t get_num_vectors() const
#define SG_REF(x)
Definition: SGObject.h:44
A generic learning machine interface.
Definition: Machine.h:96
float64_t get_log_likelihood_example(uint16_t *vector, int32_t len)
Definition: LinearHMM.cpp:186
CLabels * labels
Definition: Machine.h:251
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:23
#define ASSERT(x)
Definition: SGIO.h:102
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:76
virtual EFeatureClass get_feature_class()=0
double float64_t
Definition: common.h:56
virtual void set_features(CStringFeatures< uint16_t > *feat)
CPluginEstimate(float64_t pos_pseudo=1e-10, float64_t neg_pseudo=1e-10)
float64_t get_label(int32_t idx)
Definition: Labels.cpp:223
void free_feature_vector(ST *feat_vec, int32_t num, bool dofree)
SGVector< ST > get_feature_vector(int32_t num)
#define SG_FREE(ptr)
Definition: memory.h:39
#define SG_UNREF(x)
Definition: SGObject.h:45
The class Features is the base class of all feature objects.
Definition: Features.h:56
virtual bool train_machine(CFeatures *data=NULL)
virtual bool train(CFeatures *data=NULL)
Definition: LinearHMM.cpp:54
CStringFeatures< uint16_t > * features
#define SG_MALLOC(type, len)
Definition: memory.h:36
The class LinearHMM is for learning Higher Order Markov chains.
Definition: LinearHMM.h:39

SHOGUN Machine Learning Toolbox - Documentation