-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
cross_validator.dart
139 lines (130 loc) · 4.58 KB
/
cross_validator.dart
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
import 'package:ml_algo/src/common/constants/default_parameters/common.dart';
import 'package:ml_algo/src/metric/metric_type.dart';
import 'package:ml_algo/src/model_selection/_init_module.dart';
import 'package:ml_algo/src/model_selection/_injector.dart';
import 'package:ml_algo/src/model_selection/assessable.dart';
import 'package:ml_algo/src/model_selection/cross_validator/cross_validator_impl.dart';
import 'package:ml_algo/src/model_selection/split_indices_provider/split_indices_provider_factory.dart';
import 'package:ml_algo/src/model_selection/split_indices_provider/split_indices_provider_type.dart';
import 'package:ml_dataframe/ml_dataframe.dart';
import 'package:ml_linalg/dtype.dart';
import 'package:ml_linalg/vector.dart';
typedef ModelFactory = Assessable Function(DataFrame observations);
typedef DataPreprocessFn = List<DataFrame> Function(
DataFrame trainData, DataFrame testData);
/// A factory and an interface for all the cross validator types
abstract class CrossValidator {
/// Creates a k-fold validator to evaluate the quality of a ML model.
///
/// It splits a dataset into [numberOfFolds] test sets and evaluates a model
/// on each produced test set
///
/// Parameters:
///
/// [samples] A dataset that is going to be split into [numberOfFolds] parts
/// to iteratively evaluate on them a model's performance
///
/// [numberOfFolds] A number of parts of the [samples]
///
/// [dtype] A type for all numerical data
factory CrossValidator.kFold(
DataFrame samples, {
int numberOfFolds = 5,
DType dtype = dTypeDefaultValue,
}) {
initModelSelectionModule();
final dataSplitterFactory =
modelSelectionInjector.get<SplitIndicesProviderFactory>();
final dataSplitter = dataSplitterFactory.createByType(
SplitIndicesProviderType.kFold,
numberOfFolds: numberOfFolds);
return CrossValidatorImpl(
samples,
dataSplitter,
dtype,
);
}
/// Creates a LPO validator to evaluate the quality of a ML model.
///
/// It splits a dataset into all possible test sets of size [p] and evaluates
/// the quality of a model on each produced test set.
///
/// Parameters:
///
/// [samples] A dataset that is going to be split into parts to iteratively
/// evaluate on them a model's performance
///
/// [p] A size of a part of [samples] in rows.
///
/// [dtype] A type for all the numerical data.
factory CrossValidator.lpo(
DataFrame samples,
int p, {
DType dtype = dTypeDefaultValue,
}) {
initModelSelectionModule();
final dataSplitterFactory =
modelSelectionInjector.get<SplitIndicesProviderFactory>();
final dataSplitter =
dataSplitterFactory.createByType(SplitIndicesProviderType.lpo, p: p);
return CrossValidatorImpl(
samples,
dataSplitter,
dtype,
);
}
/// Returns a future that is resolved with a vector of scores of quality of a
/// model depending on given [metricType]
///
/// Parameters:
///
/// [createModel] A function that returns a model to be evaluated
///
/// [metricType] A metric used to assess a model created by [createModel]
///
/// [onDataSplit] A callback that is called when a new train-test split is
/// ready to be passed into a model. One may place some additional
/// data-dependent logic here, e.g., data preprocessing. The callback accepts
/// train and test data from a new split and returns a transformed split as a
/// list, where the first element is train data and the second one is test
/// data, both of [DataFrame] type. This new transformed split will be passed
/// into the model
///
/// Example:
///
/// ````dart
/// final data = DataFrame([
/// [ 1, 1, 1, 1],
/// [ 2, 3, 4, 5],
/// [18, 71, 15, 61],
/// [19, 0, 21, 331],
/// [11, 10, 9, 40],
/// ],
/// header: header,
/// headerExists: false,
/// );
/// final modelFactory = (trainData) =>
/// KnnRegressor(trainData, 'col_3', k: 4);
/// final onDataSplit = (trainData, testData) {
/// final standardizer = Standardizer(trainData);
/// return [
/// standardizer.process(trainData),
/// standardizer.process(testData),
/// ];
/// }
/// final validator = CrossValidator.kFold(data);
/// final scores = await validator.evaluate(
/// modelFactory,
/// MetricType.mape,
/// onDataSplit: onDataSplit,
/// );
/// final averageScore = scores.mean();
///
/// print(averageScore);
/// ````
Future<Vector> evaluate(
ModelFactory createModel,
MetricType metricType, {
DataPreprocessFn? onDataSplit,
});
}