Predicting Heart Diseases with Machine Learning


Heart disease is a major cause of death, affecting over one-third of the world’s population. In China, hundreds of thousands of people die of heart disease every year. If we can predict and diagnose heart disease in patients, we can reduce the number of deaths cause by heart diseases.

A promising method of screening heart diseases is through data mining. By extracting common physical examination indicators, we can build a reliable prediction model for each patient. This article illustrates how to build a heart disease prediction case through the Alibaba Cloud machine learning platform using real data.

Heart Disease Data Set

Data Source: UCI open-source Heart Disease Data Set

The data set below contains the physical examination data of heart disease patients in an area in the United States, with 303 instances in total. The specific fields are as follows:

FieldMeaningTypeDescriptionageAgestringAge of the subject, in number.ageAgestringAge of the subject, in number.cpChest pain typesstringThe pain severity from high to low is: typical, atypical, non-anginal and asymptomatic.trestbpsBlood pressurestringBlood pressure value.cholCholesterolstringCholesterol level.fbsFasting blood sugar (FBS)stringIf FBS > 120 mg/dl, true, otherwise false.restecgElectrocardiographic resultsstringWhether T wave exists. From mild to severe: norm, hyp.thalachMaximum heart ratestringMaximum heart rate.exangExercise induced anginastringIf the patient has angina, true; otherwise false.oldpeakST depression induced by exercise relative to reststringPressure of the ST segment.slopThe slope of the peak exercise ST segmentstringThe slope of the ST segment. Different degrees of down, flat or up.caNumber of major vessels colored by fluoroscopystringNumber of major vessels colored by fluoroscopy.thalDefect categoriesstringCategories of complications. From mild to severe: norm, fix, and rev.statusWhether diseasedstringWhether diseased. Buff indicates healthy, and sick indicates diseased.

Data Exploration Procedure

The following diagram illustrates the data mining process.

Image 1:

The diagram below illustrates the specific steps required to deploy the data mining process.

Image 2:

Data Pre-processing

Data pre-processing, also called data cleansing, removes data anomalies through data de-noising, missing value insertion, and type conversion operations before the data is used in an algorithm. The input data for this experiment consists of 14 features and a target queue. In this algorithm, the possibility of a user suffering from a heart disease is predicted based on the user’s physical indicators. Because this classification experiment adopts linear logistic regression, all input values are binary.

The table below represents the input data.

Image 3:

We can see that a most data are text descriptions. In the data pre-processing process, we need to map the character string into specific values.

  • Binary data: A majority of recorded data in this example is binary. Binary data is easier to convert and analyze. For example, the fbs field has two values: false and true. We can express false as 0 and true as 1.
  • Multi-valued data: Data such as the cp field, uses multiple values to indicate the pain severity. We can map the severity from low to high with numerical values of 0 to 3.

SQL scripts implement the data pre-processing. For details, refer to SQL script-1 component.

select age,
(case sex when 'male' then 1 else 0 end) as sex,
(case cp when 'angina' then 0 when 'notang' then 1 else 2 end) as cp,
(case fbs when 'true' then 1 else 0 end) as fbs,
(case restecg when 'norm' then 0 when 'abn' then 1 else 2 end) as restecg,
(case exang when 'true' then 1 else 0 end) as exang,
(case slop when 'up' then 0 when 'flat' then 1 else 2 end) as slop,
(case thal when 'norm' then 0 when 'fix' then 1 else 2 end) as thal,
(case status when 'sick' then 1 else 0 end) as ifHealth
from ${t1};

Feature Engineering

Feature engineering includes feature derivation and scale variation. In this example, there are two components responsible for feature engineering: filtering feature selection and normalization.

  • Filtering feature selection: This element is used to determine the impact of each feature on the results and is expressed with comentropy and Gini coefficient. You can view the evaluation report to check the final results.

Image 4:

  • Normalization: Since this experiment uses binary logistic regression for model training, the value of each feature should be normalized to between 0 and 1. The normalization formula is: result = (val-min)/(max-min). The table below represents the normalization results.

Image 5:

Model Training and Prediction

We can train our prediction model by analyzing existing data because we already know whether each patient has heart disease. This process is also known as supervision and learning. The trained model is then used to predict if users suffer from heart disease. The training and prediction process is described as follows:

  • Splitting: First, data is divided into two parts using component splitting. In this experiment, data is split based on a ratio of 7:3 for the training set and the prediction set. The training set data is used in the binary logistic regression component for model training, while the prediction set data is used in the prediction component.
  • Binary Logistic Regression: Logistic regression is a linear model where classification is achieved by calculating the threshold of the result. You can learn more about the specific algorithms online or from books. You can view the trained model after logistic regression in the model tab.

Image 6:

  • Prediction: The two inputs of the prediction component are the model and the prediction set. The prediction result shows the predicted data, actual data, and the probability of different results in each group.
  • Evaluation: The confusion matrix, also known as the error matrix, is used to evaluate the accuracy of the model.

Image 7:

Feature Weight

The prediction model can be further fine-tuned using feature engineering. We can adjust the weight of each feature to produce more accurate results. In our observations:

  • The maximum heart rate achieved (thalach) value has the highest correlation to the possibility of heart disease.
  • The gender of the patient has a low correlation with the development of heart disease.

The image below shows the adjusted weights in our model based on feature engineering. A higher weight value indicates a stronger correlation to the outcome.

Image 8:

Summary and Prediction Results

By using the weighted features provided above, we can achieve a heart disease prediction accuracy of greater than 80 percent. With further research, this model can be used to assist physicians in the prevention and treatment of heart disease.

With the prevalence of heart disease in the modern society, predicting such a disease would not only be a pioneering breakthrough but also be opening the floodgates to a variety of applications in predictive medicine.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store