MCPcopy Index your code
hub / github.com/forloopcodes/contextplus / findOptimalK

Function findOptimalK

src/core/clustering.ts:65–81  ·  view source on GitHub ↗
(eigenvalues: number[], maxK: number)

Source from the content-addressed store, hash-verified

63}
64
65function findOptimalK(eigenvalues: number[], maxK: number): number {
66 const sorted = [...eigenvalues].sort((a, b) => a - b);
67 if (sorted.length <= 2) return Math.min(2, sorted.length);
68
69 let bestGap = 0;
70 let bestK = 2;
71 const limit = Math.min(maxK, sorted.length - 1);
72
73 for (let k = 2; k <= limit; k++) {
74 const gap = sorted[k] - sorted[k - 1];
75 if (gap > bestGap) {
76 bestGap = gap;
77 bestK = k;
78 }
79 }
80 return bestK;
81}
82
83function kMeans(data: number[][], k: number): number[] {
84 const n = data.length;

Callers 1

spectralClusterFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected