how can I modify the code to print the decision path with features names rather than just numbers.
import pandas as pd
import pyspark.sql.functions as F
from pyspark.ml import Pipeline, Transformer
from pyspark.sql import DataFrame
from pyspark.ml.classification import DecisionTreeClassifier
from pyspark.ml.feature import VectorAssembler
data = pd.DataFrame({
'ball': [0, 1, 2, 3],
'keep': [4, 5, 6, 7],
'hall': [8, 9, 10, 11],
'fall': [12, 13, 14, 15],
'mall': [16, 17, 18, 10],
'label': [21, 31, 41, 51]
})
df = spark.createDataFrame(data)
assembler = VectorAssembler(
inputCols=['ball', 'keep', 'hall', 'fall'], outputCol='features')
dtc = DecisionTreeClassifier(featuresCol='features', labelCol='label')
pipeline = Pipeline(stages=[assembler, dtc]).fit(df)
transformed_pipeline = pipeline.transform(df)
ml_pipeline = pipeline.stages[1]
print(ml_pipeline.toDebugString)
Output:
DecisionTreeClassificationModel (uid=DecisionTreeClassifier_48b3a34f6fb1f1338624) of depth 3 with 7 nodes If (feature 0 <= 0.5) Predict: 21.0 Else (feature 0 >
0.5) If (feature 0 <= 1.5)
Predict: 31.0 Else (feature 0 > 1.5)
If (feature 0 <= 2.5)
Predict: 41.0
Else (feature 0 > 2.5)
Predict: 51.0