[Python] Printing rules from decision tree in River ML

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    I’m using the River ML library and working with decision tree models. I’d like to print or extract the decision rules from the trained trees. However, it seems that the library doesn’t currently offer a built-in method to do this.

    Does anyone know a way to print or extract the decision rules manually from a tree model in River?

    pipeline = compose.Pipeline(
    self.extract_features(event=event),
    OrdinalEncoder(),
    LASTClassifier(max_depth=5, track_error=True, remove_poor_attrs=True)
    )
    metric = F1()
    self.state.update({'pipeline': pipeline, 'metric': metric})


    label = 1 if classification_label else 0

    # Retrieve current pipeline components
    pipeline = self.state.value()['pipeline']
    model = pipeline['LASTClassifier']
    metric = self.state.value()['metric']

    # Make a prediction and update metric
    predicted_label = model.predict_one(self.state.value()['pipeline']['dict'])
    metric.update(y_true=label, y_pred=predicted_label)

    # Update the model with the new event
    model.learn_one(self.state.value()['pipeline']['dict'], label)

    # Save updated state
    self.state.update({'pipeline': pipeline, 'metric': metric})

    Continue reading...

Compartilhe esta Página