如何使用 knn 模型处理 R 中的新数据?
How do I use knn model for new data in R?
我刚刚用 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。我怎么知道这来自哪个物种?
Knn 是一个惰性分类器。它不会像其他分类器(如逻辑回归、基于树的算法等)那样为以后的预测创建拟合。
它同时适合和评估。完成性能参数调整后,将优化后的参数与新的测试用例一起提供给 knn。使用:
x = c(5.0, 3.3, 1.3, 0.1) # test case
knn(train = treino , test = x , cl= treino_especie, k = 3,prob=TRUE)
我刚刚用 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。我怎么知道这来自哪个物种?
Knn 是一个惰性分类器。它不会像其他分类器(如逻辑回归、基于树的算法等)那样为以后的预测创建拟合。 它同时适合和评估。完成性能参数调整后,将优化后的参数与新的测试用例一起提供给 knn。使用:
x = c(5.0, 3.3, 1.3, 0.1) # test case
knn(train = treino , test = x , cl= treino_especie, k = 3,prob=TRUE)