trekhleb/javascript-algorithms · error · Error
Either dataSet or labels or toClassify were not set
Error message
Either dataSet or labels or toClassify were not set
What it means
kNN(dataSet, labels, toClassify, k) requires all three leading arguments to be truthy; if dataSet, labels or toClassify is null/undefined the function refuses to run. The three inputs are the training points, their per-point class labels, and the query point to classify. Truthiness means empty arrays pass this check and fail later, so this message specifically means one argument was not supplied at all.
Source
Thrown at src/algorithms/ml/knn/kNN.js:20
* Classifies the point in space based on k-nearest neighbors algorithm.
*
* @param {number[][]} dataSet - array of data points, i.e. [[0, 1], [3, 4], [5, 7]]
* @param {number[]} labels - array of classes (labels), i.e. [1, 1, 2]
* @param {number[]} toClassify - the point in space that needs to be classified, i.e. [5, 4]
* @param {number} k - number of nearest neighbors which will be taken into account (preferably odd)
* @return {number} - the class of the point
*/
import euclideanDistance from '../../math/euclidean-distance/euclideanDistance';
export default function kNN(
dataSet,
labels,
toClassify,
k = 3,
) {
if (!dataSet || !labels || !toClassify) {
throw new Error('Either dataSet or labels or toClassify were not set');
}
// Calculate distance from toClassify to each point for all dimensions in dataSet.
// Store distance and point's label into distances list.
const distances = [];
for (let i = 0; i < dataSet.length; i += 1) {
distances.push({
dist: euclideanDistance([dataSet[i]], [toClassify]),
label: labels[i],
});
}
// Sort distances list (from closer point to further ones).
// Take initial k values, count with class index
const kNearest = distances.sort((a, b) => {
if (a.dist === b.dist) {
return 0;
}View on GitHub (pinned to 85293e3e2b)
Solutions
- Log all three arguments before the call to see which is falsy
- Supply the missing argument - most often toClassify (the query point, e.g. [5, 4]) or a labels array aligned 1:1 with dataSet
- Also guard the cases this check misses: dataSet.length === labels.length and toClassify.length === dataSet[0].length
Example fix
// before import kNN from './src/algorithms/ml/knn/kNN'; const label = kNN(dataSet, labels); // toClassify missing -> throws // after const label = kNN(dataSet, labels, [5, 4], 3);
Defensive patterns
Strategy: validation
Validate before calling
if (!Array.isArray(dataSet) || !Array.isArray(labels) || !Array.isArray(toClassify)) {
throw new TypeError('kNN: dataSet, labels and toClassify must all be arrays');
}
if (dataSet.length === 0 || dataSet.length !== labels.length) {
throw new TypeError('kNN: dataSet and labels must be non-empty and aligned');
}
kNN(dataSet, labels, toClassify, k); Type guard
const isKnnInput = (ds, ls, q) => Array.isArray(ds) && Array.isArray(ls) && Array.isArray(q) && ds.length > 0 && ds.length === ls.length && q.length === ds[0].length;
Try / catch
try {
cls = kNN(dataSet, labels, query, k);
} catch (e) {
if (e.message === 'Either dataSet or labels or toClassify were not set') {
cls = null; // skip classification for this record
} else {
throw e;
}
} Prevention
- Validate dataSet.length === labels.length once when training data is built
- Unit-test the missing-middle-argument case; it is the most common regression
- Consider an options object at wrapper level when call sites churn
When it happens
Trigger: kNN(dataSet, labels) with toClassify forgotten; labels undefined because a parse or zip step failed; toClassify null when classifying optional records; destructuring a response with renamed keys producing undefined.
Common situations: Refactors that reorder or rename parameters, API responses with missing fields, CSV rows whose label column is absent, and optional classification requests where the query point is genuinely absent.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- The data is empty
- The method supports only positive integers
- Items and weights must be of the same size
- Items must not be empty
- Strings must be of the same length
AI-assisted analysis of trekhleb/javascript-algorithms@85293e3e2b (2026-08-24).
Data as JSON: /api/errors/45595037aa2041b6.
Report an issue: GitHub.