DecisionTreeClassifier serialization/deserialization
See original GitHub issue- I’m submitting a …
- question about how to use this project
- Summary I am trying to serialize and deserialize DecisionTreeClassifier, however it throws an error. What is a valid way to save tree to file and restore it later?
Source:
const decision = new DecisionTreeClassifier({ featureLabels: ['a','b'] });
const deserialized = new DecisionTreeClassifier({ featureLabels: ['a','b'] });
const X = [[0, 0], [1, 1]];
const y = [0, 1];
decision.fit( X, y );
const serialized = JSON.stringify(decision.toJSON())
deserialized.fromJSON(JSON.parse(serialized))
Stacktrace:
TypeError: node.question.match is not a function
at DecisionTreeClassifier._predict (C:\apps\ml\src\lib\tree\tree.ts:434:23)
at DecisionTreeClassifier.predict (C:\apps\ml\src\lib\tree\tree.ts:202:24)
at Object.predict (C:\apps\ml/dttest.js:12:27)
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Machines · MLJ
Deserialize using SomeSerializationPkg to obtain a new object mach; Call restore!(mach) to ensure mach can be used to predict or transform new data....
Read more >python - Scikit-learn decision tree in production
I'm working at building a decision tree model that will be used in production. In documentation here pickle is used to serialize the...
Read more >serialize adaboost classifier scikit-learn - Stack Overflow
I'm trying to use scikit-learn AdaBoostClassifier, and i'm trying to serialize the output classifier using cPickle to save it to database or ...
Read more >Serialize and Deserialize a Binary Tree - GeeksforGeeks
Serialization is to store the tree in a file so that it can be later restored. The structure of the tree must be...
Read more >MLeap Model Export Demo (Python) - Databricks
Serialized pipelines (bundles) can be deserialized back into Apache Spark ... This notebook demonstrates how to use MLeap to export a DecisionTreeClassifier ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The problem is that JSON.stringify will convert the typed classes to regular, non-typed objects. Providing that string to a fromJSON will then only create objects with the same properties, but these objects won’t be the original classes with their appropriate methods. I suppose the fromJSON needs to be extended so that proper instances are created of the correct classes.
For the forest this could be something like this:
A single classifier could then be:
And the nodes:
Allthough the fromJSON should probably not return the result to be consistent with the existing fromJSON methods, but you get the idea.
I have the same problem. I tried to handle using the above example, but some classes are not exported.
import { DecisionTreeClassifier } from ‘./tree’; export { DecisionTreeClassifier };
then i’m fork and fix it import { DecisionTreeClassifier, DecisionNode, Question, Leaf } from ‘./tree’; export { DecisionTreeClassifier, DecisionNode, Question, Leaf };