# Heart Disease: Application of the K-Nearest Neighbor (KNN) Method

<figure><img src="image_1.png" /></figure>

Check for
updates

Diah Puspitasari<fnref n="1" />* ID, Alief Juan Aprian<fnref n="2" /> ID, Erma Delima Sikumbang<fnref n="1" /> ID, Kresna Ramanda<fnref n="1" /> ID,
Sulaeman Hadi Sukmana<fnref n="1" /> ID, Qudsiah Nur Azizah<fnref n="1" /> ID

Corresponding Author Email: diah.puspitasari@bsi.ac.id

Copyright: ©2024 The authors. This article is published by IIETA and is licensed under the CC BY 4.0 license
(http://creativecommons.org/licenses/by/4.0/).

https://doi.org/10.18280/isi.290403

**Received:** 11 March 2024

**Revised:** 9 April 2024

**Accepted:** 2 June 2024

**Available online:** 21 August 2024

**Keywords:**

clustering, heart disease, blood sugar, K-Nearest Neighbor

## ABSTRACT

The increasing prevalence of non-communicable diseases, especially heart disease, in Indonesia highlights the need to improve the effectiveness of medical procedures, especially for patients with heart disease. Data and information from the Ministry of Health of the Republic of Indonesia show that non-communicable diseases, especially heart disease, are the most prevalent diseases and account for 16% of all cases worldwide. The purpose of this study is to analyze the factors associated with the prevalence of heart disease in Indonesia, based on the frequency of gender, age, slope, blood sugar, and chest pain. This study uses the K-Nearest Neighbor algorithm method in data mining with several stages. Dataset, Preprocessing, and Clustering are stages that must be done in this research, this system is to capture patient information to be implemented. This challenge is applied by exploring some initial approaches to obtain the value of K. This is especially true for very large data. The results of this study can contribute to the understanding of data mining-based cardiac data analysis techniques using the K-Nearest Neighbor algorithm to improve the accuracy of diagnosing cardiac patients, with special attention to several types of frequencies that are key in this method. This study also obtained the results of the classification of heart disease datasets with 72.13% based on the maximum K value of KNN through the K-Nearest Neighbor clustering technique.

## 1. INTRODUCTION

Heart disease is a prevalent and serious health condition that affects a significant portion of the population. Various studies have been conducted to understand the epidemiology, risk factors, and implications of heart disease [1]. Heart disease can be caused by a wide range of factors, but major causes of morbidity and death, particularly in the elderly, include blood channel obstruction, inflammation, infection, and congenital abnormalities [1, 2].

Based on information from the Ministry of Health of the Republic of Indonesia's Data and Information Center, the country's projected 2016 population was 258,704,986, with 129,988,690 males and 128,716,296 women. Because there are more people in Indonesia between the ages of 0 and 14 than there are over 14, the country's total population is youthful [2, 3]. In the meantime, the percentage of people over 50 fell dramatically, as would be expected given the large middle-aged population's high death rate [4]. Non-communicable diseases account for the majority of Indonesia's mortality rate (NCDs). They harm and even kill a lot of individuals. The World Health Organization (2020) estimates that 16% of global fatalities are attributable to heart disease. The illness was the main cause of the growth since 2000, going from over 2 million fatalities to 8.9 million deaths in 2019 [5].

Thus, a mechanism must be created to ensure that medical

records—especially those of patients with heart disease—are used as effectively as possible [6]. The system under consideration is a computer-based decision-making system that was created by taking relevant facts from a collection of data and reorganizing it into a different structure [7]. We refer to this system as clustering or data mining. Additional analysis may be conducted using the newly created structure produced by the data mining system [8].

High dimensionality or multi-attribute data is one of the clustering challenges. Because the data points are spread over many dimensions, the data is increasingly dispersed as the dimension grows [9]. Different data mining techniques, including naïve bayes, principal components analysis, decision trees, knearest neighbor, kernel density, bagging algorithms, and support vector machines, exhibit varying degrees of accuracy when it comes to diagnosing heart disease [10].

Three widely used data mining techniques, K-Nearest Neighbor, Naïve Bayes, and K-Means, were applied to a data set in this study. In addition, the results of integrating K-Nearest Neighbor with various initial values and number of clusters in the diagnosis of patients with heart disease are shown in the study by Singh and Rajesh [11]. One of the most popular clustering methods is K-Nearest Neighbor clustering, but the initial value problem is a crucial issue that has a great impact on the results. The use of several initial value

<footnote n="1">¹ Computer Science, Universitas Bina Sarana Informatika, Central Jakarta 10450, Indonesia</footnote>

<footnote n="2">² Informatics Engineering, Universitas Ibn Khaldun Bogor, Bogor 16162, Indonesia</footnote>
approaches, including range, inlier, outlier, random attribute, random row, and random attribute methods, for the k-Nearest Neighbor methodology in the diagnosis of patients with heart disease is shown in this table [12]. The purpose of this study is to present a comprehensive analysis of clustering techniques using the K-Nearest Neighbor method with initial values that have the potential to improve diagnostic accuracy for cardiac patients.

## 2. METHOD

This study employs the frame of mind technique, which is conducted in phases to finish the research. The stages of this research method are shown in Figure 1 and are highly beneficial for researchers in their quest for the truth and a scientific knowledge of phenomena.

<figure><img src="image_2.png" /><figcaption>Figure 1. Research methods</figcaption></figure>

### 2.1 Dataset

A dataset is basically a collection of data. Based on the definition from IBM, a dataset refers to a file that contains one or more records/data. In the data mining process, data alone is not enough. Structured data in the form of datasets is needed so that the results of data analysis will be faster, more accurate, efficient, and of high quality. Therefore, we need data on heart disease (Heart Disease) obtained through Kaggle data. The data will be processed and become an important source of information in this research shown in Figure 2.

<figure><img src="image_3.png" /><figcaption>Figure 2. Visualization of datasets
(Source: https://guides.dataverse.org/en/4.6/user/dataset-management.html)</figcaption></figure>

### 2.2 Preprocessing

Data mining is a sequence of procedures that may be broken down into several phases. Preparing and cleaning the data that

is the subject of knowledge discovery in databases must first be cleaned before beginning the data mining process (KDD) [13]. Eliminating redundant data, examining inconsistent data, and fixing data errors such as printing mistakes are all part of the cleaning process [14]. Additionally, a procedure known as enrichment is carried out, which involves adding additional pertinent data or information such as necessary external data or information to the already-existing data in order to improve it for Knowledge Discovery in Databases (KDD). While the concepts in these phases differ, they are still connected to one another. Data mining is one of the stages that makes up the KDD process as a whole.

a) Selection of data before the Knowledge Discovery in Database (KDD) stage of information mining can start, a collection of operational data must be selected.

b) A file kept apart from the working database contains the chosen data that will be utilized for the data mining procedure.

