There is a private function that turns DecisionTree into Tree:
data=Table[x->Sin[x]+RandomVariate[NormalDistribution[0, .2]], {x, RandomReal[{-10, 10}, 400]}];
p=Predict[data,Method->"DecisionTree"];
MachineLearning`file23DecisionTree`PackagePrivate`toTree@p[[1]]["Model"]["Tree"]
Here is the one that does the reverse:
fromTree[tree_Tree, nominalDimension_Integer : Automatic] :=
Module[{leafPositions, nominalPositions, numericalPositions,
nodePositions, numericalFeatureIndices, nominalFeatureIndices,
nominalSplits, numericalThresholds, numericalOrdering,
nominallOrdering, ordering},
leafPositions = TreePosition[tree, _, {-1}];
nominalPositions = TreePosition[tree, _Equal, {0, -2}];
numericalPositions = TreePosition[tree, _GreaterEqual, {0, -2}];
nodePositions = Join[nominalPositions, numericalPositions];
nominalFeatureIndices =
TreeExtract[tree, nominalPositions,
TreeData/*Replace[Indexed[_, i_] == _ :> i]];
numericalFeatureIndices =
TreeExtract[tree, numericalPositions,
TreeData/*Replace[_ >= Indexed[_, i_] :> i]];
nominalSplits =
TreeExtract[tree, nominalPositions, TreeData/*Last/*(2^# &)];
numericalThresholds =
TreeExtract[tree, numericalPositions, TreeData/*First];
nominallOrdering =
Ordering@
Thread[{nominalFeatureIndices, nominalSplits, nominalPositions}];
numericalOrdering =
Ordering@
Thread[{numericalFeatureIndices, numericalThresholds,(*-Reverse/@*)
numericalPositions}];
ordering =
Join[nominallOrdering,
Length[nominallOrdering] + numericalOrdering];
MachineLearning`DecisionTree[<|
"FeatureIndices" ->
NumericArray[
Replace[Join[nominalFeatureIndices[[nominallOrdering]],
numericalFeatureIndices[[numericalOrdering]]], {} -> {-1}],
"Integer16"],
"NumericalThresholds" -> numericalThresholds[[numericalOrdering]],
"NominalSplits" -> nominalSplits[[nominallOrdering]],
"Children" ->
NumericArray[
Replace[{} -> {{-1}}]@
With[{positions = nodePositions[[ordering]]},
First@FirstPosition[
positions, #, -FirstPosition[leafPositions, #],
1] & /@ {Append[#, 1], Append[#, 2]} & /@ positions],
"Integer16"],
"LeafValues" ->
NumericArray[TreeExtract[tree, leafPositions, TreeData]],
"RootIndex" ->
First@FirstPosition[nodePositions[[ordering]], {}, {1}, 1],
"NominalDimension" ->
Replace[nominalDimension,
Automatic ->
Replace[Max[Max[nominalFeatureIndices],
Min[numericalFeatureIndices] - 1], Infinity -> 0]]|>]]