SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AUCKernel.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-2008 Gunnar Raetsch
8  * Written (W) 2009 Soeren Sonnnenburg
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #include <shogun/lib/common.h>
16 #include <shogun/io/SGIO.h>
17 
18 using namespace shogun;
19 
20 void
21 CAUCKernel::init()
22 {
23  m_parameters->add((CSGObject**) &subkernel, "subkernel",
24  "The subkernel.");
25 }
26 
28 : CDotKernel(0), subkernel(NULL)
29 {
30  init();
31 }
32 
34 : CDotKernel(size), subkernel(s)
35 {
36  init();
38 }
39 
41 {
43  cleanup();
44 }
45 
47 {
48  SG_INFO( "setting up AUC maximization\n") ;
49  ASSERT(labels);
50  ASSERT(labels->is_two_class_labeling());
51 
52  // get the original labels
53  ASSERT(labels);
54  SGVector<int32_t> int_labels=labels->get_int_labels();
55  ASSERT(subkernel->get_num_vec_rhs()==int_labels.vlen);
56 
57  // count positive and negative
58  int32_t num_pos=0;
59  int32_t num_neg=0;
60 
61  for (int32_t i=0; i<int_labels.vlen; i++)
62  {
63  if (int_labels.vector[i]==1)
64  num_pos++;
65  else
66  num_neg++;
67  }
68 
69  // create AUC features and labels (alternate labels)
70  int32_t num_auc = num_pos*num_neg;
71  SG_INFO("num_pos: %i num_neg: %i num_auc: %i\n", num_pos, num_neg, num_auc);
72 
73  uint16_t* features_auc = SG_MALLOC(uint16_t, num_auc*2);
74  int32_t* labels_auc = SG_MALLOC(int32_t, num_auc);
75  int32_t n=0 ;
76 
77  for (int32_t i=0; i<int_labels.vlen; i++)
78  {
79  if (int_labels.vector[i]!=1)
80  continue;
81 
82  for (int32_t j=0; j<int_labels.vlen; j++)
83  {
84  if (int_labels.vector[j]!=-1)
85  continue;
86 
87  // create about as many positively as negatively labeled examples
88  if (n%2==0)
89  {
90  features_auc[n*2]=i;
91  features_auc[n*2+1]=j;
92  labels_auc[n]=1;
93  }
94  else
95  {
96  features_auc[n*2]=j;
97  features_auc[n*2+1]=i;
98  labels_auc[n]=-1;
99  }
100 
101  n++;
102  ASSERT(n<=num_auc);
103  }
104  }
105 
106  // create label object and attach it to svm
107  CLabels* lab_auc = new CLabels(num_auc);
108  lab_auc->set_int_labels(SGVector<int32_t>(labels_auc, num_auc));
109  SG_REF(lab_auc);
110 
111  // create feature object
113  f->set_feature_matrix(features_auc, 2, num_auc);
114 
115  // create AUC kernel and attach the features
116  init(f,f);
117 
118  int_labels.free_vector();
119  SG_FREE(labels_auc);
120 
121  return lab_auc;
122 }
123 
124 
125 bool CAUCKernel::init(CFeatures* l, CFeatures* r)
126 {
127  CDotKernel::init(l, r);
128  init_normalizer();
129  return true;
130 }
131 
132 float64_t CAUCKernel::compute(int32_t idx_a, int32_t idx_b)
133 {
134  int32_t alen, blen;
135  bool afree, bfree;
136 
137  uint16_t* avec=((CSimpleFeatures<uint16_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
138  uint16_t* bvec=((CSimpleFeatures<uint16_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
139 
140  ASSERT(alen==2);
141  ASSERT(blen==2);
142 
144 
145  float64_t k11,k12,k21,k22;
146  int32_t idx_a1=avec[0], idx_a2=avec[1], idx_b1=bvec[0], idx_b2=bvec[1];
147 
148  k11 = subkernel->kernel(idx_a1,idx_b1);
149  k12 = subkernel->kernel(idx_a1,idx_b2);
150  k21 = subkernel->kernel(idx_a2,idx_b1);
151  k22 = subkernel->kernel(idx_a2,idx_b2);
152 
153  float64_t result = k11+k22-k21-k12;
154 
155  ((CSimpleFeatures<uint16_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
156  ((CSimpleFeatures<uint16_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
157 
158  return result;
159 }

SHOGUN Machine Learning Toolbox - Documentation