Signature | Description | Parameters |
---|---|---|
#include <DataFrame/DataFrameStatsVisitors.h> template<typename T, typename I = unsigned long, std::size_t A = 0> struct MedianVisitor; // ------------------------------------- template<typename T, typename I = unsigned long, std::size_t A = 0> using med_v = MedianVisitor<T, I, A>; |
This is a “single action visitor”, meaning it is passed the whole data vector in one call and you must use the single_act_visit() interface. This functor class finds the median of the given column, using the above Kth element visitor. It computes in linear time. |
T: Column data type I: Index type A: Memory alignment boundary for vectors. Default is system default alignment |
static void test_median() { std::cout << "\nTesting Median ..." << std::endl; std::vector<unsigned long> idx = { 123450, 123451, 123452, 123453, 123454, 123455, 123456, 123457, 123458, 123459, 123460, 123461, 123462, 123466, 123467, 123468, 123469, 123470, 123471, 123472, 123473 }; std::vector<double> d1 = { 1.0, 10, 8, 18, 19, 16, 21, 17, 20, 3, 2, 11, 7.0, 5, 9, 15, 14, 13, 12, 6, 4 }; std::vector<double> d2 = { 1.0, 10, 8, 18, 19, 16, 17, 20, 3, 2, 11, 7.0, 5, 9, 15, 14, 13, 12, 6, 4 }; std::vector<int> i1 = { 1, 10, 8, 18, 19, 16, 21, 17, 20, 3, 2, 11, 7, 5, 9, 15, 14, 13, 12, 6, 4 }; std::vector<int> i2 = { 1, 10, 8, 18, 19, 16, 17, 20, 3, 2, 11, 7, 5, 9, 15, 14, 13, 12, 6, 4 }; MyDataFrame df; df.load_data(std::move(idx), std::make_pair("dblcol_1", d1), std::make_pair("intcol_1", i1)); df.load_column("dblcol_2", std::move(d2), nan_policy::dont_pad_with_nans); df.load_column("intcol_2", std::move(i2), nan_policy::dont_pad_with_nans); MedianVisitor<double> med_visit; double result = df.single_act_visit<double>("dblcol_1", med_visit).get_result(); assert(result == 10.0); result = df.single_act_visit<double>("dblcol_2", med_visit).get_result(); assert(result == 10.50); MedianVisitor<int> med_visit2; int result2 = df.single_act_visit<int>("intcol_1", med_visit2).get_result(); assert(result2 == 10); result2 = df.single_act_visit<int>("intcol_2", med_visit2).get_result(); assert(result2 == 10); }