JavaScript将数组划分为范围

时间:2019-07-07 14:57:55

标签: javascript statistics range

我需要一个可以将给定数据集划分为x个相等范围的函数。它应该首先获取数据集的最小值和最大值,然后从中获取分布(最大值-最小值)。然后,将其扩展分为x个部分,并返回包含x个数字的数组。数字表示范围内有多少个数字,数组的索引表示范围。

我的功能:

function getRanges(dataset, numranges)
{
    min = Math.min.apply(dataset);
    max = Math.max.apply(dataset);
    spread = max - min;
    step = spread/numranges;
    ranges = [];

    for (i=0; i<numranges; i++)
    {
        for (n=0; n<dataset.length-1; n++)
        {
            if (dataset[n] >= step*i + min   &&   dataset[n] < step*(i+1) + min)
            {
                ranges[i]++;
            }
        }
    }

    return ranges;
}

输入:

getRanges( [
0.015451195257224088,
0.05483521461643034,
0.06617379094530743,
0.07870092892661003,
0.09966687613379838,
0.10666514582416224,
0.10690839801842311,
0.11439821585323147,
0.12306810641438438,
0.12392980727994662,
0.1408540614658845,
0.15281179716445356,
0.1614029704208362,
0.1718209472324214,
0.18492944998962643,
0.19174111693616214,
0.1924469352070364,
0.201953815915777,
0.20309976786182543,
0.2178176544850601,
0.2213545989524347,
0.22550557354900477,
0.23089729892233193,
0.23819719697005529,
0.23984420754302957,
0.2429043996153234,
0.2437201992017493,
0.24773906891348088,
0.24787672203748462,
0.25129651551005683,
0.25236227381026965,
0.25555897741435185,
0.2568307873610046,
0.2579000754060184,
0.25991068215231194,
0.26076078604654973,
0.26938676930938527,
0.27137058611500353,
0.2714326441795975,
0.2764167784037549,
0.28195315820279987,
0.2846181948125861,
0.28494790062924125,
0.2955654519507425,
0.2980014993424446,
0.30091593716696113,
0.30212161416680083,
0.30658886094504534,
0.31305918674142585,
0.317928758159428,
0.3326659331080829,
0.34726651130860553,
0.3577536858416604,
0.36040942846958357,
0.3634475366141404,
0.3636502832650337,
0.37657879597026606,
0.37814949618916843,
0.38186803855924834,
0.38845503635191553,
0.3910772647246715,
0.39400632306393685,
0.4109357393771207,
0.4163410915311307,
0.41816459971235165,
0.42252764144480326,
0.43347380456325807,
0.435696281748153,
0.44301634426481185,
0.453481768706172,
0.46307154320261923,
0.4691677512037378,
0.46934557070509453,
0.4736100446708187,
0.4764587668478003,
0.4914682078649019,
0.49708883552851807,
0.4989653008464718,
0.5011285370045405,
0.5098189247039806,
0.5209596336702528,
0.5278237946925293,
0.5501036890701407,
0.5551041026696315,
0.5593579433255046,
0.5614983229767005,
0.5617926864659308,
0.5655289412257961,
0.5802908272771363,
0.5889310183452419,
0.6006524589952945,
0.624822034287017,
0.6302801026102428,
0.6498155436414438,
0.6711314255389402,
0.7097413148908829,
0.7279695179821742,
0.7805217243317325,
0.8095861819199952,
1
], 10);

预期输出:
[5,12,28,17,16,12,5,3,1,1]
因为有5个数字n >= 0 && n < 0.1
和12个数字n >= 0.1 && n < 0.2
和28个数字n >= 0.2 && n < 0.3
等等

实际输出:
[]
(不是在开玩笑)

2 个答案:

答案 0 :(得分:2)

有一些问题:

  1. 您以错误的方式使用Math.min.applyMath.max.apply

  2. 您的ranges变量是一个空数组,因此当您运行ranges[i]++时,它会得到NaN

  3. 您的预期输出不正确,因为step不是0.1

这是工作版本:

