# PharmiTech: Addressing Polypharmacy Challenges through AI-Driven Solutions
## 3 Proposed Solution
### Drug Abuse Module
#### 3.2.1 Patient Clustering  
To reliably detect that a patient is abusing a certain substance or group of substances, it is necessary to know the maximum allowed dosage for that patient. Since privacy regulations prevent pharmacies and para-pharmacies from accessing the medical history of the health conditions and required dosages of their patients [27], a pharmacist must gain a deeper understanding of the patient population without having direct access to their sensitive personal data.  
A valuable approach to provide useful information to a pharmacist, without accessing medical data, is to analyze how a purchase being made by a certain patient deviates from the purchases of other similar patients. Therefore, the system must classify a patient into a general type that represents the shared characteristics of many patients. This can be done by applying clustering algorithms to a dataset of many purchases over a period of time, and then training machine learning models to perform the classification for each new patient.  
Within the scope of the ForPharmacy project, the purchases of 200 patients in Portuguese pharmacies from 2020 to 2023 were recorded and organized into a dataset with  
Figure 2: Herb-Drug Interaction detection module.  
over 36 thousand entries. To uphold privacy and maintain confidentiality, all potentially identifiable data of both patients and pharmacies was anonymized, and a unique identifier was generated to distinguish between different anonymous patients. The dataset contains various essential attributes: the National Medication Code (PNMC) used by Portuguese authorities to identify the purchased product, the quantity of the purchased product, the month and year of the purchase, a randomly generated patient identifier, and the gender and age group of the patient.  
Even though the dataset itself did not provide all the required information to analyze shared characteristics, conditions, and medication profiles of the patients, it was possible to use the PNMC to obtain additional attributes of each purchased product from the official database of the Infarmed [28], the Portuguese National Authority of Medicines and Health Products. The obtained attributes were: the Anatomical Therapeutic Chemical (ATC) codes of each active substance within the product, the international name of each active substance, the dosage of each active substance, and the pharmaceutical form of the product.  
In a data preprocessing stage, these additional attributes were used to improve the dataset and provide more valuable insights of the patterns and trends of purchases of the 200 patients. First, the products that contained multiple active substances were split into multiple entries, one for each substance. Then, the correct dosage of each substance was calculated, considering the different quantities and units of measurement of different products. To provide a better generalization to groups of substances with similar characteristics, each active substance was identified by the more general second level of its ATC code, which corresponds to the pharmacological or therapeutic subgroup [29], taking into account their different dosages. Finally, all the information was combined into 200 entries, one per patient, representing their overall purchase behaviour. The entry of each patient contained the total dosages of each second level ATC code for each of the four seasons of the year, as well as the demographic information of the gender and age group.  
By applying the well-established K-Means machine learning algorithm [30] to this improved dataset, the patients could effectively be divided into four clusters, with just a small overlap between them. These clusters achieved a Silhouette Coefficient of 0.82, which is reasonably close to the maximum value of 1. Figure 3 provides a two dimensional representation of the purchase behaviour of the 200 patients, obtained through Principal Component Analysis (PCA) [31] after the clusters were computed.  
Figure 3: Two-dimensional representation of the patient’s behaviour.