SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
multiclass
tree
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
11
#include <
shogun/evaluation/CrossValidationSplitting.h
>
12
#include <
shogun/multiclass/tree/RelaxedTreeUtil.h
>
13
#include <
shogun/evaluation/MulticlassAccuracy.h
>
14
15
using namespace
shogun;
16
17
SGMatrix<float64_t>
RelaxedTreeUtil::estimate_confusion_matrix
(
CBaseMulticlassMachine
*machine,
CFeatures
*X,
CMulticlassLabels
*Y, int32_t num_classes)
18
{
19
const
int32_t N_splits = 2;
// 5
20
CCrossValidationSplitting
*
split
=
new
CCrossValidationSplitting
(Y, N_splits);
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
76
void
RelaxedTreeUtil::get_confusion_matrix
(
SGMatrix<float64_t>
&conf_mat,
CMulticlassLabels
*gt,
CMulticlassLabels
*pred)
77
{
78
SGMatrix<int32_t>
conf_mat_int =
CMulticlassAccuracy::get_confusion_matrix
(pred, gt);
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
}
SHOGUN
机器学习工具包 - 项目文档