Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in DecisionTreeClassifier.predict: this.root.classify(...).maxRowIndex is not a function after deserializing RandomForestClassifier #46

Open
MadsHL opened this issue Sep 30, 2024 · 0 comments

Comments

@MadsHL
Copy link

MadsHL commented Sep 30, 2024

Description:

I am encountering a TypeError when trying to use a deserialized RandomForestClassifier. The error occurs when calling predictProbability on the rehydrated classifier, specifically in the DecisionTreeClassifier:

DecisionTreeClassifier.js:58 Uncaught (in promise) TypeError: this.root.classify(...).maxRowIndex is not a function
    at _DecisionTreeClassifier.predict (DecisionTreeClassifier.js:58:10)
    at _RandomForestClassifier.predictionValues (RandomForestBase.js:288:48)
    at _RandomForestClassifier.predictProbability (RandomForestClassifier.js:111:35)

Steps to Reproduce:

  1. Serialize a RandomForestClassifier using toJSON():

    const randomForestClassifier = new RandomForestClassifier(/* options */);
    const classifierJson = randomForestClassifier.toJSON();
  2. Deserialize the RandomForestClassifier using load():

    const classifier = new RandomForestClassifier.load({
        isClassifier: classifierModel.baseModel.isClassifier,
        noOOB: classifierModel.baseModel.noOOB,
        selectionMethod: classifierModel.baseModel.selectionMethod,
        useSampleBagging: classifierModel.baseModel.useSampleBagging,
        maxFeatures: classifierModel.baseModel.maxFeatures,
        nEstimators: classifierModel.baseModel.estimators.length,
        seed: classifierModel.baseModel.seed,
        replacement: classifierModel.baseModel.replacement,
        treeOptions: classifierModel.baseModel.treeOptions,
    }, classifierJson as RandomForestClassifierModel);
    
    classifier.indexes = classifierModel.baseModel.indexes;
    classifier.n = classifierModel.baseModel.n;
    classifier.estimators = classifierModel.baseModel.estimators;
  3. Call predictProbability() on the deserialized RandomForestClassifier:

    const predictions = classifier.predictProbability(testData, 1);

Expected Behavior:

The deserialized RandomForestClassifier should properly function, and the estimators (decision trees) should be rehydrated as instances of DecisionTreeClassifier, allowing predict() to work correctly.

Actual Behavior:

The estimators array contains objects but they do not have the predict() method, and calling predictProbability throws the following error:

TypeError: this.root.classify(...).maxRowIndex is not a function

Cause:

It seems that during deserialization, the estimators (which are instances of DecisionTreeClassifier) are being restored as plain JSON objects, losing their methods such as predict() and classify(). This leads to the failure when the classifier tries to call methods on these objects.

Possible Fix:

To fix this, each estimator in the estimators array needs to be rehydrated as an instance of DecisionTreeClassifier after deserialization. For example:

classifier.estimators = classifierModel.baseModel.estimators.map(estimator => {
    return DecisionTreeClassifier.load(estimator);  // Rehydrate each estimator
});

Environment:

  • Package: ml-random-forest
  • Version: 2.1.0

Additional Context:

The deserialization works fine up to the point where it reaches the estimators. The issue arises when trying to call methods on these deserialized objects, which are missing their expected methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant