我想我会对数据进行排序(如果还没有),那么请考虑相邻的差异.将差异除以数字中较小的数字,即获得百分比变化之间的差异.设置阈值,当更改超过该阈值时,启动新的“群集”.
编辑:C中的快速演示代码:
#include
#include
#include
#include
#include
#include
int main() {
std::vector data{
1.91, 2.87, 3.61, 10.91, 11.91, 12.82, 100.73, 100.71, 101.89, 200
};
// sort the input data
std::sort(data.begin(), data.end());
// find the difference between each number and its predecessor
std::vector diffs;
std::adjacent_difference(data.begin(), data.end(), std::back_inserter(diffs));
// convert differences to percentage changes
std::transform(diffs.begin(), diffs.end(), data.begin(), diffs.begin(),
std::divides());
// print out the results
for (int i = 0; i < data.size(); i++) {
// if a difference exceeds 40%, start a new group:
if (diffs[i] > 0.4)
std::cout << "
";
// print out an item:
std::cout << data[i] << "";
}
return 0;
}
结果:
1.91 2.87 3.61
10.91 11.91 12.82
100.71 100.73 101.89
200