Skip to content

Commit 0220d85

Browse files
authored
Merge pull request #27 from kevinrobinson/readme-constraints
Add helpful error explanation when size of data doesn't match nNeighbors
2 parents 7c78c4c + 8a09f28 commit 0220d85

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/umap.ts

+4
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ export class UMAP {
322322
* SGD optimization.
323323
*/
324324
initializeFit(X: Vectors): number {
325+
if (X.length <= this.nNeighbors) {
326+
throw new Error(`Not enough data points (${X.length}) to create nNeighbors: ${this.nNeighbors}. Add more data points or adjust the configuration.`);
327+
}
328+
325329
// We don't need to reinitialize if we've already initialized for this data.
326330
if (this.X === X && this.isInitialized) {
327331
return this.getNEpochs();

test/umap.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ describe('UMAP', () => {
202202

203203
expect(nInvocations).toBeGreaterThan(0);
204204
});
205+
206+
test('initializeFit throws helpful error if not enough data', () => {
207+
const umap = new UMAP({ random });
208+
const smallData = testData.slice(0, 15);
209+
expect(() => umap.initializeFit(smallData)).toThrow(/Not enough data points/);
210+
})
205211
});
206212

207213
function computeMeanDistances(vectors: number[][]) {

0 commit comments

Comments
 (0)