The procedures involved in preprocessing are depicted in Figure 3 and include data cleansing, integration, transformation, reduction, and discretization.

*   **Data Cleaning:** Data cleaning is the process of locating and fixing or eliminating inaccurate data from a dataset. It is also referred to as data cleansing, data scrubbing, or data cleansing. The cleansed data may contain defects that might impede the next steps in the data analysis process, such as being inconsistent, erroneous, duplicate, or mis formatted.

*   **Data Integration:** The practice of merging data from several sources to provide a single, cohesive perspective is known as data integration.

*   **Data Transformation:** The process of transforming data or information from one format to another is another definition of data transformation. For data processing, data analysis, and application development, data transformation is crucial.

<figure><img src="image_4.png" /><figcaption>Figure 3. Data preprocessing steps</figcaption></figure>
* Data Reduction: The goal of data reduction is to convert unprocessed data into a more focused, usable, and topic-appropriate format. Statistical tools or categories that facilitate data analysis for researchers might help in data reduction.

* Data Discretization: The process of breaking down a large number of data values into smaller ones to make managing and evaluating the data easier is known as data discretization. Put differently, data discretization refers to a technique that minimizes data loss while transforming the attribute values of continuous data into a limited collection of intervals.

### 3. CLUSTERING

According to Darmi and Setiawan, clustering [5]. Clustering, often known as classification, is a technique used to organize data into a few groups according to previously determined similarities. Clusters are collections of similar or different object data inside similar and dissimilar clusters relative to object-data that differ from one other. A subset of objects will be grouped into one or more clusters, so that objects inside one cluster will have a close relationship with one another [15]. The objects are grouped according to the principle of maximizing object similarity in a similar cluster and minimizing object dissimilarity in a different cluster. The object's similarity is usually derived from the attributes that describe the object, hence the object and object data are often represented as a set of variables in a multidimensional space [16]. By using this clustering technique, we may identify regions with low population densities, determine spatial distribution patterns, and identify significant correlations between data attributes. Within data mining, the focus is on finding effective and efficient clustering methods based on large-scale data. A few requirements for clustering in data mining are scalability, the ability to handle different attribute types that can handle high dimensionality, handling noisy data, and ease of handling [17]. The goal of data clustering, however, is to minimize the objective function that is selected throughout the clustering process, which is often concerned with minimizing the variance within a cluster [18]. And minimize inter-cluster variation. Broadly speaking, there are several methods for classifying data. The choice of clustering method depends on the type of data and the clustering goal itself. The clustering method used in this study is called K-Nearest Neighbor [19]. The final results of the previous steps in this research will yield an accuracy value using the K-Nearest Neighbor method.

