class Histogram 数据直方图统计函数
作用:统计所有数据中不同区间数据的个数
-
函数:
- 默认构造
-
add(float value) :添加数据
- ToString(int buckets) :输出统计结果
-
buckets 输入,表示统计数据分成的区间数量
-
参数:
- values_; 待统计数据集合
carto 代码引用说明:
Histogram —> score_histogram_( 在 constraint_builder_2d.h Histogram of scan matcher scores.统计 评分激光地图匹配评分结果)
为了更好理解代码,简单改了下类中函数,添加了打印,形成histogram_test.cc 可以直接编译运行。
histogram_test.cc
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <math.h>
using namespace std;
std::string toString2f(float a)
{
char buffer[10];
sprintf(buffer, "%.2f", a);
std::string str = buffer;
return str;
}
class Histogram {
public:
void Add(float value);
std::string ToString(int buckets) const;
private:
std::vector<float> values_;
};
void Histogram::Add(const float value)
{
cout << " add: " << value;
values_.push_back(value);
}
std::string Histogram::ToString(int buckets) const
{
cout << endl;
if (values_.empty()) {
return "Count: 0";
}
float min = *std::min_element(values_.begin(), values_.end());
float max = *std::max_element(values_.begin(), values_.end());
/* std::string result = absl::StrCat("Count: ", values_.size(), " Min: ", min,
" Max: ", max, " Mean: ", mean);*/
std::string result = "result: " ;
int values_size = values_.size();
result +="count: " + to_string(values_size);
result +=" Min: " + toString2f(min);
result +=" Max: " + toString2f(max);
if (min == max) {
return result;
}
float lower_bound = min;
int total_count = 0;
for (int i = 0; i != buckets; ++i) {
const float upper_bound =
(i + 1 == buckets)
? max
: (max * (i + 1) / buckets + min * (buckets - i - 1) / buckets);
int count = 0;
for (const float value : values_) {
if (lower_bound <= value &&
(i + 1 == buckets ? value <= upper_bound : value < upper_bound)) {
++count;
}
}
total_count += count;
result += "
[" + toString2f(lower_bound) + "," + toString2f(upper_bound);
result += i + 1 == buckets ? ']' : ')';
/* absl::StrAppendFormat(&result, "
[%f, %f%c", lower_bound, upper_bound,
i + 1 == buckets ? ']' : ')');*/
constexpr int kMaxBarChars = 20;
const int bar =
(count * kMaxBarChars + values_.size() / 2) / values_.size();
result += " ";
for (int i = 0; i != kMaxBarChars; ++i) {
result += (i < (kMaxBarChars - bar)) ? " " : "#";
}
result += " Count: "+ to_string(count) + " (" +
toString2f(count * 1e2f / values_.size()) + "%)"
+ " Total: " + to_string(total_count) + " ("
+ toString2f(total_count * 1e2f / values_.size()) + "%)";
/* absl::StrAppend(&result, " Count: ", count, " (",
count * 1e2f / values_.size(), "%)",
" Total: ", total_count, " (",
total_count * 1e2f / values_.size(), "%)");*/
lower_bound = upper_bound;
}
return result;
}
int main()
{
Histogram h;
h.Add(1);h.Add(3);h.Add(6);
h.Add(3);h.Add(3);h.Add(8);
h.Add(4);h.Add(7);h.Add(9);
h.Add(2);h.Add(6);h.Add(5);
h.Add(3);h.Add(5);h.Add(7);
cout << h.ToString(3);
}
输出结果:
add: 1 add: 3 add: 6 add: 3 add: 3 add: 8 add: 4 add: 7 add: 9 add: 2 add: 6 add: 5 add: 3 add: 5 add: 7
result: count: 15 Min: 1.00 Max: 9.00
[1.00,3.67) ######## Count: 6 (40.00%) Total: 6 (40.00%)
[3.67,6.33) ####### Count: 5 (33.33%) Total: 11 (73.33%)
[6.33,9.00] ##### Count: 4 (26.67%) Total: 15 (100.00%)[Finished in 0.6s]