@wlearn/core
v0.2.0
Published
Runtime core for wlearn: matrix helpers, bundle format, registry, pipeline
Maintainers
Readme
@wlearn/core
Runtime core for wlearn: matrix helpers, bundle format, model registry, pipeline, preprocessing, metrics, and cross-validation. No WASM. No heavy dependencies.
Part of wlearn (GitHub, all packages).
Install
npm install @wlearn/coreQuick start
const { Pipeline, load, accuracy, crossValScore } = require('@wlearn/core')
const { LinearModel } = require('@wlearn/liblinear')
// Build a pipeline
const model = await LinearModel.create({ task: 'classification' })
const pipe = new Pipeline([['clf', model]])
pipe.fit(X, y)
const preds = pipe.predict(X_test)
console.log('accuracy:', accuracy(y_test, preds))
// Save / load
const bytes = pipe.save()
const restored = await load(bytes)
pipe.dispose()API
Matrix utilities
Convert user input to typed arrays for WASM consumption.
normalizeX(X)--number[][] | DenseMatrixto contiguousDenseMatrixnormalizeY(y)--number[] | TypedArraytoFloat64ArraymakeDense(data, rows, cols)-- createDenseMatrixfrom typed arrayvalidateMatrix(m)-- validate matrix structure and dimensions
Bundle format
Portable binary format for model artifacts. Language-agnostic, deterministic.
encodeBundle(manifest, artifacts)-- encode toUint8ArraydecodeBundle(bytes)-- decode to{ manifest, toc, blobs }validateBundle(bytes)-- decode + verify SHA-256 hashes
Artifacts are { id, mediaType?, data: Uint8Array } objects. The manifest includes typeId, params, and metadata. See @wlearn/types for full shapes.
Registry
Global loader dispatcher. Model packages register themselves on import.
register(typeId, loaderFn)-- register a deserializerload(bytes)-- async: decode bundle, dispatch to registered loaderloadSync(bytes)-- sync variant (limited to sync loaders)getRegistry()-- inspect registered loaders
Pipeline
Sequential composition of transformers and estimators.
new Pipeline(steps)--stepsis[name, estimator][]pipe.fit(X, y)-- fit all steps in orderpipe.predict(X)-- transform + predictpipe.score(X, y)-- transform + scorepipe.save()/Pipeline.load(bytes)-- serialize/deserializepipe.dispose()-- dispose all steps
Preprocessing
StandardScaler-- zero mean, unit varianceMinMaxScaler-- scale to [0, 1]Preprocessor-- base transformer class
Metrics
Classification: accuracy, confusionMatrix, precisionScore, recallScore, f1Score, logLoss, rocAuc
Regression: r2Score, meanSquaredError, meanAbsoluteError
const { accuracy, f1Score, r2Score } = require('@wlearn/core')
accuracy(yTrue, yPred) // number
f1Score(yTrue, yPred, { average: 'macro' }) // number
r2Score(yTrue, yPred) // numberCross-validation
kFold(n, k?, opts?)-- k-fold split indicesstratifiedKFold(y, k?, opts?)-- stratified k-foldtrainTestSplit(n, opts?)-- single train/test splitcrossValScore(ModelClass, X, y, opts?)-- evaluate with CVgetScorer(name)-- get scoring function by name ('accuracy','r2','neg_mse')
createModelClass
Factory for building unified model classes from separate classifier/regressor implementations. Handles automatic task detection, async WASM pre-loading, and lifecycle management.
const { createModelClass } = require('@wlearn/core')
// Task-agnostic model (same class handles both tasks)
const XGBModel = createModelClass(XGBModelImpl, XGBModelImpl, {
name: 'XGBModel',
load: loadXGB // async WASM loader, called in create()
})
// Split model (separate classifier/regressor classes)
const MLPModel = createModelClass(MLPClassifier, MLPRegressor, {
name: 'MLPModel'
})The returned class supports:
Model.create(params)-- async factory. Passtask: 'classification'ortask: 'regression'to select explicitly, or omit to auto-detect fromyatfit()time.model.fit(X, y)-- trains the model. Auto-detects task from labels if not set.model.predict(X),model.predictProba(X),model.score(X, y)-- proxied to inner model.model.save()/Model.load(bytes)-- serialize/deserialize.model.dispose()-- free resources.model.task-- the detected or specified task.- Extra methods and getters from the inner classes are discovered and proxied automatically.
Auto-detection rules: if y is Int32Array, task is classification. Otherwise, if any value is non-integer, task is regression. If all values are integers and there are 20 or fewer unique values, task is classification; otherwise regression.
Errors
WlearnError, BundleError, RegistryError, ValidationError, NotFittedError, DisposedError
Utilities
sha256Sync(data)-- pure JS SHA-256makeLCG(seed?)-- deterministic LCG random number generatorshuffle(arr, rng)-- in-place shuffleisPromiseLike(x)/lift(x, fn)-- MaybePromise utilities
License
MIT
