Recall that regression aims to predict a numerical outcome. In contrast, classification aims to predict a categorical outcome, that is, assigning an observation to a known category or class.
Classification problems frequently occur in practice. Some examples include:
Email spam detection: spam or not spam
Medical diagnosis: disease or no disease
Fraud detection and loan approval
Image recognition: animals, digits, objects
Sentiment analysis: positive, negative, neutral
k-nearest neighbors
The \(k\)-nearest neighbors (kNN) classifies a new observation by finding its \(k\) closest training data points and applying the majority rule. This is considered as a nonparametric method because it does not involve estimation of parameters in an assumed functional form.
From Dr. Guangliang Chen
The Euclidean distance is a popular distance measure between two numerical observations \((x_1, x_2, \dots, x_p)\) and \((y_1, y_2, \dots, y_p)\)
The number of nearest neighbors \(k\) affects the classification results.
From Dr. Guangliang Chen
A riding-mower manufacturer is interested in classifying families in a city into prospective owners or non-owners on the basis of income (in thousand dollars) and lot size (in 1000 squared feet). A pilot random sample is given in the mowers data set.
Choosing the number of neighbors \(k\) is an important decision. Generally speaking, if \(k\) is too low, we may get misleading signal. If \(k\) is too high, we will not learn the local structure in the data.
In practice, to choose \(k\), we typically split the data into a training set and a validation (test) set. We then try several different values of \(k\), for each of which we train the model, predict the labels of the validation (test) data, and compute the accuracy. The value of \(k\) that gives the highest accuracy on the validation set is typically chosen as the best one.
Consider the iris data set, where the response variable is the flower species and the predictors are the four length and width measurements. Split the dataset into a training set (about 70%) and a test set (about 30%). Using the K-nearest neighbors (KNN) method, fit models with different values of \(k\), such as \(k = 1, 3, 5, 7, 9\).
For each value of \(k\), compute the classification accuracy on the test set.
Which value of \(k\) gives the highest test accuracy?