ggplot, ggvis의 산점도와 iris에서 k-nn

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

이상

좋은 웹페이지 즐겨찾기