### 4. RESULT

#### 4.1 Dataset

The public dataset from Kaggle was used for this study; it contains text, numbers, photos, and even combinations of other kinds of data. In a specific context, this data may be utilized to find patterns, test hypotheses, and provide answers to problems. The acquired dataset contains information about heart disease. Table 1 has 303 CSV-formatted data about heart illness.

The data on heart disease, which includes age, gender, blood sugar, slope, and chest discomfort, is shown in the table below that was collected from Kaggle in CSV format.

<table><caption>**Table 1.** Heart disease data</caption><thead><tr><th>No</th><th>Age</th><th>Sex</th><th>Cp</th><th>Trestbps</th><th>Chol</th><th>Fbs</th><th>Restecg</th></tr></thead><tbody><tr><td>1</td><td>63</td><td>1</td><td>3</td><td>145</td><td>233</td><td>1</td><td>0</td></tr><tr><td>2</td><td>37</td><td>1</td><td>2</td><td>130</td><td>250</td><td>0</td><td>1</td></tr><tr><td>3</td><td>41</td><td>0</td><td>1</td><td>130</td><td>204</td><td>0</td><td>0</td></tr><tr><td>4</td><td>56</td><td>1</td><td>1</td><td>120</td><td>236</td><td>0</td><td>1</td></tr><tr><td>5</td><td>57</td><td>0</td><td>0</td><td>120</td><td>354</td><td>0</td><td>1</td></tr><tr><td>6</td><td>57</td><td>1</td><td>0</td><td>140</td><td>192</td><td>0</td><td>1</td></tr><tr><td>7</td><td>56</td><td>0</td><td>1</td><td>140</td><td>294</td><td>0</td><td>0</td></tr><tr><td>8</td><td>44</td><td>1</td><td>1</td><td>120</td><td>263</td><td>0</td><td>1</td></tr><tr><td>9</td><td>52</td><td>1</td><td>2</td><td>172</td><td>199</td><td>1</td><td>1</td></tr><tr><td>10</td><td>57</td><td>1</td><td>2</td><td>150</td><td>168</td><td>0</td><td>1</td></tr><tr><td>11</td><td>54</td><td>1</td><td>0</td><td>140</td><td>239</td><td>0</td><td>1</td></tr><tr><td>12</td><td>48</td><td>0</td><td>2</td><td>130</td><td>275</td><td>0</td><td>1</td></tr><tr><td>13</td><td>49</td><td>1</td><td>1</td><td>130</td><td>266</td><td>0</td><td>1</td></tr><tr><td>14</td><td>64</td><td>1</td><td>3</td><td>110</td><td>211</td><td>0</td><td>0</td></tr><tr><td>15</td><td>58</td><td>0</td><td>3</td><td>150</td><td>283</td><td>1</td><td>0</td></tr><tr><td>16</td><td>50</td><td>0</td><td>2</td><td>120</td><td>219</td><td>0</td><td>1</td></tr><tr><td>16</td><td>58</td><td>0</td><td>2</td><td>120</td><td>340</td><td>0</td><td>1</td></tr><tr><td>18</td><td>66</td><td>0</td><td>3</td><td>150</td><td>226</td><td>0</td><td>1</td></tr><tr><td>19</td><td>43</td><td>1</td><td>0</td><td>150</td><td>247</td><td>0</td><td>1</td></tr><tr><td>20</td><td>69</td><td>0</td><td>3</td><td>140</td><td>239</td><td>0</td><td>1</td></tr><tr><td>21</td><td>59</td><td>1</td><td>0</td><td>135</td><td>234</td><td>0</td><td>1</td></tr><tr><td>22</td><td>44</td><td>1</td><td>2</td><td>130</td><td>233</td><td>0</td><td>1</td></tr><tr><td>23</td><td>42</td><td>1</td><td>0</td><td>140</td><td>226</td><td>0</td><td>1</td></tr><tr><td>24</td><td>61</td><td>1</td><td>2</td><td>150</td><td>243</td><td>1</td><td>1</td></tr><tr><td>25</td><td>40</td><td>1</td><td>3</td><td>140</td><td>199</td><td>0</td><td>1</td></tr><tr><td>26</td><td>71</td><td>0</td><td>1</td><td>160</td><td>302</td><td>0</td><td>1</td></tr><tr><td>27</td><td>59</td><td>1</td><td>2</td><td>150</td><td>212</td><td>1</td><td>1</td></tr><tr><td>28</td><td>51</td><td>1</td><td>2</td><td>110</td><td>175</td><td>0</td><td>1</td></tr><tr><td>29</td><td>65</td><td>0</td><td>2</td><td>140</td><td>417</td><td>1</td><td>0</td></tr><tr><td>30</td><td>53</td><td>1</td><td>2</td><td>130</td><td>197</td><td>1</td><td>0</td></tr><tr><td>31</td><td>41</td><td>0</td><td>1</td><td>105</td><td>198</td><td>0</td><td>1</td></tr><tr><td>32</td><td>65</td><td>1</td><td>0</td><td>120</td><td>177</td><td>0</td><td>1</td></tr><tr><td>33</td><td>44</td><td>1</td><td>1</td><td>130</td><td>219</td><td>0</td><td>0</td></tr><tr><td>34</td><td>54</td><td>1</td><td>2</td><td>125</td><td>273</td><td>0</td><td>0</td></tr><tr><td>35</td><td>51</td><td>1</td><td>3</td><td>125</td><td>213</td><td>0</td><td>0</td></tr><tr><td>36</td><td>46</td><td>0</td><td>2</td><td>142</td><td>177</td><td>0</td><td>0</td></tr><tr><td>37</td><td>54</td><td>0</td><td>2</td><td>135</td><td>304</td><td>1</td><td>1</td></tr><tr><td>38</td><td>54</td><td>1</td><td>2</td><td>150</td><td>232</td><td>0</td><td>0</td></tr><tr><td>39</td><td>65</td><td>0</td><td>2</td><td>155</td><td>269</td><td>0</td><td>1</td></tr><tr><td>40</td><td>65</td><td>0</td><td>2</td><td>160</td><td>360</td><td>0</td><td>0</td></tr><tr><td>41</td><td>51</td><td>0</td><td>2</td><td>140</td><td>308</td><td>0</td><td>0</td></tr><tr><td>42</td><td>48</td><td>1</td><td>1</td><td>130</td><td>245</td><td>0</td><td>0</td></tr><tr><td>43</td><td>45</td><td>1</td><td>0</td><td>104</td><td>208</td><td>0</td><td>0</td></tr><tr><td>44</td><td>53</td><td>0</td><td>0</td><td>130</td><td>264</td><td>0</td><td>0</td></tr><tr><td>45</td><td>39</td><td>1</td><td>2</td><td>140</td><td>321</td><td>0</td><td>0</td></tr><tr><td>46</td><td>52</td><td>1</td><td>1</td><td>120</td><td>325</td><td>0</td><td>1</td></tr><tr><td>47</td><td>44</td><td>1</td><td>2</td><td>140</td><td>235</td><td>0</td><td>0</td></tr><tr><td>48</td><td>47</td><td>1</td><td>2</td><td>138</td><td>257</td><td>0</td><td>0</td></tr><tr><td>49</td><td>53</td><td>0</td><td>2</td><td>128</td><td>216</td><td>0</td><td>0</td></tr><tr><td>50</td><td>53</td><td>0</td><td>0</td><td>138</td><td>234</td><td>0</td><td>0</td></tr><tr><td>......</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td>303</td><td>57</td><td>0</td><td>1</td><td>130</td><td>236</td><td>0</td><td>0</td></tr></tbody></table>

