Documentation Index Fetch the complete documentation index at: https://mintlify.com/Seyamalam/bun-scikit/llms.txt
Use this file to discover all available pages before exploring further.
Get up and running with bun-scikit by training your first models. This guide covers regression, classification, and decision trees with native Zig acceleration.
Linear regression
Import libraries
Import the essential components for regression. import {
LinearRegression ,
StandardScaler ,
trainTestSplit ,
meanSquaredError
} from "bun-scikit" ;
Prepare your data
Create sample data and split it for training and testing. const X = [[ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ]];
const y = [ 3 , 5 , 7 , 9 , 11 , 13 ];
const scaler = new StandardScaler ();
const XScaled = scaler . fitTransform ( X );
const { XTrain , XTest , yTrain , yTest } = trainTestSplit ( XScaled , y , {
testSize: 0.33 ,
randomState: 42
});
Train and evaluate
Fit the model and make predictions. const model = new LinearRegression ({ solver: "normal" });
model . fit ( XTrain , yTrain );
const predictions = model . predict ( XTest );
const mse = meanSquaredError ( yTest , predictions );
console . log ( "MSE:" , mse );
console . log ( "Coefficients:" , model . coef_ );
Classification
Import libraries
import {
LogisticRegression ,
StandardScaler ,
accuracyScore
} from "bun-scikit" ;
Prepare classification data
const X = [[ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ]];
const y = [ 0 , 0 , 0 , 1 , 1 , 1 ];
const scaler = new StandardScaler ();
const XScaled = scaler . fitTransform ( X );
Train classifier
const classifier = new LogisticRegression ({
solver: "gd" ,
learningRate: 0.8 ,
maxIter: 100 ,
tolerance: 1e-5
});
classifier . fit ( XScaled , y );
const predictions = classifier . predict ( XScaled );
const accuracy = accuracyScore ( y , predictions );
console . log ( "Accuracy:" , accuracy );
console . log ( "Predicted probabilities:" , classifier . predictProba ( XScaled ));
Decision trees with Zig acceleration
Train a decision tree using native Zig backend for maximum performance.
import { DecisionTreeClassifier } from "bun-scikit" ;
const X = [
[ 0 , 0 ],
[ 0 , 1 ],
[ 1 , 0 ],
[ 2 , 2 ],
[ 2 , 3 ],
[ 3 , 2 ]
];
const y = [ 0 , 0 , 0 , 1 , 1 , 1 ];
const tree = new DecisionTreeClassifier ({
maxDepth: 3 ,
randomState: 42
});
tree . fit ( X , y );
// Check backend (should be "zig" on Linux/Windows)
console . log ( "Fit backend:" , tree . fitBackend_ );
console . log ( "Backend library:" , tree . fitBackendLibrary_ );
// Make predictions
const predictions = tree . predict ([[ 1 , 1 ], [ 3 , 3 ]]);
console . log ( "Predictions:" , predictions ); // [0, 1]
The Zig backend provides 2-6x faster training compared to scikit-learn on Linux and Windows. See Performance Benchmarks for detailed comparisons.
Random forest ensemble
Random forests combine multiple decision trees for improved accuracy.
import { RandomForestClassifier } from "bun-scikit" ;
const forest = new RandomForestClassifier ({
nEstimators: 25 ,
maxDepth: 4 ,
randomState: 42
});
forest . fit ( X , y );
console . log ( "Fit backend:" , forest . fitBackend_ );
const predictions = forest . predict ([[ 1 , 1 ], [ 3 , 3 ]]);
console . log ( "Forest predictions:" , predictions );
Complete example
Here’s a full workflow combining preprocessing, training, and evaluation:
import {
LinearRegression ,
LogisticRegression ,
StandardScaler ,
trainTestSplit ,
meanSquaredError ,
accuracyScore
} from "bun-scikit" ;
// Regression workflow
const XReg = [[ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ]];
const yReg = [ 3 , 5 , 7 , 9 , 11 , 13 ];
const scaler = new StandardScaler ();
const XScaled = scaler . fitTransform ( XReg );
const { XTrain , XTest , yTrain , yTest } = trainTestSplit ( XScaled , yReg , {
testSize: 0.33 ,
randomState: 42
});
const regressor = new LinearRegression ({ solver: "normal" });
regressor . fit ( XTrain , yTrain );
console . log ( "Regression MSE:" , meanSquaredError ( yTest , regressor . predict ( XTest )));
// Classification workflow
const yCls = [ 0 , 0 , 0 , 1 , 1 , 1 ];
const classifier = new LogisticRegression ({
solver: "gd" ,
learningRate: 0.8 ,
maxIter: 100 ,
tolerance: 1e-5
});
classifier . fit ( XScaled , yCls );
console . log ( "Classification accuracy:" , accuracyScore ( yCls , classifier . predict ( XScaled )));
Run your code
Save your code to train.ts and run:
Next steps
Core Concepts Learn about preprocessing, pipelines, and model evaluation
Guides Explore in-depth guides for different algorithms
API Reference Browse the complete API documentation
Performance See benchmark comparisons vs scikit-learn