New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

micro-ml

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

micro-ml

8 ML algorithms + statistics in ~56KB gzipped. Rust/WASM, zero dependencies, browser + Node.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

micro-ml

npm version npm downloads bundle size license GitHub stars

Live Docs & Demos

You don't need TensorFlow.js for a trendline.

Most apps just need simple predictions: forecast next month's sales, add a trendline to a chart, smooth noisy sensor data. You don't need a 500KB neural network library for that.

micro-ml is ~56KB gzipped — 8 ML algorithms + regression + smoothing + forecasting. All in WASM. Sub-millisecond on typical datasets.

npm install micro-ml

What Can You Do With It?

Predict Future Values

Got historical data? Predict what comes next.

// You have: sales data for 12 months
const sales = [10, 12, 15, 18, 22, 25, 28, 32, 35, 40, 45, 50];

// You want: forecast for next 3 months
const forecast = await trendForecast(sales, 3);
console.log(forecast.getForecast()); // [55, 60, 65]
console.log(forecast.direction);      // "up"

Is your data going up, down, or flat? How strong is the trend?

const model = await linearRegressionSimple(sales);
console.log(model.slope);     // 3.7 (growing by ~3.7 per month)
console.log(model.rSquared);  // 0.98 (98% confidence - strong trend)

Smooth Noisy Data

Sensor readings jumping around? Stock prices too volatile? Smooth them out.

// Raw sensor data (noisy)
const readings = [22.1, 25.3, 21.8, 24.9, 23.2, 26.1, 22.5, ...];

// Smoothed (removes noise, shows real trend)
const smooth = await ema(readings, 5);

Fit Curves to Data

Data doesn't follow a straight line? Fit a curve instead.

// Exponential growth (bacteria, viral spread, compound interest)
const expModel = await exponentialRegression(time, population);
console.log(expModel.doublingTime()); // "Population doubles every 3.2 days"

// Polynomial curve (projectile motion, diminishing returns)
const polyModel = await polynomialRegression(x, y, { degree: 2 });

Classify Data

Got labelled data? Train a classifier.

import { knnClassifier, logisticRegression } from 'micro-ml';

// kNN — simple, no training
const knn = await knnClassifier(trainingData, labels, { k: 5 });
knn.predict([[1.5, 2.0]]); // [0] or [1]

// Logistic Regression — fast, probabilistic
const lr = await logisticRegression(data, labels, { maxIterations: 200 });
lr.predictProba([[1.5, 2.0]]); // [0.87]

Cluster Data

Find natural groups without labels.

import { kmeans, dbscan } from 'micro-ml';

// k-Means — you know how many clusters
const km = await kmeans(points, { k: 3 });
km.getCentroids();    // [[x,y], [x,y], [x,y]]
km.getAssignments();  // [0, 1, 2, 0, 1, ...]

// DBSCAN — discovers clusters + noise automatically
const db = await dbscan(points, { eps: 0.5, minPoints: 4 });
db.nClusters; // 3
db.nNoise;    // 12

Reduce Dimensions

Visualise high-dimensional data in 2D.

import { pca } from 'micro-ml';

// 50-dimensional data → 2 components
const result = await pca(data, { nComponents: 2 });
result.getExplainedVarianceRatio(); // [0.85, 0.10] (95% variance kept)
result.getTransformed();            // [[x,y], [x,y], ...]

When to Use Which Function?

Your Data Looks LikeUse ThisExample
Straight line trendlinearRegressionStock price over time
Curved linepolynomialRegressionBall trajectory, learning curves
Exponential growthexponentialRegressionBacteria growth, viral spread
Logarithmic (fast then slow)logarithmicRegressionLearning a skill, diminishing returns
Noisy/jumpy dataema or smaSensor readings, stock prices
Need future predictionstrendForecastSales forecast, weight loss goal
Find peaks/valleysfindPeaks / findTroughsDetect anomalies, buy/sell signals
Group similar itemskmeansCustomer segments, image colours
Classify new itemsknnClassifierSpam detection, image recognition
Binary yes/nologisticRegressionChurn prediction, fraud detection
Find clusters + outliersdbscanAnomaly detection, geo clustering
Decision rulesdecisionTreeLoan approval, feature importance
Reduce dimensionspcaVisualisation, feature extraction
Seasonal patternsdetectSeasonalityMonthly sales cycles, weekly patterns

