partitioning an float array into similar segments (clustering)

前端 未结 2 1084
盖世英雄少女心
盖世英雄少女心 2021-01-04 18:13

I have an array of floats like this:

[1.91, 2.87, 3.61, 10.91, 11.91, 12.82, 100.73, 100.71, 101.89, 200]

Now, I want to partition the arra

2条回答
  •  礼貌的吻别
    2021-01-04 18:59

    I think I'd sort the data (if it's not already), then take adjacent differences. Divide the differences by the smaller of the numbers it's a difference between to get a percentage change. Set a threshold and when the change exceeds that threshold, start a new "cluster".

    Edit: Quick demo code in 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 << "\n";
    
            // print out an item:
            std::cout << data[i] << "\t";
        }
    
        return 0;
    }
    

    Result:

    1.91    2.87    3.61
    10.91   11.91   12.82
    100.71  100.73  101.89
    200
    

提交回复
热议问题