分布javascript算法

gg0vcinb  于 2021-09-08  发布在  Java
关注(0)|答案(3)|浏览(288)

您好,请了解我正在尝试编写的频率分布算法。
我已经实现了下面的代码来计算频率分布算法:

let class_boundaries = [
  [145, 146],
  [147, 148],
  [149, 150],
  [151, 152],
  [153, 154],
  [155, 156],
  [157, 158],
  [159, 160]
];

let un_gouped_data = [151, 147, 145, 153, 156, 152, 159, 153, 157, 152,
  145, 151, 157, 147, 150, 157, 153, 151, 149, 147,
  151, 147, 155, 156, 151, 158, 149, 147, 153, 152,
  149, 151, 153, 150, 152, 154, 150, 152, 149, 151,
  151, 154, 155, 152, 154, 152, 156, 155, 154, 150
];

function makeFrequencies(un_grouped_data) {
  let data_obj = {};
  for (let i = 0; i < un_grouped_data.length; i++) {
    let element = un_grouped_data[i];

    if (data_obj[element] !== undefined) {
      data_obj[element] += 1;
    } else {
      data_obj[element] = 1;
    }
  }
  return (data_obj)
}

我想要的输出应该是以下对象:

{
  145-146: 2, 
  147-148: 5, 
  149-150: 8,  
  151-152: 8, 
  153-154: 9, 
  155-156: 6, 
  157-158: 4, 
  159-160: 2
}

但我的代码得到的输出如下:

{
  145: 2, 
  147: 5, 
  149: 4, 
  150: 4, 
  151: 8, 
  152: 7, 
  153: 5, 
  154: 4, 
  155: 3, 
  156: 3, 
  157: 3, 
  158: 1, 
  159: 1
}

注意:键是类边界,值是该类中数字出现次数的计数。

5sxhfpxr

5sxhfpxr1#

您可以找到该范围所需的键并增加值。

function makeFrequencies(data, boundaries) {
    const
        getKey = v => boundaries
            .find(([l, r]) => v >= l && v <= r)
            .join('-');

    return data.reduce(
        (r, v) => (r[getKey(v)]++, r),
        Object.fromEntries(boundaries.map(a => [a.join('-'), 0]))
    );
}

const
    class_boundaries = [[145, 146], [147, 148], [149, 150], [151, 152], [153, 154], [155, 156], [157, 158], [159, 160]],
    un_gouped_data = [151, 147, 145, 153, 156, 152, 159, 153, 157, 152, 145, 151, 157, 147, 150, 157, 153, 151, 149, 147, 151, 147, 155, 156, 151, 158, 149, 147, 153, 152, 149, 151, 153, 150, 152, 154, 150, 152, 149, 151, 151, 154, 155, 152, 154, 152, 156, 155, 154, 150];

console.log(makeFrequencies(un_gouped_data, class_boundaries));
.as-console-wrapper { max-height: 100% !important; top: 0; }
cwxwcias

cwxwcias2#

let class_boundaries = [
  [145, 146],
  [147, 148],
  [149, 150],
  [151, 152],
  [153, 154],
  [155, 156],
  [157, 158],
  [159, 160]
];

let un_grouped_data = [151, 147, 145, 153, 156, 152, 159, 153, 157, 152,
  145, 151, 157, 147, 150, 157, 153, 151, 149, 147,
  151, 147, 155, 156, 151, 158, 149, 147, 153, 152,
  149, 151, 153, 150, 152, 154, 150, 152, 149, 151,
  151, 154, 155, 152, 154, 152, 156, 155, 154, 150
];

function makeFrequencies() {
  let output = {}

  //initiliazed output
  class_boundaries.forEach(class_b => {
    output[`${class_b[0]}-${class_b[1]}`] = 0;
  })

  un_grouped_data.forEach(item => {
    class_boundaries.forEach(class_b => {
      if (item >= class_b[0] && item <= class_b[1]) {
        output[`${class_b[0]}-${class_b[1]}`] = output[`${class_b[0]}-${class_b[1]}`] + 1
      }
    })
  })
 return output
}

console.log(makeFrequencies())
q7solyqu

q7solyqu3#

您可以从中创建范围 class_boundaries 作为

class_boundaries.map(([a, b]) => ({
  [`${a}-${b}`]: Array.from(Array(b - a + 1).fill(a), (x, i) => x + i).reduce(
    (acc, curr) => acc + (temp[curr] ?? 0),
    0
  ),
}));
let class_boundaries = [
  [145, 146],
  [147, 148],
  [149, 150],
  [151, 152],
  [153, 154],
  [155, 156],
  [157, 158],
  [159, 160],
];

let un_gouped_data = [
  151,
  147,
  145,
  153,
  156,
  152,
  159,
  153,
  157,
  152,
  145,
  151,
  157,
  147,
  150,
  157,
  153,
  151,
  149,
  147,
  151,
  147,
  155,
  156,
  151,
  158,
  149,
  147,
  153,
  152,
  149,
  151,
  153,
  150,
  152,
  154,
  150,
  152,
  149,
  151,
  151,
  154,
  155,
  152,
  154,
  152,
  156,
  155,
  154,
  150,
];

function makeFrequencies(un_grouped_data) {
  let data_obj = {};
  for (let i = 0; i < un_grouped_data.length; i++) {
    let element = un_grouped_data[i];

    if (data_obj[element] !== undefined) {
      data_obj[element] += 1;
    } else {
      data_obj[element] = 1;
    }
  }
  return data_obj;
}
const temp = makeFrequencies(un_gouped_data);
const result = class_boundaries.map(([a, b]) => ({
  [`${a}-${b}`]: Array.from(Array(b - a + 1).fill(a), (x, i) => x + i).reduce(
    (acc, curr) => acc + (temp[curr] ?? 0),
    0
  ),
}));
console.log(result);

相关问题