Skip to content

Commit bd395c4

Browse files
authored
add friendlier message for oneHot error (#268)
1 parent f611aee commit bd395c4

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/NeuralNetwork/NeuralNetworkData.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,18 @@ class NeuralNetworkData {
502502
};
503503

504504
const uniqueVals = _uniqueValuesArray; // [...new Set(this.data.raw.map(obj => obj.xs[prop]))]
505+
506+
// Validate that we have at least 2 classes for classification
507+
if (uniqueVals.length < 2) {
508+
throw new Error(
509+
`🟪 ml5.js classification error: Classification requires at least 2 different classes, but only found ${
510+
uniqueVals.length
511+
} class: "${
512+
uniqueVals[0] || "undefined"
513+
}". Please add training data with multiple different output values.`
514+
);
515+
}
516+
505517
// get back values from 0 to the length of the uniqueVals array
506518
const onehotValues = uniqueVals.map((item, idx) => idx);
507519
// oneHot encode the values in the 1d tensor

0 commit comments

Comments
 (0)