function getRanges(dataset, numranges)
{
    min = Math.min.apply(null, dataset);
    max = Math.max.apply(null, dataset);
    spread = max - min;
    step = spread/numranges;
    ranges = [];

    for (i=0; i<numranges; i++)
    {
        for (n=0; n<dataset.length-1; n++)
        {
            if (dataset[n] >= step*i + min   &&   dataset[n] < step*(i+1) + min)
            {
                if (!ranges[i]) ranges[i] = 0;
                ranges[i]++;
            }
        }
    }

    return ranges;
}

console.log(getRanges( [
0.015451195257224088,
0.05483521461643034,
0.06617379094530743,
0.07870092892661003,
0.09966687613379838,
0.10666514582416224,
0.10690839801842311,
0.11439821585323147,
0.12306810641438438,
0.12392980727994662,
0.1408540614658845,
0.15281179716445356,
0.1614029704208362,
0.1718209472324214,
0.18492944998962643,
0.19174111693616214,
0.1924469352070364,
0.201953815915777,
0.20309976786182543,
0.2178176544850601,
0.2213545989524347,
0.22550557354900477,
0.23089729892233193,
0.23819719697005529,
0.23984420754302957,
0.2429043996153234,
0.2437201992017493,
0.24773906891348088,
0.24787672203748462,
0.25129651551005683,
0.25236227381026965,
0.25555897741435185,
0.2568307873610046,
0.2579000754060184,
0.25991068215231194,
0.26076078604654973,
0.26938676930938527,
0.27137058611500353,
0.2714326441795975,
0.2764167784037549,
0.28195315820279987,
0.2846181948125861,
0.28494790062924125,
0.2955654519507425,
0.2980014993424446,
0.30091593716696113,
0.30212161416680083,
0.30658886094504534,
0.31305918674142585,
0.317928758159428,
0.3326659331080829,
0.34726651130860553,
0.3577536858416604,
0.36040942846958357,
0.3634475366141404,
0.3636502832650337,
0.37657879597026606,
0.37814949618916843,
0.38186803855924834,
0.38845503635191553,
0.3910772647246715,
0.39400632306393685,
0.4109357393771207,
0.4163410915311307,
0.41816459971235165,
0.42252764144480326,
0.43347380456325807,
0.435696281748153,
0.44301634426481185,
0.453481768706172,
0.46307154320261923,
0.4691677512037378,
0.46934557070509453,
0.4736100446708187,
0.4764587668478003,
0.4914682078649019,
0.49708883552851807,
0.4989653008464718,
0.5011285370045405,
0.5098189247039806,
0.5209596336702528,
0.5278237946925293,
0.5501036890701407,
0.5551041026696315,
0.5593579433255046,
0.5614983229767005,
0.5617926864659308,
0.5655289412257961,
0.5802908272771363,
0.5889310183452419,
0.6006524589952945,
0.624822034287017,
0.6302801026102428,
0.6498155436414438,
0.6711314255389402,
0.7097413148908829,
0.7279695179821742,
0.7805217243317325,
0.8095861819199952,
1
], 10))

答案 1 :(得分:0)

使用以下任一解决方案从数组创建范围。范围从数组的最小数开始到最大数。让我知道这是否有效!

// sample array that needs to be divided into n ranges
const arr = [10,20,34,20,50,76];

//1. Calculate min and max of array
const min = Math.min(...arr);
const max = Math.max(...arr);

//2. Calculate ranges in [[10, 20], [20, 30], [30, 40]] format
const getRangeArray = (min, max, n) => {
  const delta = (max - min) / n;

  const ranges = [];
  let range1 = min;
  for (let i = 0; i < n; i += 1) {
    const range2 = range1 + delta;
    ranges.push([range1, range2]);
    range1 = range2;
  }

  return ranges;
};

const rangeArrayIn3Parts = getRangeArray(min, max, 3);
console.log("Divided range array into 3 parts", rangeArrayIn3Parts);

//2. Calculate ranges in [10, 20, 30, 40] format
const getRange = (min, max, n) => {
  const delta = (max - min) / n;

  const ranges = [];
  let range1 = min;
  for (let i = 0; i <= n; i += 1) {
    ranges.push(range1);
    range1 += delta;
  }

  return ranges;
};

const rangeInto10Parts = getRange(min, max, 9);
console.log("Divided range into 10 parts", rangeInto10Parts);