数据预处理之Minkowski距离计算

template <class T1, class T2>
double Minkowski(const std::vector<T1> &inst1, const std::vector<T2> &inst2, const double &k) {
    if(inst1.size() != inst2.size()) {
        std::cout<<"the size of the vectors is not the same\n";
        return -1;
    }
    std::vector<double> temp;
    for(size_t i=0; i<inst1.size(); ++i) {
        temp.push_back(pow(std::abs(inst1.at(i)-inst2.at(i)), k));
    }
    double distance=accumulate(temp.begin(), temp.end(), 0.0);
    distance=pow(distance, 1.0/k);

    return distance;
}

 

posted @ 2019-03-06 16:57  东宫得臣  阅读(470)  评论(0编辑  收藏  举报