sh ze
sh ze

Reputation: 169

What does cl parameter in knn function in R mean?

I'm using R to implement knn. The knn function in 'class' package contains a parameter called cl:

knn(train, test, cl, k = 1, l = 0, prob = FALSE, use.all = TRUE)

It is written in the package documentation that cl is a factor of true classifications of training set. I don't know that means! could any one help?

Upvotes: 3

Views: 13306

Answers (2)

HelloWorld
HelloWorld

Reputation: 725

As you point out CL stands for classifications. CL should contain the categories of the response variables belonging to the training test. If one defines X as the independent variables and Y as the independent variable and then defines a training and a test set from the two, than knn should be called as:

train <- sample(1 : dim(data)[1], round(dim(data)[1] / 3 * 2))
trainX <- data[train, ]
testX <- data[-train, ]
trainCl <- factor(data[train, "classifications"])
testCl <- factor(data[-train, "classifications"])
knnPred <- knn(trainX, testX, trainCl, k=1)
# confusion matrix
table(knnPred, testCl)

Upvotes: 3

Sotero Alvarado
Sotero Alvarado

Reputation: 31

Suppose you are trying to predict outcome y. The cl is the y values for the training set. Check out the link below. Notice that when they define the cl=iris.trainLabels they use the 5th column of the iris data set which is species. So cl is the species type. Then the knn algorithm predicts the species type.

http://blog.datacamp.com/machine-learning-in-r/

Upvotes: 3

Related Questions