R question in use of class package and in particular the knn function library(class) pr <- knn(train, valid, cl=cl, k=3,

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

R question in use of class package and in particular the knn function library(class) pr <- knn(train, valid, cl=cl, k=3,

Post by answerhappygod »

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
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply