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.

github TerriaJS / terriajs / lib / Map / TableDataSource.js View on Github external
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' && binCount > 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 < 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 < clusters.length; i++) {
            if (i > 0 && clusters[i].length === 1 && 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 < this._binColors.length; i++) {
            this._binColors[i].color = this._getColorFromGradient(i / (this._binColors.length - 1));
        }
    }
};
github commaai / cabana / src / components / Meta.js View on Github external
(partialMapping, [msgId, msg]) => {
        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 =>
      parseInt(count, 10)
    );
    const binnedEntryCounts = ckmeans(
      entryCounts,
      Math.min(entryCounts.length, 10)
    );
    const sortedKeys = binnedEntryCounts
      .map(bin =>
        bin
          .map(entryCount => messagesByEntryCount[entryCount.toString()])
          .reduce((messages, partial) => messages.concat(partial), [])
          .sort((msg1, msg2) => {
            if (msg1.address < msg2.address) {
              return 1;
            } else {
              return -1;
            }
          })
          .map(msg => msg.id)
github commaai / cabana / src / components / Meta.js View on Github external
if (Object.keys(messages).length === 0) return [];
    const messagesByEntryCount = Object.entries(messages).reduce(
      (partialMapping, [msgId, msg]) => {
        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) => parseInt(count, 10));
    const binnedEntryCounts = ckmeans(
      entryCounts,
      Math.min(entryCounts.length, 10)
    );
    const sortedKeys = binnedEntryCounts
      .map((bin) => bin
        .map((entryCount) => messagesByEntryCount[entryCount.toString()])
        .reduce((messages, partial) => messages.concat(partial), [])
        .sort((msg1, msg2) => {
          if (msg1.address < msg2.address) {
            return 1;
          }
          return -1;
        })
        .map((msg) => msg.id))
      .reduce((keys, bin) => keys.concat(bin), [])
      .reverse();
github stevage / mapbox-choropleth / choropleth.js View on Github external
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() {