Real-World Use Cases

1. Sales Forecasting

Problem: "How much will we sell next quarter?"

import { trendForecast, linearRegressionSimple } from 'micro-ml';

const monthlySales = [42000, 45000, 48000, 52000, 55000, 58000];

// Analyze trend
const model = await linearRegressionSimple(monthlySales);
console.log(`Growing by $${model.slope.toFixed(0)}/month`);

// Forecast next 3 months
const forecast = await trendForecast(monthlySales, 3);
console.log('Next 3 months:', forecast.getForecast());
// → [61000, 64000, 67000]

2. Stock/Crypto Trendlines

Problem: "Is this stock trending up or down? Add a trendline to my chart."

import { linearRegression, ema } from 'micro-ml';

const days = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const prices = [150, 152, 149, 155, 158, 156, 160, 163, 161, 165];

// Fit trendline
const trend = await linearRegression(days, prices);
const trendlinePoints = trend.predict(days);
// → Draw this as a line on your chart

// Add moving average (smoothed price)
const smoothPrices = await ema(prices, 3);
// → Draw this as another line

3. Weight Loss Prediction

Problem: "When will I reach my goal weight?"

import { linearRegressionSimple } from 'micro-ml';

const weeklyWeights = [200, 198, 196.5, 195, 193, 191.5]; // lbs
const goalWeight = 175;

const model = await linearRegressionSimple(weeklyWeights);
const lossPerWeek = Math.abs(model.slope); // 1.5 lbs/week

const currentWeight = weeklyWeights[weeklyWeights.length - 1];
const weeksToGoal = (currentWeight - goalWeight) / lossPerWeek;

console.log(`Losing ${lossPerWeek.toFixed(1)} lbs/week`);
console.log(`Goal in ${Math.ceil(weeksToGoal)} weeks`);
// → "Losing 1.5 lbs/week, Goal in 11 weeks"

4. IoT Sensor Smoothing

Problem: "Temperature sensor is noisy, I want a stable reading."

import { ema, exponentialSmoothing } from 'micro-ml';

// Raw readings jump around: 22.1, 25.3, 21.8, 24.9, ...
const rawTemperature = getSensorReadings();

// Smoothed readings: 22.5, 23.1, 22.8, 23.2, ...
const smoothed = await ema(rawTemperature, 5);

// Display the last smoothed value
displayTemperature(smoothed[smoothed.length - 1]);

5. Growth Rate Analysis

Problem: "How fast is our user base growing? When will we hit 1 million?"

import { exponentialRegression } from 'micro-ml';

const months = [1, 2, 3, 4, 5, 6];
const users = [1000, 1500, 2200, 3300, 5000, 7500];

const model = await exponentialRegression(months, users);

console.log(`Doubling every ${model.doublingTime().toFixed(1)} months`);
// → "Doubling every 1.4 months"

// When will we hit 1 million?
// Solve: 1000000 = a * e^(b*t)
const monthsToMillion = Math.log(1000000 / model.a) / model.b;
console.log(`1M users in ${monthsToMillion.toFixed(0)} months`);

6. Detecting Anomalies

Problem: "Alert me when sensor readings spike."

import { findPeaks, ema } from 'micro-ml';

const readings = [...sensorData];

// Find all spike indices
const spikes = await findPeaks(readings);

// Alert if recent spike
if (spikes.includes(readings.length - 1)) {
  alert('Anomaly detected!');
}

Installation

npm install micro-ml

Quick Start

import { linearRegression, trendForecast, ema } from 'micro-ml';

// Fit a line to data
const model = await linearRegression([1,2,3,4,5], [2,4,6,8,10]);
console.log(model.slope);        // 2
console.log(model.predict([6])); // [12]

// Forecast future values
const forecast = await trendForecast([10,20,30,40,50], 3);
console.log(forecast.getForecast()); // [60, 70, 80]

// Smooth noisy data
const smooth = await ema([10,15,12,18,14,20], 3);

Browser Usage