#### 4.2 Preprocessing

Text mining techniques and applications heavily depend on preprocessing procedures. It is the process's initial phase in text mining. The selection data and the selection data findings that will be processed for clustering are the two components of the selection data used in the study by Crone et al. [20].

Table 2 displays the first five rows of the Data Frame data to give an initial view of the dataset structure and counts the number of occurrences of each unique value in the 'target' column and displays the results. This data uses a public dataset on heart disease presented as a CSV file [21].

To normalize the data is done using Min-Max scaling; with
the formula as given below, this will scale all numerical values in the range of 0 to 1.

Figure 4 provides an overview of the class distribution in the target column. Created a bar plot using Seaborn to display the class distribution in the 'target' column. On the x-axis, it has 'target'. This figure also presents the amount of data according to gender, where 0 is data on female gender which is obtained as much as 138 and 1 presents data with male gender as much as 165. In this data, it can be concluded that there are more males than females.

$$X_{\text{normalized}} = \frac{(X - X_{\text{min}})}{(X_{\text{max}} - X_{\text{min}})}$$

### 4.3 Clustering

Clustering or classification is a method used to divide a data set into groups based on predefined similarities [21]. This research uses the K-Nearest Neighbor Algorithm to classify

the risk level of heart disease based on age and gender. Data sources as objects in this study is data taken from the Kaggle.com website. The data used in This study consists of attributes or variables such as gender, age, slope, blood sugar, and chest pain.

