Description Usage Arguments Details Value Author(s) References See Also Examples
This function provides a formula interface to the existing
knn()
function of package class
. On top of this type of
convinient interface, the function also allows normalization of the
given data.
1 |
form |
An object of the class |
train |
The data to be used as training set. |
test |
The data set for which we want to obtain the k-NN classification, i.e. the test set. |
norm |
A boolean indicating whether the training data should be previously normalized before obtaining the k-NN predictions (defaults to TRUE). |
norm.stats |
This argument allows the user to supply the centrality and spread
statistics that will drive the normalization. If not supplied they
will default to the statistics used in the function |
... |
Any other parameters that will be forward to the |
This function is essentially a convenience function that provides a
formula-based interface to the already existing knn()
function of package class
. On top of this type of interface it
also incorporates some facilities in terms of normalization of the
data before the k-nearest neighbour classification algorithm is
applied. This algorithm is based on the distances between
observations, which are known to be very sensitive to different scales
of the variables and thus the usefulness of normalization.
The return value is the same as in the knn()
function of package class
. This is a factor of classifications
of the test set cases.
Luis Torgo ltorgo@dcc.fc.up.pt
Torgo, L. (2010) Data Mining using R: learning with case studies, CRC Press (ISBN: 9781439810187).
http://www.dcc.fc.up.pt/~ltorgo/DataMiningWithR
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | ## A small example with the IRIS data set
data(iris)
## Split in train + test set
idxs <- sample(1:nrow(iris),as.integer(0.7*nrow(iris)))
trainIris <- iris[idxs,]
testIris <- iris[-idxs,]
## A 3-nearest neighbours model with no normalization
nn3 <- kNN(Species ~ .,trainIris,testIris,norm=FALSE,k=3)
## The resulting confusion matrix
table(testIris[,'Species'],nn3)
## Now a 5-nearest neighbours model with normalization
nn5 <- kNN(Species ~ .,trainIris,testIris,norm=TRUE,k=5)
## The resulting confusion matrix
table(testIris[,'Species'],nn5)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.