Shark源码分析(十):KNN算法

2024-04-27 00:48
文章标签 算法 分析 源码 knn shark

本文主要是介绍Shark源码分析(十):KNN算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Shark源码分析(十):KNN算法

关于这个算法,我之前已经有博客详细介绍过。虽然说这个算法看上去非常的简单,但是在搜索k个最近邻居数据点时,还是非常具有技巧性的。这里还是有必要再次强调一下。如果输入数据的维度不高,可以使用树形结构(kd树)来加快查找的速度。如果输入的维度较高,则利用树型结构的速度与计算两两数据间距离的速度并不会有太大的差别。之后我们要介绍的代码也是利用kd树来组织的。

在计算距离时,不仅可以选择欧几里得距离,同样可以选择基于核函数的距离。同样地,也有基于核函数距离的kd树。

BinaryTree类

这个类不是我们通常所认为的二叉树的结点类,而是表示binary space-partitioning tree 的结点。在每一个父结点处,表示将当前的空间分为两个子空间。这个分隔,不仅允许线性地分隔,同样也可以使用基于核函数的分隔。该类定义在<include/shark/Models/Trees/BinaryTree.h>

template <class InputT>
class BinaryTree
{
public:typedef InputT value_type;BinaryTree(std::size_t size): mep_parent(NULL), mp_left(NULL), mp_right(NULL), mp_indexList(NULL), m_size(size), m_nodes(0), m_threshold(0.0){SHARK_ASSERT(m_size > 0);mp_indexList = new std::size_t[m_size];boost::iota(boost::make_iterator_range(mp_indexList,mp_indexList+m_size),0);}virtual ~BinaryTree(){if (mp_left != NULL) delete mp_left;if (mp_right != NULL) delete mp_right;if (mep_parent == NULL) delete [] mp_indexList;}BinaryTree* parent(){ return mep_parent; }const BinaryTree* parent() const{ return mep_parent; }bool hasChildren() const{ return (mp_left != NULL); }bool isLeaf() const{ return (mp_left == NULL); }BinaryTree* left(){ return mp_left; }const BinaryTree* left() const{ return mp_left; }BinaryTree* right(){ return mp_right; }const BinaryTree* right() const{ return mp_right; }std::size_t size() const{ return m_size; }std::size_t nodes() const{ return m_nodes; }std::size_t index(std::size_t point)const{return mp_indexList[point];}double distanceFromPlane(value_type const& point) const{return funct(point) - m_threshold;}double threshold() const{return m_threshold;}// 注意到,前面的left函数表示返回左孩子结点,而该函数的意思是// 查询结点是否位于左子空间内bool isLeft(value_type const& point) const{ return (funct(point) < m_threshold); }bool isRight(value_type const& point) const{ return (funct(point) >= m_threshold); }//如果计算距离时使用的是核函数,则返回核函数的对象virtual AbstractKernelFunction<value_type> const* kernel()const{//default is no kernel metricreturn NULL;}// 计算查询点与当前空间距离下界的平方// 灵活使用三角不等式,可以使这个界更紧,搜索的速度也更快virtual double squaredDistanceLowerBound(value_type const& point) const = 0;protected:BinaryTree(BinaryTree* parent, std::size_t* list, std::size_t size): mep_parent(parent), mp_left(NULL), mp_right(NULL), mp_indexList(list), m_size(size), m_nodes(0){}// 计算查询点与当前分隔平面的距离virtual double funct(value_type const& point) const = 0;// 将结点中的数据分开。并返回分隔点。// Range1表示具体的数据值,Range2表示具体的数据点template<class Range1, class Range2>typename boost::range_iterator<Range2>::type splitList (Range1& values, Range2& points){typedef typename boost::range_iterator<Range1>::type iterator1;typedef typename boost::range_iterator<Range2>::type iterator2;iterator1 valuesBegin = boost::begin(values);iterator1 valuesEnd = boost::end(values);//partitionEqually函数是将整个range划分为大小尽可能相等的两部分std::pair<iterator1, iterator2> splitpoint = partitionEqually(zipKeyValuePairs(values,points)).iterators();iterator1 valuesSplitpoint = splitpoint.first;iterator2 pointsSplitpoint = splitpoint.second;if (valuesSplitpoint == valuesEnd) {// partitioning failed, all values are equal :(m_threshold = *valuesBegin;return splitpoint.second;}// We don't want the threshold to be the value of an element but always in between two of them.// This ensures that no point of the training set lies on the boundary. This leeds to more stable// results. So we use the mean of the found splitpoint and the nearest point on the other side// of the boundary.double maximum = *std::max_element(valuesBegin, valuesSplitpoint);m_threshold = 0.5*(maximum + *valuesSplitpoint);return pointsSplitpoint;}//父结点指针BinaryTree* mep_parent;//左孩子结点指针BinaryTree* mp_left;//右孩子结点指针BinaryTree* mp_right;//存储当前结点中数据类标签的列表std::size_t* mp_indexList;//当前结点中数据的个数std::size_t m_size;//以当前结点为根节点的子树的结点个数std::size_t m_nodes;//分隔空间的阈值double m_threshold;};

TreeConstruction类

这个类表示的是树构造的停止条件,停止条件可以是树的高度,或是叶子结点中包含数据的最小个数。该文件的定义位置与BinaryTree是一样的。

class TreeConstruction
{
public:TreeConstruction(): m_maxDepth(0xffffffff), m_maxBucketSize(1){ }TreeConstruction(TreeConstruction const& other): m_maxDepth(other.m_maxDepth), m_maxBucketSize(other.m_maxBucketSize){ }TreeConstruction(unsigned int maxDepth, unsigned int maxBucketSize): m_maxDepth(maxDepth ? maxDepth : 0xffffffff), m_maxBucketSize(maxBucketSize ? maxBucketSize : 1){ }//使树的高度限制减1TreeConstruction nextDepthLevel() const{ return TreeConstruction(m_maxDepth - 1, m_maxBucketSize); }unsigned int maxDepth() const{ return m_maxDepth; }unsigned int maxBucketSize() const{ return m_maxBucketSize; }protected://树的最大深度unsigned int m_maxDepth;//叶子就诶点钟所含数据的最小个数unsigned int m_maxBucketSize;
};

KDTree类

该类定义在<include/shark/Models/Trees/KDTree.h>中。

template <class InputT>
class KDTree : public BinaryTree<I

这篇关于Shark源码分析(十):KNN算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/939113

相关文章

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

Mysql的主从同步/复制的原理分析

《Mysql的主从同步/复制的原理分析》:本文主要介绍Mysql的主从同步/复制的原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录为什么要主从同步?mysql主从同步架构有哪些?Mysql主从复制的原理/整体流程级联复制架构为什么好?Mysql主从复制注意

java -jar命令运行 jar包时运行外部依赖jar包的场景分析

《java-jar命令运行jar包时运行外部依赖jar包的场景分析》:本文主要介绍java-jar命令运行jar包时运行外部依赖jar包的场景分析,本文给大家介绍的非常详细,对大家的学习或工作... 目录Java -jar命令运行 jar包时如何运行外部依赖jar包场景:解决:方法一、启动参数添加: -Xb

Apache 高级配置实战之从连接保持到日志分析的完整指南

《Apache高级配置实战之从连接保持到日志分析的完整指南》本文带你从连接保持优化开始,一路走到访问控制和日志管理,最后用AWStats来分析网站数据,对Apache配置日志分析相关知识感兴趣的朋友... 目录Apache 高级配置实战:从连接保持到日志分析的完整指南前言 一、Apache 连接保持 - 性

Linux中的more 和 less区别对比分析

《Linux中的more和less区别对比分析》在Linux/Unix系统中,more和less都是用于分页查看文本文件的命令,但less是more的增强版,功能更强大,:本文主要介绍Linu... 目录1. 基础功能对比2. 常用操作对比less 的操作3. 实际使用示例4. 为什么推荐 less?5.

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Java集成Onlyoffice的示例代码及场景分析

《Java集成Onlyoffice的示例代码及场景分析》:本文主要介绍Java集成Onlyoffice的示例代码及场景分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 需求场景:实现文档的在线编辑,团队协作总结:两个接口 + 前端页面 + 配置项接口1:一个接口,将o

IDEA下"File is read-only"可能原因分析及"找不到或无法加载主类"的问题

《IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题》:本文主要介绍IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题,具有很好的参... 目录1.File is read-only”可能原因2.“找不到或无法加载主类”问题的解决总结1.File