k-nnは完全におまけ。
お気に入りの散布図コードを備忘録として残す。
本題
library(ggplot2)
library(ggvis)
library(class)
head(iris)
names(iris) <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
iris %>% ggvis(~Sepal.Length, ~Sepal.Width, fill = ~Species) %>% layer_points()
iris %>% ggvis(~Petal.Length, ~Petal.Width, fill = ~Species) %>% layer_points()
k-NN
set.seed(1234)
ind <- sample(2, nrow(iris), replace=TRUE, prob=c(0.67, 0.33))
iris.training <- iris[ind==1, 1:4]
iris.test <- iris[ind==2, 1:4]
iris.trainLabels <- iris[ind==1,5]
iris.testLabels <- iris[ind==2, 5]
iris_pred <- knn(train = iris.training, test = iris.test, cl = iris.trainLabels, k=3)
iris_pred
> iris_pred
[1] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa
[11] setosa setosa versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor
[21] versicolor versicolor versicolor versicolor virginica virginica virginica virginica versicolor virginica
[31] virginica virginica virginica virginica virginica virginica virginica virginica virginica virginica
Levels: setosa versicolor virginica