This is home work.
I have a 2 matrices, 1 for training and 1 for test. The data has two columns of data which shall be used for classification and a third column with the known class. Both matrices has the third column.
[1] [2] [3]
[1] 6.4 0.32 2
[2] 4.8 0.34 0
[3] 4.9 0.25 2
[4] 7.2 0.32 1
where the intergers are the classes (from 0-2).
The dimension for my datasets are 100 3 for the training set and 38 3 for the testing set.
I have tried to use the knn() function of the class lbrary.
knn uses the follwing arguments: (train, test, cl, k = 1, l = 0, prob = FALSE, use.all = TRUE)
I have tried to use my data sets directly, but then i get the error: "'train' and 'class' have different lengths"
I have tried a few thing, but I am stuck after some hours now. At the moment I have this code in my editor:
cl <- t(factor(c(rep("0",1), rep("1",1), rep("2",1))))
k <- knn(train, test, cl)
but it does not work. Can someone help me?
I want to find run the function with 3 different k-values, and find the accuracy of each. After that I will be 5-fold cross validating the best k.