forked from antononcube/MathematicaForPrediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassifierEnsembles.m
593 lines (429 loc) · 24.1 KB
/
ClassifierEnsembles.m
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
(*
Classifier ensembles functions Mathematica package
Copyright (C) 2016 Anton Antonov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Written by Anton Antonov,
antononcube @ gmail . com,
Windermere, Florida, USA.
*)
(*
Mathematica is (C) Copyright 1988-2016 Wolfram Research, Inc.
Protected by copyright law and international treaties.
Unauthorized reproduction or distribution subject to severe civil
and criminal penalties.
Mathematica is a registered trademark of Wolfram Research, Inc.
*)
(* Mathematica Package *)
(* :Title: ClassifierEnsembles *)
(* :Context: ClassifierEnsembles` *)
(* :Author: Anton Antonov *)
(* :Date: 2016-10-12 *)
(* :Package Version: 0.1 *)
(* :Mathematica Version: *)
(* :Copyright: (c) 2016 Anton Antonov *)
(* :Keywords: *)
(* :Discussion:
This package provides functions for creation and classification with ensembles of classifiers.
An ensemble of classifiers is simply an Association that maps classifier IDs to classifier functions.
Given a classifier ensemble we have the obvious option to classify a record by classifier voting.
Each classifier returns a label, we tally the returned labels, the returned label of the ensemble is
the label with the largest tally number.
Since ClassifierFunction has the method "Probabilities" for a classifier ensemble we can also average
the probabilities for each label, and return the label with the highest average probability.
If a threshold is specified for a label, then we can pick that label as the classification result
if its average probability is above the threshold.
The functions in this package are especially useful when used together with functions of
the package ROCFunctions.m. See:
https://github.com/antononcube/MathematicaForPrediction/blob/master/ROCFunctions.m .
An attempt to import the package ROCFunctions.m is made if definitions of its functions are not found.
Usage example
=============
## Getting data
data = ExampleData[{"MachineLearning", "Titanic"}, "TrainingData"];
data = ((Flatten@*List) @@@ data)[[All, {1, 2, 3, -1}]];
trainingData = DeleteCases[data, {___, _Missing, ___}];
data = ExampleData[{"MachineLearning", "Titanic"}, "TestData"];
data = ((Flatten@*List) @@@ data)[[All, {1, 2, 3, -1}]];
testData = DeleteCases[data, {___, _Missing, ___}];
## Create a classifier ensemble
aCLs = EnsembleClassify[Automatic, trainingData[[All, 1 ;; -2]] -> trainingData[[All, -1]]]
## Classify a record
EnsembleClassify[aCLs, testData[[1, 1 ;; -2]]]
(* "survived" *)
EnsembleClassifyByThreshold[aCLs, testData[[1, 1 ;; -2]], "survived" -> 2, "Votes"]
(* "survived" *)
EnsembleClassifyByThreshold[aCLs, testData[[1, 1 ;; -2]], "survived" -> 0.2, "ProbabilitiesMean"]
(* "survived" *)
## Classify a list of records using a thershold
### Return "survived" if it gets at least two votes
EnsembleClassifyByThreshold[aCLs, testData[[1 ;; 12, 1 ;; -2]], "survived" -> 2, "Votes"]
(* {"survived", "died", "survived", "survived", "died", "survived", \
"survived", "survived", "died", "survived", "died", "survived"} *)
### Return "survived" if its average probability is at least 0.7
EnsembleClassifyByThreshold[aCLs, testData[[1 ;; 12, 1 ;; -2]], "survived" -> 0.7, "ProbabilitiesMean"]
(* {"survived", "died", "survived", "died", "died", \
"survived", "died", "survived", "died", "survived", "died", \
"survived"} *)
## Threshold classification with ROC
rocRange = Range[0, 1, 0.1];
aROCs =
Table[(cres = EnsembleClassifyByThreshold[aCLs, testData[[All, 1 ;; -2]], "survived" -> i];
ToROCAssociation[{"survived", "died"}, testData[[All, -1]], cres]),
{i, rocRange}];
ROCPlot[rocRange, aROCs]
This file was created by Mathematica Plugin for IntelliJ IDEA.
Anton Antonov
2016-10-12
Winderemere, FL, 2016
*)
(*
TODO
1. Better error messages.
2. Add error message for EnsembleClassifierROCData and EnsembleClassifierROCPlots.
*)
(**************************************************************)
(* Importing packages (if needed) *)
(**************************************************************)
If[Length[DownValues[ROCFunctions`ToROCAssociation]] == 0,
Echo["ROCFunctions.m", "Importing from GitHub:"];
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/ROCFunctions.m"]
];
If[Length[DownValues[CrossTabulate`CrossTabulate]] == 0,
Echo["CrossTabulate.m", "Importing from GitHub:"];
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/CrossTabulate.m"]
];
(**************************************************************)
(* Package definition *)
(**************************************************************)
BeginPackage["ClassifierEnsembles`"];
EnsembleClassifier::usage = "EnsembleClassifier[ cls : (Automatic | _String | {_String..} ), args__) ] \
creates an ensemble of classifiers over the same data using Classify. \
Returns an Association of IDs mapped to classifier functions. \
The argument cls is expected to be specify which Classify methods to be used.";
EnsembleClassifierVotes::usage = "Finds votes by a classifier ensemble for a record or a list of records.";
EnsembleClassifierProbabilities::usage = "Gives the averaged probabilities of a classifier ensemble \
for a record or a list of records.";
EnsembleClassify::usage = "EnsembleClassify[ cls_Association, r_, type_. ] \
classifies by a classifier ensemble for a record or a list of records. \
The third argument is one of \"Votes\" or \"ProbabilitiesMean\".";
EnsembleClassifyByThreshold::usage = "EnsembleClassifyByThreshold[ cls_Association, r_, thr_, type_. ] \
Classifies by a classifier ensemble for a record or a list of records. \
The third argument is a rule label->threshold or an association of such rules. \
The fourth argument is one of \"Votes\" or \"ProbabilitiesMean\". \
A specified label is the classification result if its votes or average probability are higher or equal than \
the corresponding threshold.";
ClassifyByThreshold::usage = "A shortcut to calling EnsembleClassifyByThreshold using a classifier function \
instead of a classifier ensemble.";
EnsembleClassifierMeasurements::usage = "EnsembleClassifierMeasurements[ cls_Association, testData_, props_ ] \
gives measurements corresponding to props when the ensemble of classifiers cls is evaluated over testData. \
(Emulates ClassifierMeasurements for ensembles of classifiers.)";
ResamplingEnsembleClassifier::usage = "ResamplingEnsembleClassifier[ {(_String | {_String, _?NumberQ} | {_String, _?NumberQ, _Integer}) ..}, data_ ] \
builds an ensemble classifier based on a specification.";
EnsembleClassifierROCData::usage = "EnsembleClassifierROCData[ cls_Association, testData_, thRange_, targetClasses_ ] \
returns an association of classifier ensemble ROC data.";
EnsembleClassifierROCPlots::usage = "EnsembleClassifierROCPlots[ cls_Association, testData_, thRange_, targetClasses_, opts___ ] \
returns an association of classifier ensemble ROC plots.";
EnsembleClassifierConfusionMatrix::usage = "EnsembleClassifierConfusionMatrix[ cls_Association, testData_, spec_, opts___ ] \
computes the confusion matrix for a classifier ensemble and test data. \
The third argument is expected to be one of \"Votes\" or \"ProbabilitiesMean\".
If the fourth argument is a label-threshold specification then EnsembleClassifyByThreshold is used.";
Begin["`Private`"];
Needs["ROCFunctions`"];
Needs["CrossTabulate`"];
Clear[EnsembleClassifier];
EnsembleClassifier::nargs =
"The first argument is expected to match (_String|{_String..}|Automatic). \
The rest of the arguments are given to Classify.";
EnsembleClassifier[Automatic, args___] :=
EnsembleClassifier[{
"GradientBoostedTrees",
"LogisticRegression",
"NaiveBayes",
"NearestNeighbors",
"NeuralNetwork",
"RandomForest",
"SupportVectorMachine"
}, args];
EnsembleClassifier[clID_String, args___] := EnsembleClassifier[{clID}, args];
EnsembleClassifier[clIDs : {_String ..}, args___] :=
Association @ Table[cl -> Classify[args, Method -> cl], {cl, clIDs}];
EnsembleClassifier[___] := (Message[EnsembleClassifier::nargs]; $Failed);
(**************************************************************)
(* Resampling classifier making *)
(**************************************************************)
Clear[ClassifierDataQ];
ClassifierDataQ[data_] :=
MatchQ[data, {Rule[_List, _] ..}] && ArrayQ[data[[All, 1]]] || MatchQ[data, {Rule[_?AtomQ, _] ..}];
Clear[ClassifierMethodQ];
ClassifierMethodQ[x_] := StringQ[x] || MatchQ[ x, {_String, _Rule..} ]; (* And check is it known by Classify. *)
Clear[ResamplingEnsembleClassifier];
ResamplingEnsembleClassifier[specs : {(_?ClassifierMethodQ | {_?ClassifierMethodQ, _?NumberQ} | {_?ClassifierMethodQ, _?NumberQ, _Integer} | {_?ClassifierMethodQ, _?NumberQ, _Integer, RandomSample|RandomChoice}) ..},
data_?ClassifierDataQ, args___] :=
Block[{fullSpecs},
fullSpecs =
specs /. {
m_?ClassifierMethodQ :> <| "method"-> m |>,
{ m_?ClassifierMethodQ, f_?NumberQ} :> <| "method"->m, "sampleFraction"->f|>,
{ m_?ClassifierMethodQ, f_?NumberQ, n_Integer } :> <| "method"->m, "sampleFraction"->f, "numberOfClassifiers"->n|>,
{ m_?ClassifierMethodQ, f_?NumberQ, n_Integer, sf:(RandomSample|RandomChoice) } :> <| "method"->m, "sampleFraction"->f, "numberOfClassifiers"->n, "samplingFunction"->sf|>
};
ResamplingEnsembleClassifier[ fullSpecs, data, args ]
];
ResamplingEnsembleClassifier::wskey = "The given specification key `1` is not one of `2`.";
ResamplingEnsembleClassifier[specs:{_Association..}, data_?ClassifierDataQ, args___ ] :=
Block[{fullSpecs, res, knownSpecKeys, allSpecKeys},
knownSpecKeys = {"method", "sampleFraction", "numberOfClassifiers", "samplingFunction"};
allSpecKeys = Union[Flatten[Keys/@specs]];
If[ Length[Complement[allSpecKeys, knownSpecKeys]] > 0,
Message[ResamplingEnsembleClassifier::wskey, #, knownSpecKeys ] & /@ Complement[allSpecKeys, knownSpecKeys]
];
fullSpecs = Map[ Join[ <| "method"->"LogisticRegression", "sampleFraction"->0.9, "numberOfClassifiers"->1, "samplingFunction"->RandomChoice |>, # ]&, specs];
res =
Map[
Table[ToString[#["method"]] <> "[" <> ToString[i] <> "," <> ToString[#["sampleFraction"]] <> "]" ->
Classify[#["samplingFunction"][data, Floor[#["sampleFraction"]*Length[data]]], args, Method -> #["method"]], {i, #["numberOfClassifiers"]}] &,
fullSpecs];
Association@Flatten[res, 1]
];
(**************************************************************)
(* Ensemble classification functions *)
(**************************************************************)
Clear[EnsembleClassifierVotes];
EnsembleClassifierVotes::nargs =
"The first argument is expected to be an Association of classifier IDs to \
classifier functions. The second argument is expected to be a vector or a \
matrix.";
EnsembleClassifierVotes[cls_Association, record_?VectorQ] :=
Association[Rule @@@ Sort[Tally[Through[Values[cls][record]]], -#[[-1]] &]];
EnsembleClassifierVotes[cls_Association, records_?MatrixQ] :=
Map[Association[Rule @@@ Sort[Tally[#], -#[[-1]] &]] &, Transpose[Through[Values[cls][records]]]];
EnsembleClassifierVotes[___] := (Message[EnsembleClassifierVotes::nargs]; $Failed);
Clear[EnsembleClassifierProbabilities];
EnsembleClassifierProbabilities::nargs =
"The first argument is expected to be an Association of classifier IDs to \
classifier functions. The second argument is expected to be a vector or a \
matrix.";
EnsembleClassifierProbabilities[cls_Association, record_?VectorQ] :=
Mean[Through[Values[cls][record, "Probabilities"]]];
EnsembleClassifierProbabilities[cls_Association, records_?MatrixQ] :=
Mean /@ Transpose[Through[Values[cls][records, "Probabilities"]]];
EnsembleClassifierProbabilities[___] := (Message[EnsembleClassifierProbabilities::nargs]; $Failed);
Clear[EnsembleClassify];
EnsembleClassify::nargs =
"The first argument is expected to be an Association of classifier IDs to \
classifier functions. The second argument is expected to be a vector or a \
matrix. The third argument is expected to be one of \"Votes\" or \
\"ProbabilitiesMean\".";
EnsembleClassify[cls_Association, record_] := EnsembleClassify[cls, record, "Votes"];
EnsembleClassify[cls_Association, record_?VectorQ, "Votes"] :=
First@Keys@TakeLargest[EnsembleClassifierVotes[cls, record], 1];
EnsembleClassify[cls_Association, records_?MatrixQ, "Votes"] :=
Map[First@Keys@TakeLargest[#, 1] &, EnsembleClassifierVotes[cls, records]];
EnsembleClassify[cls_Association, record_?VectorQ, "ProbabilitiesMean"] :=
First@Keys@
TakeLargest[Mean[Through[Values[cls][record, "Probabilities"]]], 1];
EnsembleClassify[cls_Association, records_?MatrixQ, "ProbabilitiesMean"] :=
Map[First@Keys@TakeLargest[#, 1] &,
EnsembleClassifierProbabilities[cls, records]];
EnsembleClassify[___] := (Message[EnsembleClassify::nargs]; $Failed);
(**************************************************************)
(* EnsembleClassifyByThreshold *)
(**************************************************************)
Clear[EnsembleClassifyByThreshold];
EnsembleClassifyByThreshold::nargs =
"The first argument is expected to be an Association of classifier IDs to \
classifier functions. The second argument is expected to be a vector or a \
matrix. The third argument is expected to be a label-threshold rule or a list of label-threshold rules.
The specified threshold(s) must be numerical. The fourth argument is expected to be one of \
\"Votes\" or \"ProbabilitiesMean\".";
EnsembleClassifyByThreshold[cls_Association,
records : ( _?VectorQ | _?MatrixQ ),
label_ -> threshold_?NumericQ,
method_String: "ProbabilitiesMean"] :=
EnsembleClassifyByThreshold[ cls, records, {label->threshold}, method ];
EnsembleClassifyByThreshold[cls_Association,
records : ( _?VectorQ | _?MatrixQ ),
thresholds : Association[ (_ -> _?NumericQ) ..],
method_String: "ProbabilitiesMean"] :=
EnsembleClassifyByThreshold[ cls, records, Normal[thresholds], method ];
EnsembleClassifyByThreshold[cls_Association,
records : ( _?VectorQ | _?MatrixQ ),
thresholds: { (_ -> _?NumericQ) .. },
method_String: "ProbabilitiesMean"] :=
Block[{pmeans, code},
Which[
TrueQ[method == "ProbabilitiesMean"],
pmeans = EnsembleClassifierProbabilities[cls, records],
VectorQ[records],
pmeans = Join[AssociationThread[ Keys[thresholds] -> 0 ], EnsembleClassifierVotes[cls, records]],
True,
pmeans = Map[Join[AssociationThread[ Keys[thresholds] -> 0 ], #] &, EnsembleClassifierVotes[cls, records]]
];
(* Make threshold classification function. *)
(* Is this code slow for a large number specified label-threshold rules? *)
(* It can be with associations Merge with Subtract and Select instead of Which. *)
code =
Join[
Flatten[ MapThread[ Function[{k,v}, { #[ k ] >= v, k }], Transpose[ List @@@ thresholds ] ] ],
{ Length[thresholds] < Hold[Length[#]], Hold[First@Keys@TakeLargest[ KeyDrop[#,Keys[thresholds]], 1]] },
{ True, Hold[First@Keys@TakeLargest[#,1]] }
];
code = ReleaseHold[Evaluate[Which @@ code]&];
If[ VectorQ[records], code @ pmeans, code /@ pmeans ]
];
EnsembleClassifyByThreshold[___] := (Message[EnsembleClassifyByThreshold::nargs]; $Failed);
ClassifyByThreshold[ cf_ClassifierFunction, data:(_?VectorQ|_?MatrixQ), label_ -> threshold_?NumericQ ] :=
EnsembleClassifyByThreshold[ <| "cf"->cf |>, data, label->threshold, "ProbabilitiesMean" ];
(**************************************************************)
(* Calculating classifier ensemble measurements *)
(**************************************************************)
Clear[EnsembleClassifierMeasurements];
EnsembleClassifierMeasurements::nargs =
"The first argument, the classifier ensemble, is expected to be an Association of classifier IDs to \
classifier functions. \
The second argument, the test data, is expected to be a list of record-to-label rules. \
The third argument is expected to be a list of measures; see ROCFunctions`ROCFunctions[\"FunctionNames\"]. \
Use the option \"Classes\" to specify target classes. \
Use the option Method to specify which method the classifier ensemble should classify with.";
Options[EnsembleClassifierMeasurements] = {"Classes"->Automatic, Method -> Automatic};
EnsembleClassifierMeasurements[cls_Association,
testData_?ClassifierDataQ, measure_String,
opts : OptionsPattern[]] :=
First @ EnsembleClassifierMeasurements[cls, testData, {measure}, opts];
EnsembleClassifierMeasurements[cls_Association, testData_, args___] :=
EnsembleClassifierMeasurements[cls, Thread[testData], args] /; MatchQ[testData, Rule[_?ArrayQ, _]];
EnsembleClassifierMeasurements[cls_Association, testData_?ClassifierDataQ, measures : {_String ..}, opts : OptionsPattern[]] :=
Block[{targetClasses, cfMethod, testLabels, clRes, clVals, clClasses, aROCs, knownMeasures,
ccNotLabel, ccTestLabels, ccModelVals},
targetClasses = OptionValue[EnsembleClassifierMeasurements, "Classes"];
cfMethod = OptionValue[EnsembleClassifierMeasurements, Method];
If[ cfMethod === Automatic,
cfMethod = (EnsembleClassify[#1, #2, "ProbabilitiesMean"] &)
];
testLabels = testData[[All, 2]];
clVals = cfMethod[cls, testData[[All, 1]]];
(* It is assumed here that all ClassifierFunction objects have the same classes. *)
(* clClasses = ClassifierInformation[cls[[1]], "Classes"]; *)
clClasses = Information[cls[[1]], "Classes"];
If[ targetClasses === Automatic,
targetClasses = clClasses;
];
knownMeasures = measures /. {"Precision" -> "PPV", "Recall" -> "TPR", "Accuracy" -> "ACC"};
clRes =
Table[
If[ MemberQ[ targetClasses, clClasses[[i]] ],
ccNotLabel = "Not-"<>ToString[clClasses[[i]]];
ccTestLabels = Map[ If[# == clClasses[[i]], #, ccNotLabel]&, testLabels ];
ccModelVals = Map[ If[# == clClasses[[i]], #, ccNotLabel]&, clVals ];
aROCs =
ToROCAssociation[{ clClasses[[i]], ccNotLabel }, ccTestLabels, ccModelVals];
clClasses[[i]] ->
AssociationThread[measures -> Through[N[ROCFunctions[knownMeasures][aROCs]]]],
(*ELSE*)
Nothing
],
{i, Length[clClasses]}
];
clRes = Dataset[Association@clRes];
clRes = Map[Normal[clRes[All, #]] &, measures];
MapThread[If[MemberQ[{"Accuracy", "ACC"}, #1], First@Values[#2], #2] &, {measures, clRes}]
];
(**************************************************************)
(* Calculating classifier ensemble ROC data and plots *)
(**************************************************************)
Clear[EnsembleClassifierROCData];
EnsembleClassifierROCData::nargs =
"The first argument, the classifier ensemble, is expected to be an Association of classifier IDs to \
classifier functions. \
The second argument, the test data, is expected to be a list of record-to-label rules. \
The optional third argument, the threshold range, is expected to be a list of numbers between 0 and 1. \
The optional fourth argument, the target classes, is expected to be list of class labels or All."
EnsembleClassifierROCData[aCL_Association,
testData_?ClassifierDataQ,
thRange : {_?NumericQ ..}, targetClasses : (_List | All) : All] :=
Block[{clClasses, clRes, testLabels, ccLabel, ccNotLabel, ccTestLabels, rocs},
If[TrueQ[targetClasses === All || targetClasses === Automatic],
clClasses = Information[aCL[[1]], "Classes"],
clClasses = targetClasses
];
clRes = EnsembleClassifierProbabilities[aCL, testData[[All, 1]]];
testLabels = testData[[All, 2]];
Table[
ccNotLabel = "Not-" <> ToString[ccLabel];
ccTestLabels =
Map[If[# == ccLabel, #, ccNotLabel] &, testLabels];
rocs =
Table[
Join[
ToROCAssociation[{ccLabel, ccNotLabel}, ccTestLabels,
Map[If[# >= th, ccLabel, ccNotLabel] &, Through[clRes[ccLabel]]]],
<|"ROCParameter"->th|>
],
{th, thRange}];
ccLabel -> rocs,
{ccLabel, clClasses}]
];
EnsembleClassifierROCData[___] := (Message[EnsembleClassifierROCData::nargs]; $Failed);
Clear[EnsembleClassifierROCPlots];
EnsembleClassifierROCPlots::nargs =
"The first argument, the classifier ensemble, is expected to be an Association of classifier IDs to \
classifier functions. \
The second argument, the test data, is expected to be a list of record-to-label rules. \
The optional third argument, the threshold range, is expected to be a list of numbers between 0 and 1. \
The optional fourth argument, the target classes, is expected to be list of class labels or All. \
As options the options of ROCFunctions`ROCPlot and Graphics can be given.";
Options[EnsembleClassifierROCPlots] = Options[ROCPlot];
EnsembleClassifierROCPlots[aCL_Association,
testData_?ClassifierDataQ,
thRange : {_?NumericQ ..}, targetClasses : (_List | All) : All,
opts : OptionsPattern[]] :=
Block[{rocRes},
rocRes = Association@EnsembleClassifierROCData[aCL, testData, thRange, targetClasses];
AssociationMap[ROCPlot[thRange, rocRes[#], opts] &, Keys[rocRes]]
];
EnsembleClassifierROCPlots[___] := (Message[EnsembleClassifierROCPlots::nargs]; $Failed);
(**************************************************************)
(* Calculating classifier ensemble confusion matrix *)
(**************************************************************)
Clear[ThresholdsSpecQ];
ThresholdsSpecQ[spec_]:= MatchQ[ spec, ( None | {} | (_->_?NumericQ) | { (_->_?NumericQ).. } | Association[ (_->_?NumericQ).. ] )]
Clear[EnsembleClassifierConfusionMatrix];
EnsembleClassifierConfusionMatrix::nargs =
"The first argument, the classifier ensemble, is expected to be an Association of classifier IDs to \
classifier functions. \
The second argument, the test data, is expected to be a list of record-to-label rules. \
The third argument, is expected to be one of \"ProbabilitiesMean\" or \"Votes\". \
The optional fourth argument, is expected to be a label-threshold specification or None.";
Options[EnsembleClassifierConfusionMatrix] = Options[CrossTabulate];
EnsembleClassifierConfusionMatrix[
aCL_Association,
testData_?ClassifierDataQ,
aggrSpec : ("ProbabilitiesMean" | "Votes" ) : "ProbabilitiesMean",
opts:OptionsPattern[] ] :=
EnsembleClassifierConfusionMatrix[aCL, testData, aggrSpec, None, opts];
EnsembleClassifierConfusionMatrix[
aCL_Association,
testData_?ClassifierDataQ,
aggrSpec : ("ProbabilitiesMean" | "Votes" ),
thresholds_?ThresholdsSpecQ,
opts:OptionsPattern[] ] :=
Block[{lsClassLabels},
If[ TrueQ[ thresholds === None ] || TrueQ[ thresholds === {} ],
lsClassLabels = EnsembleClassify[aCL, testData[[All, 1]], aggrSpec],
(* ELSE*)
lsClassLabels = EnsembleClassifyByThreshold[aCL, testData[[All, 1]], thresholds]
];
CrossTabulate[ Transpose[{ testData[[All, 2]], lsClassLabels} ], opts ]
];
EnsembleClassifierConfusionMatrix[___] := (Message[EnsembleClassifierConfusionMatrix::nargs]; $Failed);
End[]; (* `Private` *)
EndPackage[]