Figure 5 visualizes the distribution of count data based on the 'sex' column. This count plot provides information about the number of observations for each 'sex' category (0 for female, 1 for male). The plot does not display a legend and uses the 'mako_r' color palette. The x-axis shows the 'sex' category, and the y-axis shows the number of observations for each category. The program helps provide an initial understanding of the distribution of the 'sex' variable in the dataset.

Figure 6 provides useful information about the gender distribution, summary statistics related to the target, and visualization of the frequency of heart disease by age in the dataset. The figure displays a graph where the largest value of heart disease is in women based on age 58.

<table><caption>**Table 2.** Selection data</caption><thead><tr><th></th><th>Age</th><th>Sex</th><th>Cp</th><th>Trestbps</th><th>Chol</th><th>Fbs</th><th>Restecg</th><th>Target</th></tr></thead><tbody><tr><td>0</td><td>63</td><td>1</td><td>3</td><td>145</td><td>233</td><td>1</td><td>0</td><td>1</td></tr><tr><td>1</td><td>37</td><td>1</td><td>2</td><td>130</td><td>250</td><td>0</td><td>1</td><td>1</td></tr><tr><td>2</td><td>41</td><td>0</td><td>1</td><td>130</td><td>204</td><td>0</td><td>0</td><td>1</td></tr><tr><td>3</td><td>56</td><td>1</td><td>1</td><td>120</td><td>236</td><td>0</td><td>1</td><td>1</td></tr><tr><td>4</td><td>57</td><td>0</td><td>0</td><td>120</td><td>354</td><td>0</td><td>1</td><td>1</td></tr></tbody></table>

<figure><img src="image_5.png" /><figcaption>**Figure 4.** Result of selection data based on target</figcaption></figure>

<figure><img src="image_6.png" /><figcaption>**Figure 5.** Count sex</figcaption></figure>

<figure><img src="image_7.png" /><figcaption>**Figure 6.** Frequency for age</figcaption></figure>
<figure><img src="image_8.png" /><figcaption>**Figure 7.** Maximum heart rate maximum heart rate for age</figcaption></figure>

<figure><img src="image_9.png" /><figcaption>**Figure 8.** Heart disease frequency for slope</figcaption></figure>

In Figure 7, this scatter plot helps visualize the distribution of age and maximum heart rate data for both patient groups. With the red and blue colors and the added legend, this plot makes it easier to interpret the data and understand possible patterns in the relationship between age and maximum heart rate based on heart disease status. The blue color (Not Disease) shows more of the plot.

The bar chart in Figure 8 shows the frequency of occurrence of combinations of values from the 'slope' and 'target' columns, with different colors for each target value. The relationship between the variable 'slope' (slope of the peak exercise ST segment) and the target variable ('target' which may indicate the presence or absence of heart disease). Through the bar chart, it is possible to see how often combinations of slope and

target values occur, as well as a comparison of the frequency between different target values for each slope value. This helps in understanding the pattern of relationship between the two in understanding the pattern of relationship between the two variables.

The bar chart in Figure 9 visualizes the frequency of heart disease by Fasting Blood Sugar (FBS). It also clearly shows the distribution of the presence or absence of heart disease in groups with high and low FBS values. The differentiated colors make it easy to identify patterns, with the first color indicating those without heart disease and the second color indicating those with heart disease.

Figure 10 visualizes the frequency of presence or absence of heart disease based on chest pain type. Through this diagram, we can see the distribution of the number of heart disease cases for each type of chest pain. Different colors are used to distinguish between those with heart disease (first color) and those without heart disease (second color).

<figure><img src="image_10.png" /><figcaption>**Figure 9.** Heart disease frequency according to FBS</figcaption></figure>

<figure><img src="image_11.png" /><figcaption>**Figure 10.** Heart disease frequency according to chest pain type</figcaption></figure>

