k最近邻

标签: opencv k最近邻
6人阅读 评论(0) 收藏 举报
分类:

K-Nearest Neighbors 
该算法存储所有的训练样本(已知标签),然后通过分析新给的样本(标签未知)与已知标签的训练样本的相似度,选出其中的K个最相似的训练样本进行投票得到新样本的标签,并计算加权和等。 该方法有时被称为是“learning by example”,因为他总是根据新样本的特征向量与已知标签的样本特征向量的相似度来判断新样本的类别。

CvKNearest 
class CvKNearest : public CvStatModel 
该类实现了 K-Nearest Neighbors 模型

CvKNearest::CvKNearest

构造函数

默认构造函数. 

Default and training constructors.
C++: CvKNearest::CvKNearest()
C++: CvKNearest::CvKNearest(const Mat& trainData, const Mat& responses, const Mat& sampleIdx=Mat(), bool isRegression=false, int max_k=32 )
C++: CvKNearest::CvKNearest(const CvMat* trainData, const CvMat* responses, const CvMat* sampleIdx=0, bool isRegression=false, int max_k=32 )

训练函数
CvKNearest::trainC++: bool CvKNearest::train(const Mat& trainData, const Mat& responses, const Mat& sampleIdx=Mat(), bool isRegression=false, int maxK=32, bool updateBase=false )
C++: bool CvKNearest::train(const CvMat* trainData, const CvMat* responses, const CvMat* sampleIdx=0, bool is_regression=false, int maxK=32, bool updateBase=false)
Python: cv2.KNearest.train(trainData, responses[, sampleIdx[, isRegression[, maxK[, updateBase]]]]) ---> retval

参数:

    isRegression – Type of the problem: true for regression and false for classification.
    maxK – Number of maximum neighbors that may be passed to the method CvKNearest::find_nearest()
    updateBase – Specifies whether the model is trained from scratch (update_base=false), or it is updated using the new training data (update_base=true). In the latter case, the parameter maxK must not be larger than the original value.

The method trains the K-Nearest model. It follows the conventions of the generic CvStatModel::train() approach with the following limitations:

    • Only CV_ROW_SAMPLE data layout is supported.
    • Input variables are all ordered.
    • Output variables can be either categorical (
is_regression=false ) or ordered ( is_regression=true ).

    • Variable subsets (var_idx) and missing measurements are not supported.


找到邻居并预测输入向量的响应

CvKNearest::find_nearest

Finds the neighbors and predicts responses for input vectors.
C++: float CvKNearest::find_nearest(const Mat& samples, int k, Mat* results=0, const float** neighbors=0, Mat* neighborResponses=0, Mat* dist=0 ) const
C++: float CvKNearest::find_nearest(const Mat& samples, int k, Mat& results, Mat& neighborResponses, Mat& dists) const
C++: float CvKNearest::find_nearest(const CvMat* samples, int k, CvMat* results=0, const float** neighbors=0, CvMat* neighborResponses=0, CvMat* dist=0) const
Python: cv2.KNearest.find_nearest(samples, k[, results[, neighborResponses[, dists]]]) ----> retval, results, neighborResponses, dists

Parameters
samples
– Input samples stored by rows. It is a single-precision floating-point matrix of number_of_samples × number_of_features size
k – Number of used nearest neighbors. 
results – Vector with results of prediction (regression or classification) for each input sample. It is a single-precision floating-point vector with number_of_samples elements.
neighbors – Optional output pointers to the neighbor vectors themselves. It is an array of k*samples->rows pointers.
neighborResponses – Optional output values for corresponding neighbors. It is a singleprecision floating-point matrix of number_of_samples × k size.
dist – Optional output distances from the input vectors to the corresponding neighbors. It is a single-precision floating-point matrix of number_of_samples × k size.
For each input vector (a row of the matrix
samples), the method finds the k nearest neighbors. In case of regression,
the predicted result is a mean value of the particular vector’s neighbor responses. In case of classification, the class is
determined by voting.
For each input vector, the neighbors are sorted by their distances to the vector.
In case of C++ interface you can use output pointers to empty matrices and the function will allocate memory itself.
If only a single input vector is passed, all output matrices are optional and the predicted value is returned by the method.
The function is parallelized with the TBB library.


例程:

Ptr<ml::KNearest>  knn(ml::KNearest::create());
Mat_<float> trainFeatures(6,4);
trainFeatures << 2,2,2,2,
                 3,3,3,3,
                 4,4,4,4,
                 5,5,5,5,
                 6,6,6,6,
                 7,7,7,7;

Mat_<int> trainLabels(1,6);
trainLabels << 2,3,4,5,6,7;

knn->train(trainFeatures, ml::ROW_SAMPLE, trainLabels);

Mat_<float> testFeature(1,4);
testFeature<< 3,3,3,3;

int K=1;
Mat response,dist;
knn->findNearest(testFeature, K, noArray(), response, dist);
cerr << response << endl;
cerr << dist<< endl;

查看评论

k最近邻分类算法

  • 2012年03月13日 11:12
  • 48KB
  • 下载

基于K最近邻的支持向量机快速训练算法.pdf

  • 2009年05月01日 11:01
  • 221KB
  • 下载

K最近邻分类算法 最简单的分类算法

  • 2010年06月09日 11:08
  • 17KB
  • 下载

KNN (K-最近邻分类)算法源代码

  • 2010年01月29日 16:33
  • 290KB
  • 下载

邻近算法(k-Nearest Neighbor algorithm,k最近邻算法)

下图中,绿色圆要被决定赋予哪个类,是红色三角形还是蓝色四方形?如果K=3(即实线圆内部),由于红色三角形所占比例为2/3,绿色圆将被赋予红色三角形那个类,如果K=5(即虚线圆内),由于蓝色四方形比例为...
  • whycold
  • whycold
  • 2012-06-23 15:05:35
  • 4152

K最近邻分类算法(KNN)

1、基本思想:给定一个测试样本,计算它与训练集中每个对象的距离,圈定距离最近的k个训练对象作为其最近邻,然后使用这k个最近邻中出现次数最多的类标号作为测试样本的类标号值。 每个样本可以表示为(x,x...
  • Algorithmguy
  • Algorithmguy
  • 2017-02-07 09:46:54
  • 783

knn算实现源码

  • 2014年03月30日 13:55
  • 14.28MB
  • 下载

KNN算法原理和java实现.doc

  • 2013年03月03日 11:47
  • 7KB
  • 下载

[机器学习]K近邻算法及其应用--WEKA工具

K邻近算法 weka
  • u010536377
  • u010536377
  • 2015-12-16 14:27:40
  • 4333

最近邻法和k-近邻法 KD树

最近邻法和k-近邻法   下面图片中只有三种豆,有三个豆是未知的种类,如何判定他们的种类?   提供一种思路,即:未知的豆离哪种豆最近就认为未知豆和该豆是同一种类。由此,我们引出最...
  • u012422446
  • u012422446
  • 2017-02-22 15:33:14
  • 3619
    个人资料
    持之以恒
    等级:
    访问量: 2万+
    积分: 551
    排名: 8万+
    最新评论