【发布时间】:2018-11-10 22:41:39
【问题描述】:
我刚刚在 R 中编写了一个 knn 模型。但是,我不知道如何使用输出来预测新数据。
# 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。我怎么知道它来自哪个物种?
【问题讨论】:
标签: r classification knn