r - KNN with multidimensional vectors -
let me give background context: have list of 100 vectors, each 1 50 dimensions, , i'd associate first 50 vectors class , last ones class b.
my question is: how can in order apply knn later , library has knn method more appropriate this?
thanks in advance.
one of first options come mind make data.frame list of vectors, create factor indicator , use knn class package.
make data.frame list of vectors
using rbind, make matrix , use as.data.frame function (more examples in question). assuming l list of vectors:
data <- as.data.frame(do.call(rbind, l)) factor indicator
class <- as.factor(c(rep("a", 50), rep("b", 50))) knn classification using class package
in case don't have separate data testing, best way
train.ind <- sample(1:100, 75) # making indexes split data 75% train , 25% test resulting.classes <- knn(train = data[train.ind, ], test = data[-train.ind, ], cl = class) and if have separate train , test data use
resulting.classes <- knn(train = train.data, test = test.data, cl = class) other choices knn might useful - ‘kknn’ package , 'fnn' package., class package seems easiest 1 simple knn classification.
Comments
Post a Comment