<commit_before>// Unsupervised clusterization model using K-means
// Authors: <NAME> (current), <NAME> (preliminary)
// License: Apache License 2.0

'use strict';

const thenify = require('thenify');

function KMeans(nClusters, options) {
  if (!(this instanceof KMeans))
    return new KMeans(nClusters, options);
  options = options || {};
  this.nClusters = nClusters;
  this.maxMse = options.maxMse || 0.0000001;
  this.means = options.means;
  this.maxIterations = options.maxIterations || 100;

  // Return cluster index for which element is closest to center (euclidean norm)
  // Function is inlined in object (vs prototype) serialized to workers
  this.predict = function (element) {
    let means = this.means;
    let smallestSn = Infinity;
    let smallestSnIdx;
    for (let i = 0; i < means.length; i++) {
      let sn = 0;
      for (let j = 0; j < element.length; j++)
        sn += (element[j] - means[i][j]) ** 2;
      if (sn < smallestSn) {
        smallestSnIdx = i;
        smallestSn = sn;<commit_msg>do not use ** operator for compatibility with node 6<commit_after>// Unsupervised clusterization model using K-means
// Authors: <NAME> (current), <NAME> (preliminary)
// License: Apache License 2.0

'use strict';

const thenify = require('thenify');

function KMeans(nClusters, options) {
  if (!(this instanceof KMeans))
    return new KMeans(nClusters, options);
  options = options || {};
  this.nClusters = nClusters;
  this.maxMse = options.maxMse || 0.0000001;
  this.means = options.means;
  this.maxIterations = options.maxIterations || 100;

  // Return cluster index for which element is closest to center (euclidean norm)
  // Function is inlined in object (vs prototype) serialized to workers
  this.predict = function (element) {
    let means = this.means;
    let smallestSn = Infinity;
    let smallestSnIdx;
    for (let i = 0; i < means.length; i++) {
      let sn = 0;
      for (let j = 0; j < element.length; j++) {
        let delta = (element[j] - means[i][j]);
        sn += delta * delta;
      }
      if (sn < smallestSn) {
        smallestSnIdx = i;
        smallestSn = sn;
{'fraction_non_alphanumeric': 0.12646920545369064, 'fraction_numerical': 0.015984955336154207, 'mean_word_length': 3.108108108108108, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2913941', 'n_tokens_mistral': 702, 'n_tokens_neox': 660, 'n_words': 218}