简体   繁体   中英

Finding k-Nearest-Neighbor in R with knn() from class package

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.

As the documentation states cl is the factor of true classifications of the training set ie your y variable (the third column of your training set).

This means that the function should be as follows:

cl <- factor(c(2,0,2,1)) #or alternatively factor(train[,3])
k <- knn(train[,c(1,2)], test[,c(1,2)], cl)

As you can see in both training and test sets the y variable (the column with the classes) is not included in the test and training sets. The column is only included as a factor in the cl argument.

The error you received is because the number of rows of the training set is not equal to the length of the factor in which case it only had 3 elements (which is because you thought you only needed to specify the levels of the factor there).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM