SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GUILabels.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 Soeren Sonnenburg
8  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/ui/GUILabels.h>
12 #include <shogun/ui/SGInterface.h>
13 
14 #include <shogun/lib/config.h>
15 #include <shogun/io/SGIO.h>
16 #include <shogun/io/CSVFile.h>
17 #include <shogun/labels/Labels.h>
21 
22 #include <string.h>
23 
24 using namespace shogun;
25 
26 CGUILabels::CGUILabels(CSGInterface* ui_)
27 : CSGObject(), ui(ui_), train_labels(NULL), test_labels(NULL)
28 {
29 }
30 
32 {
35 }
36 
37 bool CGUILabels::load(char* filename, char* target)
38 {
39  CLabels* labels=NULL;
40 
41  if (strncmp(target, "TEST", 4)==0)
42  labels=test_labels;
43  else if (strncmp(target, "TRAIN", 5)==0)
44  labels=train_labels;
45  else
46  SG_ERROR("Invalid target %s.\n", target)
47 
48  if (labels)
49  {
50  SG_UNREF(labels);
51  CCSVFile* file=new CCSVFile(filename);
52  labels=new CRegressionLabels(file);
53  SGVector<float64_t> labs = ((CRegressionLabels*) labels)->get_labels();
55  labels=infer_labels(lab, labs.vlen);
56 
57  if (labels)
58  {
59  if (strncmp(target, "TEST", 4)==0)
60  set_test_labels(labels);
61  else
62  set_train_labels(labels);
63 
64  return true;
65  }
66  else
67  SG_ERROR("Loading labels failed.\n")
68 
69  SG_UNREF(file);
70  }
71 
72  return false;
73 }
74 
75 bool CGUILabels::save(char* param)
76 {
77  bool result=false;
78  return result;
79 }
80 
82 {
83  CLabels* labels=NULL;
84 
85  bool binary=true;
86  bool multiclass=true;
87  for (int32_t i=0; i<len; i++)
88  {
89  if (lab[i]!=-1 && lab[i]!=+1)
90  binary=false;
91 
92  if (lab[i]<0 || lab[i]!=int(lab[i]))
93  multiclass=false;
94 
95  if (binary == false && multiclass == false)
96  {
97  labels=new CRegressionLabels(SGVector<float64_t>(lab, len));
98  break;
99  }
100  }
101 
102  if (multiclass)
103  labels=new CMulticlassLabels(SGVector<float64_t>(lab, len));
104  if (binary)
105  labels=new CBinaryLabels(SGVector<float64_t>(lab, len));
106 
107  return labels;
108 }
CLabels * infer_labels(float64_t *lab, int32_t len)
Definition: GUILabels.cpp:81
Real Labels are real-valued labels.
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
#define SG_ERROR(...)
Definition: SGIO.h:129
bool save(char *param)
Definition: GUILabels.cpp:75
Multiclass Labels for multi-class classification.
static T * clone_vector(const T *vec, int32_t len)
Definition: SGVector.cpp:213
Class CSVFile used to read data from comma-separated values (CSV) files. See http://en.wikipedia.org/wiki/Comma-separated_values.
Definition: CSVFile.h:29
index_t vlen
Definition: SGVector.h:494
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
double float64_t
Definition: common.h:50
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
bool set_test_labels(CLabels *lab)
Definition: GUILabels.h:48
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
bool load(char *filename, char *target)
Definition: GUILabels.cpp:37
CLabels * test_labels
Definition: GUILabels.h:77
bool set_train_labels(CLabels *lab)
Definition: GUILabels.h:44
CLabels * train_labels
Definition: GUILabels.h:75

SHOGUN Machine Learning Toolbox - Documentation