<script type="module">
  import { linearRegression } from 'https://esm.sh/micro-ml';

  const model = await linearRegression([1,2,3], [2,4,6]);
  console.log(model.slope); // 2
</script>

API Reference

Regression (Find Patterns)

FunctionWhat It DoesWhen to Use
linearRegression(x, y)Fits straight line: y = mx + bSteady growth/decline
linearRegressionSimple(y)Same but x = [0,1,2,...]Time series data
polynomialRegression(x, y, {degree})Fits curveCurved patterns
exponentialRegression(x, y)Fits y = a × e^(bx)Growth/decay
logarithmicRegression(x, y)Fits y = a + b × ln(x)Diminishing returns
powerRegression(x, y)Fits y = a × x^bPower laws

Smoothing (Remove Noise)

FunctionWhat It DoesWhen to Use
sma(data, window)Simple Moving AverageGeneral smoothing
ema(data, window)Exponential Moving AverageRecent values matter more
wma(data, window)Weighted Moving AverageBalance of both
exponentialSmoothing(data, {alpha})Single exponential smoothQuick smoothing

Forecasting (Predict Future)

FunctionWhat It DoesWhen to Use
trendForecast(data, periods)Analyze trend + predictFuture predictions
predict(xTrain, yTrain, xNew)One-liner predictQuick predictions
trendLine(data, periods)Get model + predictionsWhen you need both

Analysis (Understand Data)

FunctionWhat It DoesWhen to Use
findPeaks(data)Find local maximaDetect spikes
findTroughs(data)Find local minimaDetect dips
rateOfChange(data, periods)% change from n agoGrowth rate
momentum(data, periods)Difference from n agoTrend strength

Classification (Label Data)

FunctionWhat It DoesWhen to Use
knnClassifier(data, labels, {k})k-Nearest NeighboursSimple classification
logisticRegression(data, labels, opts)Logistic regressionBinary classification
naiveBayes(data, labels)Gaussian Naive BayesText/feature classification
decisionTree(data, labels, {maxDepth})CART decision treeInterpretable rules
perceptron(data, labels, opts)Single-layer perceptronLinear separability

Clustering (Find Groups)

FunctionWhat It DoesWhen to Use
kmeans(data, {k})k-Means clusteringKnown number of groups
dbscan(data, {eps, minPoints})Density-based clusteringUnknown clusters + noise

Dimensionality Reduction

FunctionWhat It DoesWhen to Use
pca(data, {nComponents})Principal Component AnalysisReduce dimensions, visualise

Seasonality

FunctionWhat It DoesWhen to Use
seasonalDecompose(data, period)Decompose trend + seasonal + residualUnderstand patterns
autocorrelation(data, maxLag)Autocorrelation functionFind repeating patterns
detectSeasonality(data)Auto-detect period + strengthUnknown periodicity

Model Properties

All regression models return:

model.rSquared   // 0-1, how well the model fits (1 = perfect)
model.n          // Number of data points used
model.predict(x) // Predict y values for new x values
model.toString() // Human-readable equation

Linear models also have:

model.slope      // Rate of change
model.intercept  // Y-intercept

Exponential models also have:

model.a          // Initial value
model.b          // Growth rate
model.doublingTime() // Time to double

Performance

Benchmarked in Node.js (median of 3 runs):

AlgorithmData SizeTime
Linear Regression100,000 pts0.9ms
k-Means10,000 pts, k=52ms
kNN5,000 train, predict 203.4ms
Logistic Regression5,000 pts11ms
DBSCAN2,000 pts12ms
Naive Bayes10,000 pts0.2ms
PCA5,000 × 50 → 213ms
Perceptron10,000 pts0.1ms
Seasonal Decompose1,000 pts0.02ms
SMA/EMA/WMA100,000 pts3ms

For very large datasets, use Web Workers:

import { createWorker } from 'micro-ml/worker';

const ml = createWorker();
const model = await ml.linearRegression(hugeX, hugeY); // Non-blocking
ml.terminate();

Comparison

LibrarySize (gzip)Speed
micro-ml~56KBFastest (WASM)
TensorFlow.js500KB+Slow
ml.js150KBMedium
simple-statistics30KBPure JS, slower

License

MIT

Keywords

machine-learning

FAQs

Package last updated on 01 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts