This Node.js module implements a Decision Tree using the ID3 Algorithm
Requires Node.js 20 or higher (ES modules support required)
npm install decision-tree
This module is written in TypeScript and provides full type definitions. The compiled JavaScript maintains full backward compatibility with existing Node.js and browser projects that support ES modules.
Note: This package uses ES modules ("type": "module"
), so CommonJS require()
is not supported.
import DecisionTree from 'decision-tree';
// Full type safety for training data
interface TrainingData {
color: string;
shape: string;
liked: boolean;
}
const training_data: TrainingData[] = [
{"color":"blue", "shape":"square", "liked":false},
{"color":"red", "shape":"square", "liked":false},
{"color":"blue", "shape":"circle", "liked":true},
{"color":"red", "shape":"circle", "liked":true}
];
const dt = new DecisionTree('liked', ['color', 'shape']);
dt.train(training_data);
// Type-safe prediction
const prediction = dt.predict({ color: "blue", shape: "hexagon" });
import DecisionTree from 'decision-tree';
Important: This package uses ES modules only. CommonJS require()
is not supported.
const training_data = [
{"color":"blue", "shape":"square", "liked":false},
{"color":"red", "shape":"square", "liked":false},
{"color":"blue", "shape":"circle", "liked":true},
{"color":"red", "shape":"circle", "liked":true},
{"color":"blue", "shape":"hexagon", "liked":false},
{"color":"red", "shape":"hexagon", "liked":false},
{"color":"yellow", "shape":"hexagon", "liked":true},
{"color":"yellow", "shape":"circle", "liked":true}
];
const test_data = [
{"color":"blue", "shape":"hexagon", "liked":false},
{"color":"red", "shape":"hexagon", "liked":false},
{"color":"yellow", "shape":"hexagon", "liked":true},
{"color":"yellow", "shape":"circle", "liked":true}
];
const class_name = "liked";
const features = ["color", "shape"];
Method 1: Separate instantiation and training
const dt = new DecisionTree(class_name, features);
dt.train(training_data);
Method 2: Instantiate and train in one step
const dt = new DecisionTree(training_data, class_name, features);
Note: Method 2 returns a new instance rather than modifying the current one. This is equivalent to:
const dt = new DecisionTree(class_name, features);
dt.train(training_data);
const predicted_class = dt.predict({
color: "blue",
shape: "hexagon"
});
const accuracy = dt.evaluate(test_data);
const treeJson = dt.toJSON();
Note: The exported model contains the tree structure but does not preserve the original training data. Only imported models have training data stored.
const treeJson = dt.toJSON();
const preTrainedDecisionTree = new DecisionTree(treeJson);
const treeJson = dt.toJSON();
dt.import(treeJson);
Important: This implementation is intentionally permissive and has limited validation:
- Feature names: Only validates that features is an array, not element types
- Target column: Does not validate that the target column exists in training data
- Empty datasets: Allows empty training datasets (may result in unexpected behavior)
- Data types: Accepts mixed data types without validation
For production use, ensure your data meets these requirements:
- Training data must be an array of objects
- Each object should contain the target column
- Feature values should be consistent across samples
The package handles many edge cases gracefully but may fail silently in some scenarios:
// This will work but may not produce expected results
const dt = new DecisionTree('nonexistent', ['feature1']);
dt.train([{ feature1: 'value1' }]); // Missing target column
// This will work but may not produce expected results
const dt2 = new DecisionTree('target', ['feature1']);
dt2.train([]); // Empty dataset
This project is written in TypeScript. To build from source:
# Install dependencies
npm install
# Build the project
npm run build
# Run tests
npm test
# Watch mode for development
npm run build:watch
src/
- TypeScript source fileslib/
- Compiled JavaScript output (generated)tst/
- TypeScript test filesdata/
- Sample datasets for testing
When contributing, please:
- Make changes in the
src/
directory (TypeScript source) - Update tests in the
tst/
directory (TypeScript tests) - Run
npm run build
to compile - Ensure all tests pass with
npm test
- The compiled JavaScript in
lib/
will be automatically generated
This package requires Node.js 20 or higher because:
- ES Modules: Uses native ES module support (
"type": "module"
) - Modern Features: Leverages ES2022 features for better performance
- Import Assertions: Uses modern import syntax for better compatibility
- Performance: Takes advantage of Node.js 20+ optimizations