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

相关文章

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

MySQL表锁、页面锁和行锁的作用及其优缺点对比分析

《MySQL表锁、页面锁和行锁的作用及其优缺点对比分析》MySQL中的表锁、页面锁和行锁各有特点,适用于不同的场景,表锁锁定整个表,适用于批量操作和MyISAM存储引擎,页面锁锁定数据页,适用于旧版本... 目录1. 表锁(Table Lock)2. 页面锁(Page Lock)3. 行锁(Row Lock

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

Redis主从/哨兵机制原理分析

《Redis主从/哨兵机制原理分析》本文介绍了Redis的主从复制和哨兵机制,主从复制实现了数据的热备份和负载均衡,而哨兵机制可以监控Redis集群,实现自动故障转移,哨兵机制通过监控、下线、选举和故... 目录一、主从复制1.1 什么是主从复制1.2 主从复制的作用1.3 主从复制原理1.3.1 全量复制

Python中的随机森林算法与实战

《Python中的随机森林算法与实战》本文详细介绍了随机森林算法,包括其原理、实现步骤、分类和回归案例,并讨论了其优点和缺点,通过面向对象编程实现了一个简单的随机森林模型,并应用于鸢尾花分类和波士顿房... 目录1、随机森林算法概述2、随机森林的原理3、实现步骤4、分类案例:使用随机森林预测鸢尾花品种4.1