SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 }

SHOGUN Machine Learning Toolbox - Documentation