Skip to content

Commit

Permalink
Added option for dicisision forest classification using votes or weig…
Browse files Browse the repository at this point in the history
…hts.
  • Loading branch information
antononcube committed Jul 21, 2013
1 parent 40e27a7 commit 5bd80f3
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions AVCDecisionTreeForest.m
Original file line number Diff line number Diff line change
Expand Up @@ -495,25 +495,34 @@ Mathematica is (C) Copyright 1988-2012 Wolfram Research, Inc.
];

(* Classify by forest *)

Clear[DecisionForestClassify]
DecisionForestClassify[forest_, record_] :=
Block[{res},
res = DecisionTreeClassify[#, record] & /@ forest;
Options[DecisionForestClassify] = {"Weighted" -> False};
DecisionForestClassify[forest_, record_, opts : OptionsPattern[]] :=
Block[{res, weightedQ = OptionValue[DecisionForestClassify, "Weighted"]},
res = TreeClassify[#, record] & /@ forest;
res = Flatten[res, 1];
res = GatherBy[res, #[[2]] &];
res = Map[{Total[#[[All, 1]]], #[[1, 2]]} &, res];
SortBy[res, -#[[1]] &]
If[TrueQ[weightedQ],
res = GatherBy[res, #[[2]] &];
res = Map[{Total[#[[All, 1]]], #[[1, 2]]} &, res];
SortBy[res, -#[[1]] &],
(*ELSE*)
SortBy[Reverse /@ Tally[res[[All, 2]]], -#[[1]] &]
]
];

Clear[ParallelDecisionForestClassify]
ParallelDecisionForestClassify[forest_, record_] :=
Block[{res},
res = ParallelMap[DecisionTreeClassify[#, record] &, forest, Method -> "CoarsestGrained", DistributedContexts -> Automatic];
Options[ParallelDecisionForestClassify] = {"Weighted" -> False};
ParallelDecisionForestClassify[forest_, record_] :=
Block[{res, weightedQ = OptionValue[ParallelDecisionForestClassify, "Weighted"]},
res = ParallelMap[TreeClassify[#, record] &, forest, Method -> "CoarsestGrained", DistributedContexts -> Automatic];
res = Flatten[res, 1];
res = GatherBy[res, #[[2]] &];
res = Map[{Total[#[[All, 1]]], #[[1, 2]]} &, res];
SortBy[res, -#[[1]] &]
If[TrueQ[weightedQ],
res = GatherBy[res, #[[2]] &];
res = Map[{Total[#[[All, 1]]], #[[1, 2]]} &, res];
SortBy[res, -#[[1]] &],
(*ELSE*)
SortBy[Reverse /@ Tally[res[[All, 2]]], -#[[1]] &]
]
];

(* Convert to rules *)
Expand Down

0 comments on commit 5bd80f3

Please sign in to comment.