concept k hyperparameter in category R

appears as: k hyperparameter
Machine Learning with R, the tidyverse, and mlr

This is an excerpt from Manning's book Machine Learning with R, the tidyverse, and mlr.

  • Load the iris dataset, and build a kNN model to classify its three species of iris (including tuning the k hyperparameter):
    data(iris)
    
    irisTask <- makeClassifTask(data = iris, target = "Species")
    
    knnParamSpace <- makeParamSet(makeDiscreteParam("k", values = 1:25))
    
    gridSearch <- makeTuneControlGrid()
    
    cvForTuning <- makeResampleDesc("RepCV", folds = 10, reps = 20)
    
    tunedK <- tuneParams("classif.knn", task = irisTask,
                         resampling = cvForTuning,
                         par.set = knnParamSpace,
                         control = gridSearch)
    
    tunedK
    
    tunedK$x
    
    knnTuningData <- generateHyperParsEffectData(tunedK)
    
    plotHyperParsEffect(knnTuningData, x = "k", y = "mmce.test.mean",
                        plot.type = "line") +
                        theme_bw()
    
    tunedKnn <- setHyperPars(makeLearner("classif.knn"), par.vals = tunedK$x)
    
    tunedKnnModel <- train(tunedKnn, irisTask)
  • Load the iris dataset using the data() function, and build a kNN model to classify its three species of iris (including tuning the k hyperparameter).

    In this section, I’ll teach you how to define a kNN learner for regression, tune the k hyperparameter, and train a model so you can use it to predict a continuous variable. Imagine that you’re a chemical engineer trying to predict the amount of heat released by various batches of fuel, based on measurements you made on each batch. We’re first going to train a kNN model on this task and then compare how it performs to a random forest and an XGBoost model, later in the chapter.