SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
RelaxedTreeUtil.cpp
浏览该文件的文档.
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) 2012 Chiyuan Zhang
8  * Copyright (C) 2012 Chiyuan Zhang
9  */
10 
14 
15 using namespace shogun;
16 
18 {
19  const int32_t N_splits = 2; // 5
21  split->build_subsets();
22 
23  SGMatrix<float64_t> conf_mat(num_classes, num_classes), tmp_mat(num_classes, num_classes);
24  conf_mat.zero();
25 
26  machine->set_labels(Y);
27  machine->set_store_model_features(true);
28 
29  for (int32_t i=0; i < N_splits; ++i)
30  {
31  // subset for training
32  SGVector<index_t> inverse_subset_indices = split->generate_subset_inverse(i);
33  X->add_subset(inverse_subset_indices);
34  Y->add_subset(inverse_subset_indices);
35 
36  machine->train(X);
37  X->remove_subset();
38  Y->remove_subset();
39 
40  // subset for predicting
41  SGVector<index_t> subset_indices = split->generate_subset_indices(i);
42  X->add_subset(subset_indices);
43  Y->add_subset(subset_indices);
44 
45  CMulticlassLabels *pred = machine->apply_multiclass(X);
46 
47  get_confusion_matrix(tmp_mat, Y, pred);
48 
49  for (index_t j=0; j < tmp_mat.num_rows; ++j)
50  {
51  for (index_t k=0; k < tmp_mat.num_cols; ++k)
52  {
53  conf_mat(j, k) += tmp_mat(j, k);
54  }
55  }
56 
57  SG_UNREF(pred);
58 
59  X->remove_subset();
60  Y->remove_subset();
61  }
62 
63  SG_UNREF(split);
64 
65  for (index_t j=0; j < tmp_mat.num_rows; ++j)
66  {
67  for (index_t k=0; k < tmp_mat.num_cols; ++k)
68  {
69  conf_mat(j, k) /= N_splits;
70  }
71  }
72 
73  return conf_mat;
74 }
75 
77 {
79 
80  for (index_t i=0; i < conf_mat.num_rows; ++i)
81  {
82  float64_t n=0;
83  for (index_t j=0; j < conf_mat.num_cols; ++j)
84  {
85  conf_mat(i, j) = conf_mat_int(i, j);
86  n += conf_mat(i, j);
87  }
88 
89  if (n != 0)
90  {
91  for (index_t j=0; j < conf_mat.num_cols; ++j)
92  conf_mat(i, j) /= n;
93  }
94  }
95 }
int32_t index_t
Definition: common.h:62
SGMatrix< float64_t > estimate_confusion_matrix(CBaseMulticlassMachine *machine, CFeatures *X, CMulticlassLabels *Y, int32_t num_classes)
index_t num_cols
Definition: SGMatrix.h:378
void split(v_array< ds_node< P > > &point_set, v_array< ds_node< P > > &far_set, int max_scale)
Definition: JLCoverTree.h:149
index_t num_rows
Definition: SGMatrix.h:376
Multiclass Labels for multi-class classification.
virtual void set_store_model_features(bool store_model)
Definition: Machine.cpp:107
void get_confusion_matrix(SGMatrix< float64_t > &conf_mat, CMulticlassLabels *gt, CMulticlassLabels *pred)
static SGMatrix< int32_t > get_confusion_matrix(CLabels *predicted, CLabels *ground_truth)
double float64_t
Definition: common.h:50
virtual void remove_subset()
Definition: Labels.cpp:49
virtual void add_subset(SGVector< index_t > subset)
Definition: Labels.cpp:39
SGVector< index_t > generate_subset_inverse(index_t subset_idx)
#define SG_UNREF(x)
Definition: SGObject.h:52
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
SGVector< index_t > generate_subset_indices(index_t subset_idx)
virtual void remove_subset()
Definition: Features.cpp:322
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual bool train(CFeatures *data=NULL)
Definition: Machine.cpp:39
Implementation of normal cross-validation on the base of CSplittingStrategy. Produces subset index se...
virtual CMulticlassLabels * apply_multiclass(CFeatures *data=NULL)
Definition: Machine.cpp:220
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:65
virtual void add_subset(SGVector< index_t > subset)
Definition: Features.cpp:310

SHOGUN 机器学习工具包 - 项目文档