如何将knn模型用于R中的新数据? [英] How do I use knn model for new data in R?

查看:196
本文介绍了如何将knn模型用于R中的新数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在R中编写了一个knn模型.但是,我不知道如何使用输出来预测新数据.

I've just written a knn model in R. However, I don't know how to use the output to predict new data.

# split into train (treino) and test (teste)
treino_index <- sample(seq_len(nrow(iris)), size = round(0.75*nrow(iris)))
treino <- iris[treino_index, ]
teste <- iris[-treino_index, ]

# take a look at the sample
head(treino)
head(teste)

# save specie from later
treino_especie = treino$Species
teste_especie = teste$Species

# exclude species from train and test dataset
treino = treino[-5]
teste = teste[-5]

# runs knn
library(class)
iris_teste_knn <- knn(train = treino, test = teste, cl= treino_especie,k = 3,prob=TRUE) 


# model performance using cross table
install.packages('gmodels')
library('gmodels')
CrossTable(x=teste_especie, y=iris_teste_knn, prop.chisq=FALSE)

如何将其应用于新数据.假设我有一个带有以下参数的物种:Sepal.Length = 5.0,Sepal.Width = 3.3,Petal.Length = 1.3,Petal.Width = 0.1.我怎么知道这是从哪个物种来的?

How do I apply this to new data. Suppose I have a specie with the following parameters: Sepal.Length = 5.0, Sepal.Width = 3.3, Petal.Length = 1.3, Petal.Width = 0.1. How do I know from which specie this come from?

推荐答案

Knn 是一个惰性分类器.像其他分类器(如逻辑回归,基于树的算法等)一样,它不会为以后的预测提供合适的条件. 它适合同时评估.完成性能参数的调整后,将优化的参数与新的测试用例一起提供给knn.使用:

Knn is a lazy classifier. It doesn't creates a fit to predict later, as in case of other classifiers like logistic regression, tree based algorithms etc. It fits and evaluates at the same time. When you are done with tuning of performance parameters, feed the optimized parameters to knn along with new test cases. Use:

  x = c(5.0, 3.3, 1.3, 0.1)                          # test case
  knn(train = treino , test = x , cl= treino_especie, k = 3,prob=TRUE)

这篇关于如何将knn模型用于R中的新数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