In [71]:
```
a = pd.get_dummies(data['cp'], prefix = "cp")
b = pd.get_dummies(data['thal'], prefix = "thal")
c = pd.get_dummies(data['slope'], prefix = "slope")
```

In [72]:
```
frames = [data, a, b, c]
data = pd.concat fr ames, axis = 1)
data .head()
```

<table><caption>Out[72]:</caption><thead><tr><th></th><th>age</th><th>sex</th><th>cp</th><th>trestbps</th><th>chol</th><th>fbs</th><th>restecg</th><th>thalach</th><th>exang</th><th>oldpeak</th><th>... cp_1</th><th>cp_2</th><th>cp_3</th><th>thal_0</th><th>thal_1</th><th>thal_2</th><th>thal_3</th><th>slope_0</th><th>slope_1</th><th>slope_2</th></tr></thead><tbody><tr><td>0</td><td>63</td><td>1</td><td>3</td><td>145</td><td>233</td><td>1</td><td>0</td><td>150</td><td>0</td><td>2.3</td><td>... False</td><td>False</td><td>True</td><td>False</td><td>True</td><td>False</td><td>False</td><td>True</td><td>False</td><td>False</td></tr><tr><td>1</td><td>37</td><td>1</td><td>2</td><td>130</td><td>250</td><td>0</td><td>1</td><td>187</td><td>0</td><td>3.5</td><td>... False</td><td>True</td><td>False</td><td>False</td><td>False</td><td>True</td><td>False</td><td>True</td><td>False</td><td>True</td></tr><tr><td>2</td><td>41</td><td>0</td><td>1</td><td>130</td><td>204</td><td>0</td><td>0</td><td>172</td><td>0</td><td>1.4</td><td>... True</td><td>False</td><td>False</td><td>False</td><td>False</td><td>True</td><td>False</td><td>False</td><td>False</td><td>True</td></tr><tr><td>3</td><td>56</td><td>1</td><td>1</td><td>120</td><td>236</td><td>0</td><td>1</td><td>178</td><td>0</td><td>0.8</td><td>... True</td><td>False</td><td>False</td><td>False</td><td>False</td><td>True</td><td>False</td><td>False</td><td>False</td><td>True</td></tr><tr><td>4</td><td>57</td><td>0</td><td>0</td><td>120</td><td>354</td><td>0</td><td>1</td><td>163</td><td>1</td><td>0.6</td><td>... False</td><td>False</td><td>False</td><td>False</td><td>False</td><td>True</td><td>False</td><td>False</td><td>False</td><td>True</td></tr></tbody></table>

5 rows × 25 columns

<caption>**Figure 11.** Frames data categories 1</caption>
In [73]:
```python
data = data.drop col(
    columns = ['cp', 'thal', 'slope'])
data head()
```

Out[73]:

<table><thead><tr><th></th><th>age</th><th>sex</th><th>trestbps</th><th>chol</th><th>fbs</th><th>restecg</th><th>thalach</th><th>exang</th><th>oldpeak</th><th>ca</th><th>... cp_1</th><th>cp_2</th><th>cp_3</th><th>thal_0</th><th>thal_1</th><th>thal_2</th><th>thal_3</th><th>slope_0</th><th>slope_1</th><th>slope_2</th></tr></thead><tbody><tr><td>0</td><td>63</td><td>1</td><td>145</td><td>233</td><td>1</td><td>0</td><td>150</td><td>0</td><td>2.3</td><td>0</td><td>False</td><td>False</td><td>True</td><td>False</td><td>True</td><td>False</td><td>True</td><td>True</td><td>False</td><td>True</td><td>False</td></tr><tr><td>1</td><td>37</td><td>1</td><td>130</td><td>250</td><td>0</td><td>1</td><td>187</td><td>0</td><td>3.5</td><td>0</td><td>False</td><td>True</td><td>False</td><td>False</td><td>False</td><td>True</td><td>False</td><td>True</td><td>False</td><td>False</td><td>False</td></tr><tr><td>2</td><td>41</td><td>0</td><td>130</td><td>204</td><td>0</td><td>0</td><td>172</td><td>0</td><td>1.4</td><td>0</td><td>True</td><td>False</td><td>False</td><td>False</td><td>False</td><td>True</td><td>False</td><td>False</td><td>False</td><td>False</td><td>True</td></tr><tr><td>3</td><td>56</td><td>1</td><td>120</td><td>236</td><td>0</td><td>1</td><td>178</td><td>0</td><td>0.8</td><td>0</td><td>True</td><td>False</td><td>False</td><td>False</td><td>False</td><td>True</td><td>False</td><td>False</td><td>False</td><td>False</td><td>True</td></tr><tr><td>4</td><td>57</td><td>0</td><td>120</td><td>354</td><td>0</td><td>1</td><td>163</td><td>1</td><td>0.6</td><td>0</td><td>False</td><td>False</td><td>False</td><td>False</td><td>False</td><td>True</td><td>False</td><td>False</td><td>False</td><td>False</td><td>True</td></tr></tbody></table>

