Skip to content

Commit

Permalink
Extended ROCPlot to take a list or association of ROCs. Added comments.
Browse files Browse the repository at this point in the history
Small bug fix.
  • Loading branch information
antononcube committed Apr 10, 2018
1 parent 5d9567e commit 175fc33
Showing 1 changed file with 54 additions and 11 deletions.
65 changes: 54 additions & 11 deletions ROCFunctions.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,22 @@ This package provides Receiver Operating Characteristic (ROC) functions calculat

(*
TODO:
# Update March-April 2018
I added the function ROCValues that corresponds in spirit to the function ROCValues in the file:
https://github.com/antononcube/MathematicaForPrediction/blob/master/R/VariableImportanceByClassifiers.R .
In R because we can get from most classifiers matrices with named columns that correspond to the class labels
and entries that correspond to the probabilities for those class labels. (The rows correspond to the test records.)
In Mathematica the build-in classifiers can return lists of Association objects (using the "Probabilities" property.)
These lists can easily be turned into Dataset objects that have named columns.
Hence ROCValues below is based on Dataset objects.
I also added the Association key "ROCParameter" to the ROC Associations objects. This makes the use ROCPlot
easier in many cases.
*)

Expand Down Expand Up @@ -299,17 +314,43 @@ computes ROC associations (for ROCPlot)."

Options[ROCPlot] =
Join[ {"ROCPointSize"-> 0.02, "ROCColor"-> Lighter[Blue], "ROCPointColorFunction" -> Automatic,
"ROCPointTooltips"->True, "ROCPointCallouts"->True, "PlotJoined" -> False }, Options[Graphics]];
"ROCPointTooltips"->True, "ROCPointCallouts"->True, "ROCCurveColorFunction" -> Automatic,
"PlotJoined" -> False }, Options[Graphics]];

ROCSpecQ[arg_] :=
MatchQ[ arg, {_?ROCAssociationQ..} | {{_?ROCAssociationQ..}..} | Association[ (_->{_?ROCAssociationQ..})..] ];

ROCPlot[ aROCs:{_?ROCAssociationQ..}, opts:OptionsPattern[]] :=
ROCPlot[ aROCs_?ROCSpecQ, opts:OptionsPattern[]] :=
ROCPlot[ "FPR", "TPR", Automatic, aROCs, opts];

ROCPlot[ parVals:{_?NumericQ..}, aROCs:{_?ROCAssociationQ..}, opts:OptionsPattern[]] :=
ROCPlot[ parVals:{_?NumericQ..}, aROCs_?ROCSpecQ, opts:OptionsPattern[]] :=
ROCPlot[ "FPR", "TPR", parVals, aROCs, opts];

ROCPlot[ xFuncName_String, yFuncName_String, aROCs:{_?ROCAssociationQ..}, opts:OptionsPattern[]] :=
ROCPlot[ xFuncName_String, yFuncName_String, aROCs_?ROCSpecQ, opts:OptionsPattern[]] :=
ROCPlot[ xFuncName, yFuncName, Automatic, aROCs, opts];

ROCPlot[
xFuncName_String, yFuncName_String,
parValsArg : (Automatic | {_?NumericQ..}),
aROCs : {{_?ROCAssociationQ..}..}, opts : OptionsPattern[]] :=
ROCPlot[ xFuncName, yFuncName, parValsArg, AssociationThread[ Range[Length[aROCs]], aROCs], opts ];

ROCPlot[
xFuncName_String, yFuncName_String,
parValsArg : (Automatic | {_?NumericQ..}),
aROCs : Association[ (_->{_?ROCAssociationQ..}) .. ], opts : OptionsPattern[]] :=
Block[{rocCurveColorFunc, cls, grs},

rocCurveColorFunc = OptionValue[ROCPlot, "ROCCurveColorFunction"];
If[ TrueQ[rocCurveColorFunc === Automatic],
rocCurveColorFunc = ColorData["DarkBands", "ColorFunction"];
];

cls = rocCurveColorFunc /@ Rescale[Range[Length[aROCs]]];
grs = MapThread[ ROCPlot[#2, "PlotJoined" -> True, "ROCColor" -> #3, opts] &, {Keys[aROCs], Values[aROCs], cls}];
Legended[Show[grs], SwatchLegend[cls, Keys[aROCs]]]
];

ROCPlot[
xFuncName_String, yFuncName_String,
parValsArg : (Automatic | {_?NumericQ..}),
Expand Down Expand Up @@ -374,7 +415,7 @@ computes ROC associations (for ROCPlot)."

DeleteCases[{opts},
( "ROCPointSize" | "ROCColor" | "ROCPointColorFunction" |
"ROCPointTooltips" | "ROCPointCallouts" | "PlotJoined") -> _ ]
"ROCPointTooltips" | "ROCPointCallouts" | "ROCCurveColorFunction" | "PlotJoined") -> _ ]
]
] /; Length[parValsArg] == Length[aROCs] || TrueQ[parValsArg===Automatic];

Expand Down Expand Up @@ -409,11 +450,13 @@ computes ROC associations (for ROCPlot)."

Table[
predictedLabels =
Normal@predictionProbabilities[All,
If[#[[1]] >= th, Keys[#][[1]], Keys[#][[2]]] &];
rocRes =
ToROCAssociation[classLabels, actualLabels, predictedLabels];
Join[<|"ROCParameter" -> th|>, rocRes]
Normal @ predictionProbabilities[All, If[#[[1]] >= th, Keys[#][[1]], Keys[#][[2]]] &];

rocRes = ToROCAssociation[classLabels, actualLabels, predictedLabels];
If[ AssociationQ[rocRes],
Join[<|"ROCParameter" -> th|>, rocRes],
$Failed
]
, {th, thRange}]
];

Expand Down

0 comments on commit 175fc33

Please sign in to comment.