KNN
K-Nearest Neighbors (KNN) is a regression model that makes decisions based on the proximity of data. Unlike other algorithms, KNN does not perform a formal training process; instead, it uses the entire original dataset (database) at the time of prediction. When new data needs to be predicted, the model calculates the distance between this data and all the points in the database, identifying the K nearest neighbors. The final prediction value is the average (or another aggregation, such as the median) of the values associated with those neighbors.
Because it relies directly on the database during prediction, KNN can consume more memory and become slower as the dataset grows. Despite this, it is an effective choice for problems involving small or medium-sized datasets, where simplicity and local accuracy are desirable.
EXAMPLE OF USE
CLASSES AND METHODS GUIDE
TKNNRegression
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.
K : number of nearest neighbors that will be used to consider the result.
aHasHeader : indicates whether the file has a header.
function Predict(aSample : TAISampleAtr; aInputNormalized : Boolean = False): string; : predicts the value of 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