Understand the fit/predict pattern and how to train machine learning models in bun-scikit
All models in bun-scikit follow a consistent API pattern inspired by scikit-learn. This page explains the core concepts of model training and inference.
Every model in bun-scikit implements a standard interface:
1
Create the model
Instantiate the model with hyperparameters:
const model = new LinearRegression({ fitIntercept: true });
2
Fit the model
Train the model on your data:
model.fit(XTrain, yTrain);
3
Make predictions
Use the trained model for inference:
const predictions = model.predict(XTest);
Models store learned parameters with a trailing underscore (e.g., coef_, intercept_) to distinguish them from hyperparameters set during initialization.
Give different importance to different samples during training.
import { LinearRegression } from "bun-scikit";const X = [[1], [2], [3], [4]];const y = [2, 4, 6, 8];// Give more weight to the first and last samplesconst sampleWeight = [2.0, 1.0, 1.0, 2.0];const model = new LinearRegression();model.fit(X, y, sampleWeight);
Convention: Parameters with trailing underscores are learned during fitting, while parameters without underscores are hyperparameters set during initialization.
import { LogisticRegression } from "bun-scikit";// Hyperparameters (set before training)const model = new LogisticRegression({ learningRate: 0.1, // Hyperparameter maxIter: 1000, // Hyperparameter tolerance: 1e-5, // Hyperparameter});model.fit(X, y);// Learned parameters (computed during training)console.log(model.coef_); // Learned coefficientsconsole.log(model.intercept_); // Learned interceptconsole.log(model.classes_); // Discovered classesconsole.log(model.fitBackend_); // "zig" (acceleration used)
bun-scikit uses Zig-accelerated native code for performance-critical operations.
import { LinearRegression } from "bun-scikit";const model = new LinearRegression();model.fit(X, y);console.log("Backend:", model.fitBackend_); // "zig"console.log("Library:", model.fitBackendLibrary_); // Path to .so/.dll
Most models automatically use Zig acceleration when available. The performance difference can be significant (2-6x speedup for linear models, up to 6x for random forests).
const model = new LinearRegression();try { model.predict(X); // Error!} catch (error) { console.error(error.message); // "LinearRegression has not been fitted."}