Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a custom-defined decision tree classifier in Scikit-learn?

I have a predefined decision tree, which I built from knowledge-based splits, that I want to use to make predictions. I could try to implement a decision tree classifier from scratch, but then I would not be able to use build in Scikit functions like predict. Is there a way to convert my tree in pmml and import this pmml to make my prediction with scikit-learn? Or do I need to do something completely different? My first attempt was to use “fake training data” to force the algorithm to build the tree the way I like it, this would end up in a lot of work because I need to create different trees depending on the user input.

like image 509
eva Avatar asked Nov 01 '25 09:11

eva


1 Answers

You can create your own decision tree classifier using Sklearn API. Please read this documentation following the predictor class types. As explained in this section, you can build an estimator following the template:

import numpy as np
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
from sklearn.utils.multiclass import unique_labels
from sklearn.metrics import euclidean_distances

class TemplateClassifier(BaseEstimator, ClassifierMixin):
    def __init__(self, demo_param='demo'):
        self.demo_param = demo_param

    def fit(self, X, y):
        # Two paths. Just return the object, or implement here your decision rules
        return self

    def predict(self, X):

        # Check is fit had been called
        check_is_fitted(self)

        # Input validation
        X = check_array(X)

        # Change this to your decision tree "rules"
        closest = np.argmin(euclidean_distances(X, self.X_), axis=1)
        return self.y_[closest]
like image 177
Noki Avatar answered Nov 04 '25 03:11

Noki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!