首页 > 其他 > 详细

两个向量之间的欧式距离

时间:2018-08-21 20:10:12      阅读:459      评论:0      收藏:0      [点我收藏+]

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

  return distance;
}

kNN具体的实现可以有很多的优化方式,如可以在计算前先排除掉与预测集数据距离较大的一些噪音点,

从而提高计算效率。

两个向量之间的欧式距离

原文:https://www.cnblogs.com/donggongdechen/p/9513575.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!