5 rows × 22 columns

<figure><img src="image_12.png" /><figcaption>**Figure 13.** K-Nearest Neighbor (KNN) value</figcaption></figure>

This program converts the categorical columns in the dataframe into dummy variables using `pd.get_dummies` (Figure 11), which converts each unique category into a column that is aligned with the current awalan. After transforming the columns `"cp," "thal,"` and `"slope"` into dummies, the results are merged back into the original dataframe. This step aims to smooth the data so that it is more compatible with machine learning algorithms, which often need numerical input. Essentially, this tool preprocesses data to convert categorical columns into a format that may be used for further in-depth analysis or machine learning models.

The next step is to delete the original columns `"cp," "thal,"` and `"slope"` from the dataframe data using `data.drop col(
= ['cp', 'thal', 'slope'])` (Figure 12). This is done after transforming the categorical columns `"cp," "thal,"` and `"slope"` into dummy variables and merging them back with the original dataframe. This guarantees that the dataframe keeps only the created fake columns and the remaining, unaltered columns. Because the initial category columns are no longer redundant, the resulting dataframe is clearer and prepared for machine learning analysis or modeling. To confirm that the column deletion was effective and the intended data structure was achieved, the final result displays the first five rows of the dataframe that have been processed.

In Figure 13 showing the visualization results, the program looks for the K value that gives the highest accuracy on the test data. The maximum accuracy result along with the corresponding K value is then printed. Overall, this graph helps in determining the optimal parameters (number of neighbors) for the KNN model being built, with the aim of improving the performance and generalization of the model on

data that has never been seen before [22]. This graph is the end result of several previous steps by displaying this plot can select the k value that gives the highest accuracy score to the KNN model, helping in decision making related to hyperparameter settings.

## 5. CONCLUSIONS

This study examines the clustering algorithm's performance inference using the K-Nearest Neighbor approach on a dataset of heart disease cases. We determined the performance classification results for five distinct types of frequencies in order to illustrate and evaluate the suggested classification technique. According to the experimental results, the frequency of heart disease based on gender is predominantly female; the age-based frequency is 58 years old; the slope at target 1 is higher based on frequency type; the frequency type based on blood sugar increases and leads to heart disease; and the frequency type based on chest pain is more prevalent among women. The K-Nearest Neighbor dimension reduction is used in this clustering method's performance, and it produces a final result graph to visualize the data in a higher-dimensional dataset in the clustering problem. Using the K-Nearest Neighbor clustering approach, we were able to accurately classify the heart disease dataset with 72.13% based on the K-value maximum KNN score.

## REFERENCES

[1] Wartman, W.B., Hellerstein, H.K. (1948). The incidence of heart disease in 2,000 consecutive autopsies. Annals of Internal Medicine, 28(1): 41-65. https://doi.org/10.7326/0003-4819-28-1-41

[2] Purnamasari, D. (2019). The emergence of non-communicable disease in Indonesia. Acta Medica Indonesiana, 50(4): 273-274.

[3] Kushwah, S., Sharma, N., Das, S. (2022). Novel E-Focused crawler and enhanced k-mean (n-gram) clustering technique for automatic classification of attribute level customer healthcare sentiments. Journal of Algebraic Statistics, 13(2): 68-94. https://publishoa.com/index.php/journal/article/view/141/129

[4] Shouman, M., Turner, T., Stocker, R. (2013). Integrating clustering with different data mining techniques in the diagnosis of heart disease. Journal Computer Science and Engineering, 20(1): 1-10.

[5] Darmi, Y.D., Setiawan, A. (2016). Penerapan metode clustering k-means dalam pengelompokan penjualan
produk. Jurnal Media Infotama, 12(2): 148-157.
https://doi.org/10.37676/jmi.v12i2.418

