机器学习—k近邻(kNN)学习方法及R语言程序实例

本节介绍机器学习中我认为最简单的但是却经常很实用的一种监督学习方法——k近邻学习。 kNN是一种分类方法,全称k-Nearest Neighbor,顾名思义,是对于给定的测试样本和…

机器学习—k近邻(kNN)学习方法及R语言程序实例

本节介绍机器学习中我认为最简单的但是却经常很实用的一种监督学习方法——k近邻学习。

kNN是一种分类方法,全称k-Nearest Neighbor,顾名思义,是对于给定的测试样本和基于某种度量距离的方式下,通过最靠近的k个训练样本来预测当前样本的分类结果。其中预测的方式也很简单,就是投票,至于投票方式可以根据工程中的实际情况来决定,可以单纯的计数,当然也可以加权。

kNN算法并不存在训练过程,其实就是单纯的记录训练样本,在需要预测的时候做计算即可,虽然很简单,但在历史上影响力也是极大的。

机器学习—k近邻(kNN)学习方法及R语言程序实例

借用百度一张图来说明kNN算法过程,加入要预测图中Xu的分类结果。就预设一个距离值,我们只考虑以Xu为圆心以这个距离值为半径的圆内的已知训练样本,然后根据这些样本的投票结果来预测Xu属于w1类别,投票结果是4:1。

以上就是kNN方法的简单的不能再简单的原理了。而真正要考察kNN算法的效果如何,根据学习,kNN方法的泛化错误率不超过贝叶斯分类器的错误率两倍,对于如此简单的方法来说,已经很好了。因而在工程中,kNN是在某些情况下非常常用的方法,个人在做一些项目中的推荐方法的时候就会使用kNN这种比较白痴的方法,因为推荐往往是成批的,所以精准率其实是很高的。

机器学习—k近邻(kNN)学习方法及R语言程序实例

下面讲R语言中kNN的使用。R语言中可以下载kknn包并引入,这个过程在RStudio上完成非常简单。

机器学习—k近邻(kNN)学习方法及R语言程序实例

机器学习—k近邻(kNN)学习方法及R语言程序实例

Tools->Install Packages->输入kknn点击Install即可。如果使用命令为install.packages(“kknn”),完成的工作相同。写代码前记得要写library(kknn)导入包

kNN实例1:

## the first example

data(iris)

m <- dim(iris)[1]

val <- sample(1:m, size = round(m/3), replace = FALSE, prob = rep(1/m, m))

iris.learn <- iris[-val,]

iris.valid <- iris[val,]

iris.kknn <- kknn(Species~., iris.learn, iris.valid, distance = 1, kernel = “triangular”)

summary(iris.kknn)

fit <- fitted(iris.kknn)

table(iris.valid$Species, fit)

详细解释实例1:

机器学习—k近邻(kNN)学习方法及R语言程序实例

iris数据格式

iris数据集包括150条数据,其中前四条是属性,Species为分类结果(有监督学习),分类包括setosa versicolor virginica三类。

iris.learn, iris.valid为将iris数据集分为训练集和验证集,比例为2/3和1/3。

kknn函数有很多参数,其中主要的就是我们使用的几个,截取R语言kknn的说明如下图:

机器学习—k近邻(kNN)学习方法及R语言程序实例

其中我们用到的formula是预测结果对应的分类公式,train是训练集,test是验证集或测试集,distance是最主要的kNN的输入参数,表示预设被考虑的距离范围,通过调整它可以改变kNN预测效果,kernal是使用的核函数,核函数后面学习到图像部分的时候再细致学习。

summary(iris.kknn)观察执行结果如下,他给出了kNN投票结果:

机器学习—k近邻(kNN)学习方法及R语言程序实例

fit <- fitted(iris.kknn)

table(iris.valid$Species, fit)” 用来展示整体的预测结果,这是实验过程中最有用的函数,执行结果为:

机器学习—k近邻(kNN)学习方法及R语言程序实例

其中第三行第二列表示有两个virginica类别的样本被预测成了versicolor,其它的全部预测正确。

kNN实例2:

## the second example

data(ionosphere)

ionosphere.learn <- ionosphere[1:200,]

ionosphere.valid <- ionosphere[-c(1:200),]

fit.kknn <- kknn(class ~ ., ionosphere.learn, ionosphere.valid)

table(ionosphere.valid$class, fit.kknn$fit)

(fit.train1 <- train.kknn(class ~ ., ionosphere.learn, kmax = 15,

kernel = c(“triangular”, “rectangular”, “epanechnikov”, “optimal”), distance = 1))

table(predict(fit.train1, ionosphere.valid), ionosphere.valid$class)

(fit.train2 <- train.kknn(class ~ ., ionosphere.learn, kmax = 15,

kernel = c(“triangular”, “rectangular”, “epanechnikov”, “optimal”), distance = 2))

table(predict(fit.train2, ionosphere.valid), ionosphere.valid$class)

第二个实例留给大家自己实践,原理都一样~

That doesn’t mean Auburn can’t find a running game, and they’ll have to if they want to beat Clemson. So it was with concern that I received complaints that celebrated sports commentator Frank Deford had offended some African Americans in successive weekly Morning Edition commentaries. This was her response to a Tweet sent by Sanders daughter, Deiondra, which said Pilar was a “Gold digging he.” Only adding fuel to the fire, Deion stated that the statement wasn’t defamation because it’s true.. Gamblin Matt and the Odds plus Winner: Tampa Bay are $1.63 favourites here to win this series while the Red Wings are $2.34 outsiders. Another advantage of college football is that the bookies sometime make mistakes; your job is to spot these and cash in on them. Arkansas falls under the sway of the Dallas Cowboys, according to a Facebook map of NFL fans based on likes of team pages.. Dinner is at the Royal Table restaurant, where your special guest cheap nfl jerseys is none other than Cinderella herself, who is practically obligated to indulge your child’s every fantastical question, and begrudgingly tolerate your thinly veiled innuendos. Going into the full twist. Houston has just 25 offensive plays of at least 20 yards, tied for last in the league (Denver). I know you’re wondering how this affects you if you are one of the millions of people who get cable from any of these companies. I think that’s why cheap nfl jerseys it persists, to some degree.”. Miami Heat Hoodies Miami Heat players make a political statement by donning hoodies in protest over the 2012 shooting death of black teenager Trayvon Martin, who was unarmed, in Sandford, Florida. You can also create your own Cowboys themed stencils and stamps by cutting the star logo or helmet shape out of contact paper or craft foam, respectively. So I am going to come over on the side just so you can see him switching leads. The Road tells us that even the good guy isn’t necessarily hockey jerseys a “good guy” cheap football jerseys china when he’s doing what he thinks is necessary to survive, and that literally everyone else is a rapist or oakleys outlet a cannibal or both.. It seemed the only way to take the city was a costly and brutal attack. That’s right: Taking a retaliatory attack ad out against a single, average citizen in a random Starbucks sounds completely shithouse crazy, but that’s precisely what Scott did. Differences in measured NFLT were most strongly correlated with posterior subcapsular cataract (average thickness, P=0.01).. College of the Albemarle, established in 1960, is a two year public institution in northeast North Carolina. I left there when I was in high school and haven’t been back since. The couple spent the New Year holidays on the Caribbean island and were repeatedly seen kissing on a sunlounger outside the property and strolling hand in hand in the streets around her getaway apartment.

为您推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注

返回顶部
本页面加载共:43 次查询 | 用时 0.683 秒 | 消耗 53.64MB 内存