Apparatus Machine Learning Routines
Apparatus is an abandoned Node.js package (last updated in 2012, current version 0.0.10) providing a collection of low-level machine learning algorithms. It focuses on numerical input, primarily arrays of numbers and vectors, and is not designed for direct text or natural language processing. Instead, it serves as a foundational library for other projects like the 'natural' package, which adds a layer of text feature extraction. Due to its age, it primarily uses CommonJS modules and is not compatible with modern Node.js ESM-only environments without transpilation or specific configuration. Its lack of maintenance means it should be approached with caution for new projects.
Common errors
-
TypeError: Class constructor KMeans cannot be invoked without 'new'
cause Attempting to call the `KMeans` constructor (or similar algorithm classes) as a function without the `new` keyword.fixInstantiate the class using `const k_means = new KMeans();` instead of `const k_means = KMeans();`. -
Error: Cannot find module 'apparatus/lib/clustering/k_means'
cause Incorrect import path or the 'apparatus' package is not installed correctly or is not accessible from the current working directory.fixEnsure 'apparatus' is installed (`npm install apparatus`) and verify the exact path `require('apparatus/lib/...')` matches the structure within `node_modules`. Paths are case-sensitive. -
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax (`import { KMeans } from 'apparatus/lib/...'`) in a Node.js environment that expects CommonJS modules.fixReplace `import` statements with CommonJS `require()` calls: `const KMeans = require('apparatus/lib/clustering/k_means').KMeans;`.
Warnings
- breaking The 'apparatus' package has been abandoned since its last commit in March 2012. It is not maintained and is highly unlikely to be compatible with modern Node.js versions (e.g., Node.js 14+). Expect runtime errors related to deprecated APIs, missing polyfills, or C++ native module incompatibilities if any are present.
- gotcha Apparatus relies exclusively on CommonJS `require()` syntax and is not designed for ES Modules (ESM). Attempting to use `import` statements will result in a `SyntaxError` unless a transpilation step (e.g., Babel) is configured or Node.js's `--experimental-modules` flag is used with care, which is not recommended for an abandoned library.
- gotcha The package provides low-level numerical algorithms. It does not include features for natural language processing (NLP), image processing, or complex data pre-processing. Users are expected to prepare their data into arrays of numbers or vectors before passing them to apparatus algorithms.
- deprecated Due to its abandonment, 'apparatus' lacks security updates, bug fixes, or performance improvements. Using it in production environments carries significant risks, including potential vulnerabilities or unexpected behavior with evolving JavaScript runtimes.
Install
-
npm install apparatus -
yarn add apparatus -
pnpm add apparatus
Imports
- BayesClassifier
import { BayesClassifier } from 'apparatus'; import { BayesClassifier } from 'apparatus/lib/classifier/bayes_classifier';const bayes = require('apparatus/lib/classifier/bayes_classifier'); const classifier = new bayes.BayesClassifier(); - KMeans
import { KMeans } from 'apparatus/lib/clustering/k_means'; const KMeans = require('apparatus/lib/clustering/k_means');const KMeans = require('apparatus/lib/clustering/k_means').KMeans; const k_means = new KMeans(); - LogisticRegression
import LogisticRegression from 'apparatus/lib/regression/logistic_regression';
const LogisticRegression = require('apparatus/lib/regression/logistic_regression').LogisticRegression; const regression = new LogisticRegression();
Quickstart
const KMeans = require('apparatus/lib/clustering/k_means').KMeans;
// Sample data: each inner array is a data point (e.g., [x, y])
const data = [
[1, 1], [1.5, 2],
[5, 7], [8, 8],
[1, 0.8], [9, 11]
];
const k_means = new KMeans();
// Configure K-Means algorithm
// k: number of clusters
// iterations: max iterations
// threshold: convergence threshold
k_means.k = 2;
k_means.iterations = 100;
k_means.threshold = 0.001;
// Train the K-Means model
k_means.train(data, (error, result) => {
if (error) {
console.error('K-Means training error:', error);
return;
}
console.log('K-Means Clusters:');
result.clusters.forEach((cluster, index) => {
console.log(` Cluster ${index + 1} Centroid: [${cluster.centroid.join(', ')}]`);
console.log(` Points in Cluster ${index + 1}:`, cluster.points);
});
});