ml-toolkit-ts
v1.0.3
Published
A TypeScript toolkit for machine learning inference and operations.
Readme
ml-toolkit-ts
A TypeScript toolkit for machine learning inference and operations.
Run your XGBoost models trained with ElectronML directly in TypeScript/JavaScript!
Installation
Install everything:
npm install ml-toolkit-tsOr install specific packages:
npm install @ml-toolkit-ts/xgboost
npm install @ml-toolkit-ts/preprocessingUsage
1. Load your inference package
Use the JSON file you created with ElectronML.
// Type definition for inference package
interface InferencePackage {
model: any;
preprocessing_metadata: any;
feature_names: string[];
class_mapping: Record<number, string>;
isRegression: boolean;
}
// Browser: using async/await
async function loadModel() {
const inferencePackage = await fetch('model/inference_package.json')
.then(response => response.json());
// Use inferencePackage...
}
// Node.js
import * as fs from 'fs';
const inferencePackage = JSON.parse(
fs.readFileSync('model/inference_package.json', 'utf-8')
);2. Initialize predictor and preprocessor
// Using the complete package
import { XGBoostPredictor, DataPreprocessor } from 'ml-toolkit-ts';
// Or import specific packages
import { XGBoostPredictor } from '@ml-toolkit-ts/xgboost';
import { DataPreprocessor } from '@ml-toolkit-ts/preprocessing';
const predictor = new XGBoostPredictor(JSON.stringify(inferencePackage.model));
const preprocessor = new DataPreprocessor(JSON.stringify(inferencePackage.preprocessing_metadata));3. Make predictions
For classification:
const inputValues = {
age: "25",
income: "50000",
category: "A"
};
const transformedFeatures = preprocessor.transform(inputValues);
const predictedClass = predictor.predict(transformedFeatures);
const probabilities = predictor.predict_proba(transformedFeatures);
console.log('Predicted class:', inferencePackage.class_mapping[predictedClass]);
console.log('Probabilities:', probabilities);
// Output:
// Predicted class: high_risk
// Probabilities: [0.15, 0.85] // 15% low_risk, 85% high_riskFor regression:
const inputValues = {
sqft: "1500",
bedrooms: "3",
location: "urban"
};
const transformedFeatures = preprocessor.transform(inputValues);
const prediction = predictor.predict(transformedFeatures);
console.log('Predicted price:', prediction);
// Output:
// Predicted price: 450000