您的位置:首页 > 其它

ML算法-kNN

2014-04-10 15:18 246 查看
前言

借鉴July博客,做一些ML算法的品读,供大家赏玩。

KNN算法

什么是KNN(K-Nearest Neighbor algorithm)

输入:训练数据集。
算法:对新的输入实例,在训练数据集中找到与该实例最近的k个实例。这K个实例用于决定实例的类(看这些点的Class Ranking,就是说多数属于哪个类就是哪个类)。

输出:类别

NN距离度量表示法

前言-远近有别

由于为了找到邻居,所以我们需要知道找到什么是近,什么是远。

欧氏距离

来源:几何学
叙述:

最常见的两点之间或多点之间的距离表示法,又称之为欧几里得度量,它定义于欧几里得空间中,如点 x = (x1,...,xn) 和 y = (y1,...,yn) 之间的距离为:



其二位平面上两点欧氏距离,代码如下:
//unixfy:计算欧氏距离
double euclideanDistance(const vector<double>& v1, const vector<double>& v2)
{
assert(v1.size() == v2.size());
double ret = 0.0;
for (vector<double>::size_type i = 0; i != v1.size(); ++i)
{
ret += (v1[i] - v2[i]) * (v1[i] - v2[i]);
}
return sqrt(ret);
}


曼哈顿距离

来源:城市交通
城市交通的驾车距离。曼哈顿距离也称为城市街区距离(City Block distance)。
From Wikipedia
Taxicab
geometry, considered by Hermann
Minkowski in 19th century Germany, is a form of geometry in
which the usual distance function or metric ofEuclidean
geometry is replaced by a new metric in which the distance between
two points is the sum of the absolute differences of
their Cartesian coordinates.
The taxicab metric is also known as rectilinear distance, L1 distance or

norm (see Lp space), city
block distance, Manhattan distance,
or Manhattan length, with corresponding variations in the name of the geometry.



叙述:
(1)二维平面两点a(x1,y1)与b(x2,y2)间的曼哈顿距离



(2)两个n维向量a(x11,x12,…,x1n)与 b(x21,x22,…,x2n)间的曼哈顿距离



切比雪夫距离(Chebyshev
distance/ Tchebychev distance/chessboard distance)

来源:国际象棋

若将国际象棋棋盘放在二维直角座标系中,格子的边长定义为1,座标的x轴及y轴和棋盘方格平行,原点恰落在某一格的中心点,则从一个位置走到其他位置需要的步数恰为二个位置的切比雪夫距离,因此切比雪夫距离也称为棋盘距离。

玩过国际象棋的朋友或许知道,国王走一步能够移动到相邻的8个方格中的任意一个。那么国王从格子(x1,y1)走到格子(x2,y2)最少需要多少步?。你会发现最少步数总是max( | x2-x1| , | y2-y1|) 步 。有一种类似的一种距离度量方法叫切比雪夫距离。

数学上,切比雪夫距离(Chebyshev
distance)或是L∞度量,是向量空间中的一种度量,二个点之间的距离定义为其各座标数值差的最大值



叙述:
(1)二维平面两点a(x1,y1)与b(x2,y2)间的切比雪夫距离



(2)两个n维向量a(x11,x12,…,x1n)与 b(x21,x22,…,x2n)间的切比雪夫距离



这个公式的另一种等价形式是



闵可夫斯基距离(Minkowski Distance)

来源:
叙述:
The Minkowski distance of order p between
two points



is defined
as:



p取1或2时的明氏距离是最为常用的,p=2即为欧氏距离,而p=1时则为曼哈顿距离。
当p取无穷时的极限情况下,可以得到切比雪夫距离:



标准化欧氏距离 (Standardized Euclidean distance )

来源:

标准化欧氏距离是针对简单欧氏距离的缺点而作的一种改进方案。标准欧氏距离的思路:既然数据各维分量的分布不一样,那先将各个分量都“标准化”到均值、方差相等。至于均值和方差标准化到多少,先复习点统计学知识。

叙述:


假设样本集X的数学期望或均值(mean)为m,标准差(standard deviation,方差开根)为s,那么X的“标准化变量”X*表示为:(X-m)/s,而且标准化变量的数学期望为0,方差为1。

即,样本集的标准化过程(standardization)用公式描述就是:



标准化后的值 = ( 标准化前的值 - 分量的均值 ) /分量的标准差  

经过简单的推导就可以得到两个n维向量a(x11,x12,…,x1n)与 b(x21,x22,…,x2n)间的标准化欧氏距离的公式:  



如果将方差的倒数看成是一个权重,这个公式可以看成是一种加权欧氏距离

马氏距离(Mahalanobis Distance)

来源:计算两个位置样本集的相似度

马氏距离是由印度统计学家马哈拉诺比斯(P.
C. Mahalanobis)提出的,表示数据的协方差距离。它是一种有效的计算两个未知样本集的相似度的方法。与欧氏距离不同的是它考虑到各种特性之间的联系(例如:一条关于身高的信息会带来一条关于体重的信息,因为两者是有关联的)并且是尺度无关的(scale-invariant),即独立于测量尺度。

叙述:


The Mahalanobis distance of an observation

from a group of
observations with mean

and covariance matrix S is
defined as:



Mahalanobis distance (or "generalized squared interpoint distance" for its squared value) can also be defined as a dissimilarity measure between two random vectors

and

of
the samedistribution with the covariance matrix S:



If the covariance matrix is the identity matrix, the Mahalanobis distance reduces to the Euclidean distance. If the covariance matrix is diagonal, then the resulting distance measure is called anormalized Euclidean distance:



where si is the standard deviation of the xi and yi over the sample set.

巴氏距离(Bhattacharyya Distance)

来源:


叙述:


汉明距离(Hamming
distance)

来源:


叙述:


夹角余弦(Cosine)

来源:


叙述:


杰卡德相似系数(Jaccard similarity coefficient)

来源:


叙述:


皮尔逊系数(Pearson
Correlation Coefficient)

来源:


叙述:


Reference

1.借鉴博客:http://blog.csdn.net/v_july_v/article/details/8203674

2.http://zh.wikipedia.org/wiki/%E6%9B%BC%E5%93%88%E9%A0%93%E8%B7%9D%E9%9B%A2

3.http://zh.wikipedia.org/wiki/%E5%88%87%E6%AF%94%E9%9B%AA%E5%A4%AB%E8%B7%9D%E7%A6%BB



KNN算法的实现:KD树

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: