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 }

SHOGUN Machine Learning Toolbox - Documentation