gpt4 book ai didi

C++ 直方图 bin 排序

转载 作者:行者123 更新时间:2023-11-30 04:20:33 24 4
gpt4 key购买 nike

我正在编写一个函数来克隆 Excell 中数据分析插件的直方图功能。基本上,提供样本数据的输入,然后是 bin 范围。 bin 范围必须单调递增,在我的例子中,需要具体为 [0 20 40 60 80 100]。如果样本大于下限(左边缘)且小于或等于上限(右边缘),则Excel会计算样本是否落入bin范围。

我在下面编写了 bin 排序算法,它为 data0 提供了不正确的输出(非常接近),但为 data1 和 data2 提供了正确的输出。在这种情况下,正确意味着该算法的输出与 Excell 生成的表中的输出完全匹配,其中样本数在 bin 旁边计数。感谢您的帮助!

#include <iostream>

int main(int argc, char **agv)
{
const int SAMPLE_COUNT = 21;
const int BIN_COUNT = 6;
int binranges[BIN_COUNT] = {0, 20, 40, 60, 80, 100};
int bins[BIN_COUNT] = {0, 0, 0, 0, 0, 0};

int data0[SAMPLE_COUNT] = {4,82,49,17,89,73,93,86,74,36,74,55,81,61,88,94,72,65,35,25,79};
// for data0 excell's bins read:
// 0 0
// 20 2
// 40 3
// 60 2
// 80 7
// 100 7
//
// instead output of bins is: 203277

int data1[SAMPLE_COUNT] = {88,83,0,0,95,86,0,94,92,77,94,73,93,90,50,95,93,83,0,95,91};
//for data1 excell and this algorithm both yield:
// 0 4
// 20 0
// 40 0
// 60 1
// 80 2
// 100 14 (correct)

int data2[SAMPLE_COUNT] = {58,48,75,68,85,78,74,83,83,75,67,58,75,58,84,68,57,88,55,79,72};
//for data2 excell and this algorithm both yield:
// 0 0
// 20 0
// 40 0
// 60 6
// 80 10
// 100 5 (correct)

for (unsigned int binNum = 1; binNum < BIN_COUNT; ++binNum)
{
const int leftEdge = binranges[binNum - 1];
const int rightEdge = binranges[binNum];

for (unsigned int sampleNum = 0; sampleNum < SAMPLE_COUNT; ++sampleNum)
{
const int sample = data0[sampleNum];

if (binNum == 1)
{
if (sample >= leftEdge && sample <= rightEdge)
bins[binNum - 1]++;
}
else if (sample > leftEdge && sample <= rightEdge)
{
bins[binNum]++;
}
}
}

for (int i = 0; i < BIN_COUNT; ++i)
std::cout << bins[i] << " " << std::flush;

std::cout << std::endl << std::endl;

return 0;
}

最佳答案

假设边总是按递增顺序排列,你只需要:

     unsigned int bin;
for (unsigned int sampleNum = 0; sampleNum < SAMPLE_COUNT; ++sampleNum)
{
const int sample = data0[sampleNum];
bin = BIN_COUNT;
for (unsigned int binNum = 0; binNum < BIN_COUNT; ++binNum) {
const int rightEdge = binranges[binNum];
if (sample <= rightEdge) {
bin = binNum;
break;
}
}
bins[bin]++;
}

尽管如此,要使此代码起作用,您需要为等于或低于第一个边缘 (0) 的值再添加一个 bin。

合理的是,如果你有 n 个分隔符,那么你就有 n+1 个间隔。

关于C++ 直方图 bin 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15213082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com