-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata.h
247 lines (176 loc) · 7.07 KB
/
data.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* data.h -*- C++ -*-
Jeremy Barnes, 30 September 2009
Copyright (c) 2009 Jeremy Barnes. All rights reserved.
File to hold the data.
*/
#ifndef __ausdm__data_h__
#define __ausdm__data_h__
#include <boost/multi_array.hpp>
#include <vector>
#include <string>
#include "jml/stats/distribution.h"
#include <boost/shared_ptr.hpp>
/// What kind of target are we calculating?
enum Target {
RMSE,
AUC
};
using ML::distribution;
class Decomposition;
/*****************************************************************************/
/* DIFFICULTY */
/*****************************************************************************/
enum Difficulty_Category {
DIF_UNKNOWN, ///< Label is unknown so difficulty is unknown
DIF_AUTOMATIC, ///< Automatically correct (all models are correct)
DIF_POSSIBLE, ///< At least one is correct
DIF_IMPOSSIBLE ///< All models have misclassified it
};
std::string print(const Difficulty_Category & cat);
std::ostream & operator << (std::ostream & stream, Difficulty_Category cat);
struct Difficulty {
Difficulty();
Difficulty(const ML::distribution<float> & model_outputs,
float label,
Target target);
Difficulty_Category category;
float difficulty;
};
/*****************************************************************************/
/* SCORES */
/*****************************************************************************/
struct Scores {
distribution<float> target_values;
operator double() const { return score; }
double category_scores[4]; // one for each difficulty category
double category_averages[4];
double score;
};
/*****************************************************************************/
/* MODEL_STATS */
/*****************************************************************************/
/// The output of one of the models that we are blending
struct Model_Stats {
/// Score over whatever target we are trying to calculate
double score;
/// Rank in accuracy over targets we are trying to calculate
int rank;
};
/*****************************************************************************/
/* MODEL_OUTPUT */
/*****************************************************************************/
/// The output of one of the models that we are blending
struct Model_Output : public distribution<float> {
/// Calculate the RMSE over the given set of targets
double calc_rmse(const distribution<float> & targets) const;
/// Same, but taking into account weights
double calc_rmse(const distribution<float> & targets,
const distribution<float> & weights) const;
/// Calculate the AUC over the given set of targets
double calc_auc(const distribution<float> & targets) const;
/// Same, but with weights. Not sure that it makes much sense
/// mathematically...
double calc_auc(const distribution<float> & targets,
const distribution<float> & weights) const;
/// Calculate the score based upon the target
double calc_score(const distribution<float> & targets,
Target target) const;
/// Calculate the weighted score based upon the target
double calc_score(const distribution<float> & targets,
const distribution<float> & weights,
Target target) const;
};
/*****************************************************************************/
/* TARGET_STATS */
/*****************************************************************************/
/** The statistics for a given target output */
struct Target_Stats {
Target_Stats()
: mean(0.0), std(0.0), min(0.0), max(0.0)
{
}
template<class Iterator>
Target_Stats(Iterator first, const Iterator & last)
{
int n = std::distance(first, last);
double total = 0.0;
float tmin = INFINITY, tmax = -INFINITY;
for (Iterator it = first; it != last; ++it) {
total += *it;
tmin = std::min(tmin, *it);
tmax = std::max(tmax, *it);
}
double mean = total / n;
total = 0.0;
for (Iterator it = first; it != last; ++it)
total += pow(*it - mean, 2);
this->mean = mean;
this->std = sqrt(total);
this->min = tmin;
this->max = tmax;
}
float mean;
float std;
float min;
float max;
};
/*****************************************************************************/
/* DATA */
/*****************************************************************************/
/** Data structure to contain the dataset that we are working on. */
struct Data {
Data()
: decomposition(0)
{
}
void load(const std::string & filename, Target target,
bool clear_first = true);
void hold_out(Data & remove_to, float proportion,
int random_seed = 1);
void hold_out(Data & remove_to, float proportion,
distribution<float> & example_weights,
distribution<float> & remove_to_example_weights,
int random_seed = 1);
void hold_out(Data & remove_to, const std::vector<bool> & to_remove,
distribution<float> & example_weights,
distribution<float> & remove_to_example_weights);
void hold_out(Data & remove_to, const std::vector<bool> & to_remove);
void clear();
void swap(Data & other);
void apply_decomposition(const Decomposition & decomposition);
distribution<float>
apply_decomposition(const distribution<float> & example) const;
int nm() const { return models.size(); }
int nx() const { return examples.size(); }
Target target;
/// Target values to predict
distribution<float> targets;
/// Names of the models
std::vector<std::string> model_names;
/// ID values of the models
std::vector<int> example_ids;
std::vector<Model_Stats> models;
/// Sorted list of models in order of score
std::vector<int> model_ranking;
struct Example {
Example()
: label(0.0)
{
}
Example(const distribution<float> & models, float label, Target target)
: label(label), models(models), stats(models.begin(), models.end()),
difficulty(models, label, target)
{
}
float label;
distribution<float> models;
distribution<float> decomposed;
Target_Stats stats;
Difficulty difficulty;
};
std::vector<boost::shared_ptr<Example> > examples;
const Decomposition * decomposition;
size_t decomposition_size() const;
void calc_scores();
};
#endif /* __ausdm__data_h__ */