KNN
K-Nearest Neighbors (KNN) is a classification model that makes decisions based on the proximity of data. Unlike other algorithms, KNN does not perform a traditional training process. Instead, it uses the entire original dataset (data base) at the time of prediction. When new data needs to be classified, the model calculates the distance between this data point and all points in the dataset, identifying the K nearest neighbors. The most common class among these neighbors is then assigned to the data point.
Since KNN relies directly on the dataset during prediction, it can consume more memory and become slower as the dataset grows. Despite this, it is an effective choice for problems involving small to medium-sized datasets, where simplicity and local accuracy are desirable.
EXAMPLE OF USE
CLASSES AND METHODS GUIDE
TKNNClassification
FDataset : TAIDatasetClassification; : access the current database contained in the model.
procedure ClearDataset; : cleans the dataset.
constructor Create(aTrainingData : String; aK: Integer; aHasHeader : Boolean = True); or
constructor Create(aTrainingData : TDataSet; aK: Integer); : creates the object with the dataset that will be used for training.
aTrainingData : CSV file path or TDataSet object that contains the data that will be used for training.
aK : number of nearest neighbors that will be used to consider the result.
aHasHeader : indicates whether the file has a header.
function Predict(aSample : TArray<Double>; aInputNormalized : Boolean = False): string; : predicts the best category for the sample.
aSample : sample to be analyzed.
aInputNormalized : it should be set to true only if the input data has already been normalized outside the component (which should be done using the range saved in the object).
Last updated