# How to use the simple-statistics.ckmeans function in simple-statistics

## To help you get started, weâ€™ve selected a few simple-statistics examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

TerriaJS / terriajs / lib / Map / TableDataSource.js View on Github
``````var vals = this.dataset.getVariableValues(this.dataset.selected.data).filter(function(x) { return typeof x === 'number'; });
var binCount = Math.min(this.colorBins, vals.length), i; // Must ask for fewer clusters than the number of items.

// here we convert the output formats of two binning methods into a structure like [ { color: 'green', upperBound: 20 } , { color: 'orange': upperBound: 80 } ]
if (this.colorBinMethod === 'quantile' || this.colorBinMethod === 'auto' &amp;&amp; binCount &gt; 10000) {
// the quantile method is simpler, less accurate, but faster for large datasets. One issue is we don't check to see if any
// values actually lie within a given quantile, so it's bad for small datasets.
for (i = 0; i &lt; binCount; i++) {
this._binColors.push({
upperBound: simplestats.quantile(vals, (i + 1) / binCount),
color: this._getColorFromGradient(i / (binCount - 1))
});
}
} else {
var clusters = simplestats.ckmeans(vals, binCount);

// turn the ckmeans format [ [5, 20], [65, 80] ]
for (i = 0; i &lt; clusters.length; i++) {
if (i &gt; 0 &amp;&amp; clusters[i].length === 1 &amp;&amp; clusters[i][0] === clusters[i - 1][clusters[i - 1].length - 1]) {
// When there are few unique values, we can end up with clusters like [1], [2],[2],[2],[3]. Let's avoid that.
continue;
}
this._binColors.push({
upperBound: clusters[i][clusters[i].length-1],
});
}
for (i = 0; i &lt; this._binColors.length; i++) {
this._binColors[i].color = this._getColorFromGradient(i / (this._binColors.length - 1));
}
}
};``````
commaai / cabana / src / components / Meta.js View on Github
``````(partialMapping, [msgId, msg]) =&gt; {
const entryCountKey = msg.entries.length.toString(); // js object keys are strings
if (!partialMapping[entryCountKey]) {
partialMapping[entryCountKey] = [msg];
} else {
partialMapping[entryCountKey].push(msg);
}
return partialMapping;
},
{}
);

const entryCounts = Object.keys(messagesByEntryCount).map(count =&gt;
parseInt(count, 10)
);
const binnedEntryCounts = ckmeans(
entryCounts,
Math.min(entryCounts.length, 10)
);
const sortedKeys = binnedEntryCounts
.map(bin =&gt;
bin
.map(entryCount =&gt; messagesByEntryCount[entryCount.toString()])
.reduce((messages, partial) =&gt; messages.concat(partial), [])
.sort((msg1, msg2) =&gt; {
return 1;
} else {
return -1;
}
})
.map(msg =&gt; msg.id)``````
commaai / cabana / src / components / Meta.js View on Github
``````if (Object.keys(messages).length === 0) return [];
const messagesByEntryCount = Object.entries(messages).reduce(
(partialMapping, [msgId, msg]) =&gt; {
const entryCountKey = msg.entries.length.toString(); // js object keys are strings
if (!partialMapping[entryCountKey]) {
partialMapping[entryCountKey] = [msg];
} else {
partialMapping[entryCountKey].push(msg);
}
return partialMapping;
},
{}
);

const entryCounts = Object.keys(messagesByEntryCount).map((count) =&gt; parseInt(count, 10));
const binnedEntryCounts = ckmeans(
entryCounts,
Math.min(entryCounts.length, 10)
);
const sortedKeys = binnedEntryCounts
.map((bin) =&gt; bin
.map((entryCount) =&gt; messagesByEntryCount[entryCount.toString()])
.reduce((messages, partial) =&gt; messages.concat(partial), [])
.sort((msg1, msg2) =&gt; {
return 1;
}
return -1;
})
.map((msg) =&gt; msg.id))
.reduce((keys, bin) =&gt; keys.concat(bin), [])
.reverse();``````
stevage / mapbox-choropleth / choropleth.js View on Github
``````breaks (vals) {
this.bins = ss.ckmeans(vals, Math.min(this.binCount, vals.length))
.map(bin => [bin[0], bin[bin.length -1]]);
this.minVal = this.bins[0][0];
this.maxVal = this.bins[this.bins.length - 1][1];
return [this.bins[0][0],  ...this.bins.map(b => b[1])];
}
makeColorScale() {``````

## simple-statistics

Simple Statistics

ISC