# Linear regression

Linear regression is a statistical technique used to understand the relationship between variables. It tries to find the best straight line that connects the points on a graph, representing how an independent variable (such as hours of study) influences a dependent variable (such as exam scores). The goal is to minimize the difference between the actual values and those predicted by the line.

Although simple and useful, linear regression can have problems when there are many variables or when the data has noise or strong correlations. This can cause the model to "overfit" the data, meaning it adjusts too much to specific variations and loses the ability to generalize to new data.

The model training is quick, and after training, the dataset is no longer needed to make predictions. It is possible to save and import the finalized training in a very lightweight file.

#### EXAMPLE OF USE

```
uses
  ULinearRegression;
  
procedure ExampleLinearRegression;
var
  vLinearReg: TLinearRegression;
begin
  vLinearReg := TLinearRegression.Create;
  try
    vLinearReg.Train('C:\Delphai\Delphai\Datasets\Housing Price.csv', True);
    ShowMessage('House price: ' + FormatCurr('##0.00', vLinearReg.Predict([2459, 1, 1, 1964, 3.1047807561601664, 0, 4])));
  finally
    vLinearReg.Free;
  end;
end;
```

#### CLASSES AND METHODS GUIDE

TLinearRegression

* *FDataset : TAIDatasetClassification;* : access the current database contained in the model.
* *procedure ClearDataset;* : cleans the dataset.
* *constructor Create;* : creates a new object to be trained.
* *constructor Create(aTrainedFile : String); overload;* : creates the object by loading the structure from an already trained file.
* *procedure Train(aTrainingData : String; aHasHeader : Boolean = True); overload;* : performs the training with the provided data.
  * *aTrainingData* : CSV file path.
  * *aHasHeader* : indicates whether the file has a header.
* *procedure Train(aTrainingData : TDataSet); overload;* : performs the training and generates the nodes based on the provided data.
  * *aTrainingData* : TDataSet object that contains the data that will be used for training.
* *function Predict(aSample: TArray\<Double>; 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).
* *procedure LoadFromFile(const FileName: string);* : loads the trained file.
* *procedure SaveToFile(const FileName: string);* : saves the trained file.
