非参方法-K NearestNeighbor(KNN)
来源:互联网 发布:ps -ef|grep java 编辑:程序博客网 时间:2024/05/22 07:45
非参方法-K NearestNeighbor(KNN)
KNN方法作为一种无参方法,使用起来十分简单,方便。更为重要的是它往往能够得到很好的效果。它既可以应用到分类中,也可以应用到回归中,是一种十分重要的方法。
问题:
给定一组训练数据
问题分析:
如果我们定义一种判断距离远近的函数,那么能够找到给定训练数据中的
参考解决方案:
(1) 定义一种距离函数,求出所有训练数据输入值,即
(2) 找出这些距离值中最小的K个值,对应于
(3) 若所求问题为回归问题(即训练数据的y值为连续的),则
若所求问题为分类问题(即y值是离散的, 且为M分类问题),则
matlab 代码:
%************************************************************% KNN for regression or classification%*************************************************************% the specified parameters are as follows:% X: the input of train datas, it should be n*m Matrix(n is the% nums of the data, while m is the dimensionalities)% y: the output of train datas, t should be n*1 Matrix(n is the% nums of the data)% k: top k nearest neighbors% predict_x: test sample, t should be m*1 Matrix(m is the% dimensionalities)% regression: 1 denotes regression, 0 denotes classification%% Author: Bai Junyang% Email: bjyhappy123@gmail.com%************************************************************function result = KNN(X, y, k, predict_x, regression)[n, m] = size(X);%compute the vector of the distancpredict_X = repmat(predict_x', n, 1);%size(X)%size(predict_X)distance = sum((X - predict_X).^2, 2);%find the top-K index:topIndextopIndex = zeros(k, 1);sort_distance = sort(distance);for i = 1:k topIndex(i) = find(distance == sort_distance(i));end;%compute the resultresult = mean(y(topIndex)); if regression == 0 if result > 0.5 result = 1; else result = 0; end;end;%plot the pointindex = 1:n;index(topIndex) = [];if regression == 1 %plot the predict point plot(predict_x, result, 'ro', 'MarkerSize', 10); hold on; %plot the training data except the top-k data for i = index plot(X(i,:), y(i), 'ko', 'MarkerSize', 5); end; %plot the top-k data for i = 1:k plot(X(topIndex(i),:), y(topIndex(i),:), 'bo', 'MarkerSize', 10); end;else %plot the predict point if result == 0 plot(predict_x(1), predict_x(2), 'ro', 'MarkerSize', 10); hold on; else plot(predict_x(1), predict_x(2), 'r+', 'MarkerSize', 10); hold on; end; %plot the training data except the top-k data for i = index if y(i) == 0 plot(X(i, 1), X(i, 2), 'yo', 'MarkerSize', 5); else plot(X(i, 1), X(i, 2), 'k+', 'MarkerSize', 5); end; end; %plot the top-k data for i = 1:k if y(i) == 0 plot(X(topIndex(i),1), X(topIndex(i),2), 'bo', 'MarkerSize', 10); else plot(X(topIndex(i),1), X(topIndex(i),2), 'b+', 'MarkerSize', 10); end; end; hold off;end
绘出图形:
红色就代表预测点的值,蓝色代表K个邻居,这里K = 5。
与线性回归的对比
测试所用的数据共97组,其中25组用于测试
代码如下:
function [knnError, lrError] = test(k)data = load('D://ex1data1.txt');X_train = data(1:72, 1);y_train = data(1:72, 2);X_test = data(73:97, 1);y_test = data(73:97, 2);%compute the Linear Regression ErrorlrX_train = [ones(72, 1), X_train];w = pinv(lrX_train)*y_train;lrX_test = [ones(25, 1), X_test];lrError = sum((lrX_test * w - y_test).^2);%compute the KNN ErrorknnError = 0;for i = 1:25 knnError = knnError + (y_test(i) - KNN(X_train, y_train, k, X_test(i), 1))^2;end;
若用平方根误差衡量两种方法,则可以得到下表:
从表格可以看出,若不考虑计算量的大小,KNN可以得到与Linear Regression一样好的效果
KNN的评价
优点:
1.KNN算法思路十分简单,容易理解。
2.KNN算法没有训练的过程,不必求解相关参数。
3.在一般情况下,KNN均能取得不错的预测效果
缺点:
1.虽然不用求解参数,但每次预测均需要较大的计算量,若对于样本数量及其庞大,且对预测时间有较高要求的实际问题中,往往不能适用。
2.同时,K的选择也是其中一个问题,K的值过大,很容易导致计算量成倍地增加,但对于误差的减小贡献有限。例如测试例子中,若将k取20,误差也有13.6699,仅比k = 5时降低了2.5左右,但计算量的增加确很大。
3.KNN预测结果十分依赖于样本数据,若样本数据数据与待预测数据相距较远。例如样本数据的X值大部分位于1附近,但预测点的值在100附近,这样的预测结果准确率会大打折扣。
4.在分类问题中,KNN采用“硬划分”的方法,即对于一个2分类问题,其预测结果不是0便是1。不像逻辑回归(Logistic Regression)可以得到预测结果是1或是0的概率,甚至可以设置不同的概率阈值来得到相关的结果。
- 非参方法-K NearestNeighbor(KNN)
- KNN最近邻算法(K-NearestNeighbor)
- KNN(K-NearestNeighbor)算法
- knn(k-NearestNeighbor) with scala
- K最近邻算法(KNN,K-NearestNeighbor)
- K最近邻(kNN,k-NearestNeighbor)
- KNN(k-NearestNeighbor)识别minist数据集
- 《Data Mining》学习——K-邻近(KNN,K-NearestNeighbor)
- KNN-K最近邻(k-NearestNeighbor)分类算法
- KNN(NearestNeighbor)临近算法,自然语言讲解
- 机器学习---最邻近规则分类KNN(k-NearestNeighbor)算法
- k-NearestNeighbor
- 【机器学习实战】第2章 K-近邻算法(k-NearestNeighbor,KNN)
- 最近邻(kNN,k-NearestNeighbor), 贝叶斯(Bayes), BPNN 人工神经网络(BPNN)算法之花的分类
- KNN(K-Nearest Neighbor)
- K-近邻算法(KNN)
- k近邻算法(kNN)
- KNN(K近邻算法)
- Demo7:背景截取图标显示
- SBA--以服务为基础的架构
- npm 淘宝镜像配置一直无效的可能原因
- 广师Android群分享集合大分类
- typeid typeinfo
- 非参方法-K NearestNeighbor(KNN)
- 【leetcode】Array——Search a 2D Matrix(74)
- 文本深度表示模型Word2Vec
- 排序之冒泡排序
- Linux精讲——chattr特殊权限
- 常用网址
- io流处理文件夹复制功能(java代码)
- 道客巴巴免费复制
- 个人机房重构——组合查询及优化