[6] Thangamani, M., Vijayalakshmi, R., Ganthimathi, M., Ranjitha, M., Malarkodi, P., Nallusamy, S. (2020). Efficient classification of heart disease using K-Means clustering algorithm. *International Journal of Engineering Trends and Technology*, 68(12): 48-53.
https://doi.org/10.14445/22315381/IJETT-V68I12P209

[7] Ziasabounchi, N., Askerzade, I.N. (2014). A comparative study of heart disease prediction based on principal component analysis and clustering methods. *Turkish Journal of Mathematics and Computer Science* (TJMCS), 16: 18.

[8] Mirmozaffari, M., Alinezhad, A., Gilanpour, A. (2017). Heart disease prediction with data mining clustering algorithms. *Int'l Journal of Computing, Communications & Instrumentation Engg*, 4(1): 16-19.
https://doi.org/10.15242/ijccie.dir1116009

[9] SundarV, B., Devi, T., Saravanan, N. (2012). Development of a data clustering algorithm for predicting heart. *International Journal of Computer Applications*, 48(7): 8-13.
https://doi.org/10.5120/7358-0095

[10] Irwansyah, E., Pratama, E.S., Ohyver, M. (2020). Clustering of cardiovascular disease patients using data mining techniques with principal component analysis and K-medoids. *Preprints 2020*, 2020080074.
https://doi.org/10.20944/preprints202008.0074.v1

[11] Singh, R., Rajesh, E. (2019). Prediction of heart disease by clustering and classification techniques prediction of heart disease by clustering and classification techniques. *International Journal of Computer Sciences and Engineering*, 7(2): 861-866.
https://doi.org/10.26438/ijcse/v7i5.861866

[12] Ripan, R.C., Sarker, I.H., Hossain, S.M.M., Anwar, M.M., Nowrozy, R., Hoque, M.M., Furhad, M.H. (2021). A data-driven heart disease prediction model through K-means clustering-based anomaly detection. *SN Computer Science*, 2(2): 112.
https://doi.org/10.1007/s42979-021-00518-7

[13] Roostaee, S., Ghaffary, H.R. (2016). Diagnosis of heart disease based on meta heuristic algorithms and clustering methods. *Journal of Electrical and Computer Engineering Innovations (JECEI)*, 4(2): 105-110.
https://doi.org/10.22061/jecei.2016.570

[14] Gárate-Escamila, A.K., El Hassani, A.H., Andrès, E. (2020). Classification models for heart disease prediction using feature selection and PCA. *Informatics in Medicine Unlocked*, 19: 100330.
https://doi.org/10.1016/j.imu.2020.100330

[15] Berger, L.A. (1998). Imaging in the diagnosis of infections in immunocompromised patients. *Current Opinion in Infectious Diseases*, 11(4): 431-436.
https://doi.org/10.1097/00001432-199808000-00006

[16] Mirkin, B. (2005). Clustering for data mining: A data recovery approach. *Chapman and Hall/CRC*.
https://doi.org/10.1201/9781420034912

[17] Fu, Z., Hu, W., Tan, T. (2005). Similarity based vehicle trajectory clustering and anomaly detection. In *IEEE International Conference on Image Processing 2005*, Genova, Italy, pp. 602-605.
https://doi.org/10.1109/ICIP.2005.1530127

[18] Aichelin, U. (2022). Book selection. *Libr. Journal of the Operational Research Society*, 57(3): 332-335.
https://doi.org/10.1057/palgrave.jors.2602163

[19] Jabbar, M.A., Deekshatulu, B.L., Chandra, P. (2013). Classification of heart disease using k-nearest neighbor and genetic algorithm. *Procedia Technology*, 10: 85-94.
https://doi.org/10.1016/j.protcy.2013.12.340

[20] Crone, S.F., Lessmann, S., Stahlbock, R. (2006). The impact of preprocessing on data mining: An evaluation of classifier sensitivity in direct marketing. *European Journal of Operational Research*, 173(3): 781-800.
https://doi.org/10.1016/j.ejor.2005.07.023

[21] Omran, M.G., Engelbrecht, A.P., Salman, A. (2007). An overview of clustering methods. *Intelligent Data Analysis*, 11(6): 583-605.
https://doi.org/10.3233/ida-2007-11602

[22] Song, Y., Huang, J., Zhou, D., Zha, H., Giles, C.L. (2007). Iknn: Informative k-nearest neighbor pattern classification. In *11th European Conference on Principles of Data Mining and Knowledge Discovery in Databases*, Warsaw, Poland, pp. 248-264.
https://doi.org/10.1007/978-3-540-74976-9_25