> For the complete documentation index, see [llms.txt](https://delphai.gitbook.io/delphai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://delphai.gitbook.io/delphai/en/documentation/images-and-media/recommendation.md).

# Recommendation

Recommendation systems with Collaborative Filtering help suggest relevant items by analyzing the behavior of different users or similar items. There are two main approaches: user-user and item-item.

For example, imagine you liked a movie on a streaming platform. With the user-user method, the system looks for people with similar tastes to yours and suggests movies they enjoyed. On the other hand, with the item-item method, it may suggest a movie based on what you just watched.

These methods work like an "invisible network of friends," helping you discover new things that perfectly match your interests as a user.

Currently, only the Collaborative Filtering model is available, but other models will be developed soon.

#### EXAMPLE OF USE

```
uses
  URecommender;
  
procedure ExampleRecommender;
var
  vRecommender: TRecommender;
begin
  vRecommender := TRecommender.Create('C:\DelphAI\DelphAI\Datasets\MovieLens-100k\user_item_matrix.csv', 5, 20, TUserScoreAggregationMethod.amWeightedAverage, dmCosine, True, False);
  try
    ShowArray(vRecommender.RecommendFromUser(105));
    ShowArray(vRecommender.RecommendFromItem(4070));
  finally
    vRecommender.Free;
  end;
end;
```

#### CLASSES AND METHODS GUIDE

TRecommender:

* *FDataset : TAIDatasetClassification;* : access the current database contained in the model.
* *procedure ClearDataset;* : cleans the dataset.
* *constructor Create(aMatrixFile : String; aItemsToRecommendCount, aK : Integer; aAggregMethod : TUserScoreAggregationMethod = amWeightedAverage; aDistanceMethod : TDistanceMode = dmCosine; aCalculateItemDistanceOnCreate : Boolean = False; aHasHeader : Boolean = True);* or
* *constructor Create(aMatrix: TDataSet; aItemsToRecommendCount, aK : Integer; aAggregMethod : TUserScoreAggregationMethod = amWeightedAverage; aDistanceMethod : TDistanceMode = dmCosine; aCalculateItemDistanceOnCreate : Boolean = False);* : creates the object with the dataset that will be used for training.
  * *aMatrixFile* : CSV file path.
  * *aMatrix* : TDataSet object that contains the data that will be used for training. See what the structure should look like [here](/delphai/en/documentation/dataset/regression.md).
  * *aItemsToRecommendCount* : maximum amount of items that will be recommended.
  * *aAggregMethod* : users' aggregation method:
    * *amMode* : refers to the most frequent value in a dataset.
    * *amWeightedAverage* : average calculated by assigning different weights to each value in the dataset, based on its importance.
    * *amSimpleSum* : sum of all values in the dataset, without applying any weights or additional factors.
  * *aDistanceMethod* :
    * *dmManhattan* **:** measures the distance between two points by summing the absolute differences in each dimension, as if traveling along straight lines on a grid.
    * *dmEuclidean* **:** calculates the straight-line distance between two points in space, being the square root of the sum of the squares of the differences between the coordinates.
    * *dmCosine* **:** measures the similarity between two vectors based on the cosine of the angle between them, focusing on the direction, not the magnitude.
    * *dmJaccard* **:** evaluates the similarity between sets by dividing the size of the intersection by the size of the union of the sets.
    * *dmPearson* **:** measures the linear correlation between two variables, evaluating the strength and direction of the relationship between them.
  * *aCalculateItemDistanceOnCreate* : defines if the item distance matrix will be calculated at creation time, used only for item-based recommendation. If not calculated at creation, it will be computed during the first item prediction.
  * *aHasHeader* : indicates whether the file has a header.
* *function RecommendFromItem(aItemID: Integer): TArray;* : recommends X items based on a given registered item.
* *function RecommendFromItem(aItemInfo: TArray; aIDSearch : Integer = -1): TArray;* : recommends X items based on the data of an item that is not in the dataset.
* *function RecommendFromUser(aUserID: Integer): TArray;* : recommends X items based on a given registered user.
* *function RecommendFromUser(aUserInfo: TArray; aIDSearch : Integer = -1): TArray;* : recommends X items based on the data of a user who is not in the dataset.
