R question in use of class package and in particular the knn function library(class) pr <- knn(train, valid, cl=cl, k=3,
Posted: Mon Jul 11, 2022 11:23 am
R question in use of class package and in particular the knnfunction
library(class)pr <- knn(train, valid, cl=cl, k=3, prob=FALSE) pr [1] 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 00 0 0 0 [30] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 00 ...[1944] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00[1973] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00Levels: 0 1
str(pr) Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 2 1 1 ...
How can I put the classifications into a variable? It can bedone for the probabilities using attr function as seen in the nextexample? How to put the factor levels into a variable?library(class)pr <- knn(train, valid, cl=cl, k=3, prob=TRUE) pr str(pr) Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 2 1 1 ... - attr(*, "prob")= num [1:2000] 1 1 1 1 1 ...
var <- attr(pr,"prob")head(var)[1] 1 1 1 1 1 1
library(class)pr <- knn(train, valid, cl=cl, k=3, prob=FALSE) pr [1] 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 00 0 0 0 [30] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 00 ...[1944] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00[1973] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00Levels: 0 1
str(pr) Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 2 1 1 ...
How can I put the classifications into a variable? It can bedone for the probabilities using attr function as seen in the nextexample? How to put the factor levels into a variable?library(class)pr <- knn(train, valid, cl=cl, k=3, prob=TRUE) pr str(pr) Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 2 1 1 ... - attr(*, "prob")= num [1:2000] 1 1 1 1 1 ...
var <- attr(pr,"prob")head(var)[1] 1 1 1 1 1 1