-
Notifications
You must be signed in to change notification settings - Fork 95
/
VariableImportanceByClassifiers.m
294 lines (200 loc) · 11 KB
/
VariableImportanceByClassifiers.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
(*
Variable importance determination by classifiers implementation in Mathematica
Copyright (C) 2013-2015 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,
ʇǝu˙oǝʇsod@ǝqnɔuouoʇuɐ,
Windermere, Florida, USA.
*)
(*
Mathematica is (C) Copyright 1988-2015 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.
*)
(* :Title: VariableImportanceByClassifiers *)
(* :Author: Anton Antonov *)
(* :Date: 2015-12-27 *)
(* :Package Version: 1.0 *)
(* :Mathematica Version: 10.3.1 *)
(* :Copyright: (c) 2015 Anton Antonov *)
(* :Keywords: Classify, variable importance, Titanic *)
(* :Discussion:
This package has a function that can be used to find the importance of variables in a data set.
-------------------
Procedure outline
-------------------
1. Build a classifier with the training set.
2. Verify using the test set that good classification results are obtained.
3. If the number of variables (attributes) is k for each i, 1≤i≤k :
3.1. Shuffle the values of the i-th column of the test data and find the classification success rates.
4. Compare the obtained k classification success rates between each other and
with the success rates obtained by the un-shuffled test data.
The variables for which the classification success rates are the worst are the most decisive.
Instead of overall classification accuracy the package can work with precisions of a given set of labels.
-------------------
Examples
-------------------
1. Load some data. (Using the Titanic dataset.)
testSetName = "Titanic"; (* "Mushroom" *)
trainingSet = ExampleData[{"MachineLearning", testSetName}, "TrainingData"];
testSet = ExampleData[{"MachineLearning", testSetName}, "TestData"];
2. Variable names and unique class labels.
varNames = Flatten[List @@ ExampleData[{"MachineLearning", testSetName}, "VariableDescriptions"]]
(* {"passenger class", "passenger age", "passenger sex", "passenger survival"} *)
classLabels = Union[ExampleData[{"MachineLearning", testSetName}, "Data"][[All, -1]]]
(* {"died", "survived"} *)
3. Make the classifier.
clFunc = Classify[trainingSet, Method -> "RandomForest"]
4. Obtain accuracies after shuffling.
accs = AccuracyByVariableShuffling[clFunc, testSet, varNames]
(* <|None -> 0.778626, "passenger class" -> 0.743003, "passenger age" -> 0.768448, "passenger sex" -> 0.580153|> *)
5. Tabulate the results.
Grid[
Prepend[
List @@@ Normal[accs/First[accs]],
Style[#, Bold, Blue, FontFamily -> "Times"] & /@ {"shuffled variable", "accuracy ratio"}],
Alignment -> Left, Dividers -> All]
6. Further confirmation of the found variable importance can be done using the mosaic plots.
(The package for MosaicPlot is provided by this repository. See the references. )
t = (Flatten /@ (List @@@ trainingSet));
MosaicPlot[t[[All, {1, 3, 4}]], ColorRules -> {3 -> ColorData[7, "ColorList"]} ]
4a. In order to use precision per class labels instead of overall accuracy the desired class labels
are specified with the option "Classes".
accs = AccuracyByVariableShuffling[clFunc, testSet, varNames, "Classes" -> classLabels]
(* <|None -> {0.836158, 0.658824},
"passenger class" -> {0.796992, 0.574803},
"passenger age" -> {0.824197, 0.638132},
"passenger sex" -> {0.704797, 0.344262}|> *)
4b. Here is another example that uses the class label with the smallest precision.
(Probably the most important since it is most mis-classified).
accs = AccuracyByVariableShuffling[clFunc, testSet, varNames,
"Classes" -> Position[#, Min[#]][[1, 1, 1]] &@
ClassifierMeasurements[clFunc, testSet, "Precision"]]
(* <|None -> {0.658824},
"passenger class" -> {0.54321},
"passenger age" -> {0.666667},
"passenger sex" -> {0.347107}|> *)
-------------------
References
-------------------
I read the description of this procedure in the book:
[1] Breiman, L. et al., Classification and regression trees, Chapman & Hall, 1984.
For further references, examples, and discussions with census data see the blog post:
[2] "Classification and association rules for census income data"
Posted on March 30, 2014 by Anton Antonov Antonov on MathematicaForPrediction at WordPress.com .
URL: https://mathematicaforprediction.wordpress.com/2014/03/30/classification-and-association-rules-for-census-income-data/
The mosaic plots suggested above can be made using this package:
[3] "Mosaic plot for data visualization implementation in Mathematica" (2014)
https://github.com/antononcube/MathematicaForPrediction/blob/master/MosaicPlot.m
This file was created using Mathematica Plugin for IntelliJ IDEA.
Anton Antonov
2015-12-28
*)
(*
2017-06-17
Made it work with classifier ensembles.
Replaced the name of the option "FScoreLabels" with "Classes".
2018-05-03
Replaced "Classes" with "ClassLabels".
*)
BeginPackage["VariableImportanceByClassifiers`"]
(* Exported symbols added here with SymbolName::usage *)
AccuracyByVariableShuffling::usage = "AccuracyByVariableShuffling[clFunc, testData, variableNames, opts] computes classification \
accuracies with the ClassifierFunction object clFunc over damaged versions of the data testData. The accuracies can be used \
in variable importance finding. The names of the variables can be specified with variableNames. \
With the option \"Classes\" the accuracies can be computed over a specific list of class labels. \
The result is an Association object with keys the damaged column names of testData (variables) and with values the corresponding \
accuracies.";
Begin["`Private`"];
(*Needs["ClassifierEnsembles`"]*)
AccuracyByVariableShuffling::nfsc = "The option \"FScoreLabels\" is obsolete; use \"ClassLabels\" instead.";
Clear[ClassifierQ, ClassifierDataQ, AccuracyByVariableShuffling]
ClassifierQ[ cl_ ] :=
MatchQ[ cl, _ClassifierFunction] ||
If[Length[DownValues[ClassifierEnsembles`EnsembleClassifierMeasurements]] > 0,
MatchQ[ cl, Association[(_ -> _ClassifierFunction) ..] ]
];
ClassifierDataQ[data_] := MatchQ[ data, { Rule[_List, _] .. } ] && ArrayQ[ data[[ All, 1 ]] ];
AccuracyByVariableShuffling::varnames = "The third argument (variableNames) is expected to be Automatic or a list of strings."
Options[AccuracyByVariableShuffling] = { "FScoreLabels" -> None, "ClassLabels" -> None };
AccuracyByVariableShuffling[ clFunc_?ClassifierQ, testData_?ClassifierDataQ, variableNames_:Automatic, opts:OptionsPattern[] ] :=
Block[{ baseAccuracy, tmat, shuffledTestSets, accuraciesOfShuffledTestSets, varNames, fscoreLabels, targetClasses },
fscoreLabels = OptionValue["FScoreLabels"];
If[ TrueQ[ fscoreLabels =!= None ],
Message[AccuracyByVariableShuffling::nfsc];
];
targetClasses = OptionValue["ClassLabels"];
If[ TrueQ[ targetClasses =!= None ] && AtomQ[targetClasses], targetClasses = {targetClasses} ];
If[targetClasses === None && fscoreLabels =!= None, targetClasses = fscoreLabels ];
(* Matrix/array of attributes *)
tmat = testData[[All, 1]];
(* Variable names *)
varNames =
Which[
TrueQ[ variableNames === Automatic ],
Range[ Dimensions[tmat][[2]] ],
MatchQ[ variableNames, {_String..} ],
Which[
Length[variableNames] == Dimensions[tmat][[2]], variableNames,
Length[variableNames] < Dimensions[tmat][[2]], Join[ variableNames, Range[ Length[variableNames]+1, Dimensions[tmat][[2]] ] ],
Length[variableNames] > Dimensions[tmat][[2]], Take[ variableNames, Dimensions[tmat][[2]] ]
],
True,
Message[AccuracyByVariableShuffling::varnames];
Return[$Failed]
];
(* Find the baseline accuracy. *)
Which[
targetClasses === None && MatchQ[ clFunc, _ClassifierFunction ],
baseAccuracy = ClassifierMeasurements[ clFunc, testData, "Accuracy"],
targetClasses =!= None && MatchQ[ clFunc, _ClassifierFunction ],
baseAccuracy = ClassifierMeasurements[ clFunc, testData, "Precision"];
baseAccuracy = baseAccuracy /@ targetClasses,
targetClasses === None,
baseAccuracy = ClassifierEnsembles`EnsembleClassifierMeasurements[ clFunc, testData, "Accuracy"],
targetClasses =!= None,
baseAccuracy = ClassifierEnsembles`EnsembleClassifierMeasurements[ clFunc, testData, "Precision", "ClassLabels"->targetClasses];
baseAccuracy = baseAccuracy /@ targetClasses;
];
(* Shuffle each column of the test set. *)
tmat = Transpose[tmat];
shuffledTestSets =
Map[Function[{i},
Thread[
Transpose[ReplacePart[tmat, i -> RandomSample[tmat[[i]]]]] -> testData[[All, 2]]]
], Range[Dimensions[tmat][[1]]]];
(* Calculate the classifier accuracy for each of the datasets *)
Which[
targetClasses === None && MatchQ[ clFunc, _ClassifierFunction ],
accuraciesOfShuffledTestSets =
ClassifierMeasurements[clFunc, #, "Accuracy"] & /@ shuffledTestSets,
targetClasses =!= None && MatchQ[ clFunc, _ClassifierFunction ],
accuraciesOfShuffledTestSets =
ClassifierMeasurements[clFunc, #, "Precision"] & /@ shuffledTestSets;
accuraciesOfShuffledTestSets = Map[ # /@ targetClasses&, accuraciesOfShuffledTestSets ],
targetClasses === None,
accuraciesOfShuffledTestSets =
ClassifierEnsembles`EnsembleClassifierMeasurements[clFunc, #, "Accuracy"] & /@ shuffledTestSets,
targetClasses =!= None,
accuraciesOfShuffledTestSets =
ClassifierEnsembles`EnsembleClassifierMeasurements[clFunc, #, "Precision", "ClassLabels"->targetClasses] & /@ shuffledTestSets;
accuraciesOfShuffledTestSets = Map[ # /@ targetClasses &, accuraciesOfShuffledTestSets];
];
(* Return result *)
PrependTo[ varNames, None ];
PrependTo[ accuraciesOfShuffledTestSets, baseAccuracy ];
AssociationThread[ varNames -> accuraciesOfShuffledTestSets ]
];
End[]; (* `Private` *)
EndPackage[]