Predicting Heart Diseases with Machine Learning-阿里云开发者社区

开发者社区> 芷沁> 正文

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.



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:

Field Meaning Type Description
age Age string Age of the subject, in number.
age Age string Age of the subject, in number.
cp Chest pain types string The pain severity from high to low is: typical, atypical, non-anginal and asymptomatic.
trestbps Blood pressure string Blood pressure value.
chol Cholesterol string Cholesterol level.
fbs Fasting blood sugar (FBS) string If FBS > 120 mg/dl, true, otherwise false.
restecg Electrocardiographic results string Whether T wave exists. From mild to severe: norm, hyp.
thalach Maximum heart rate string Maximum heart rate.
exang Exercise induced angina string If the patient has angina, true; otherwise false.
oldpeak ST depression induced by exercise relative to rest string Pressure of the ST segment.
slop The slope of the peak exercise ST segment string The slope of the ST segment. Different degrees of down, flat or up.
ca Number of major vessels colored by fluoroscopy string Number of major vessels colored by fluoroscopy.
thal Defect categories string Categories of complications. From mild to severe: norm, fix, and rev.
status Whether diseased string Whether 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.


如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
10062 0
在应用中,有时会遇到用户询问如何使单台云服务器具备多个公网IP的问题。 具体如何操作呢,有了NAT网关这个也不是难题。
26784 0
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
13875 0
windows server 2008阿里云ECS服务器安全设置
最近我们Sinesafe安全公司在为客户使用阿里云ecs服务器做安全的过程中,发现服务器基础安全性都没有做。为了为站长们提供更加有效的安全基础解决方案,我们Sinesafe将对阿里云服务器win2008 系统进行基础安全部署实战过程! 比较重要的几部分 1.
9157 0
腾讯云服务器 设置ngxin + fastdfs +tomcat 开机自启动
在tomcat中新建一个可以启动的 .sh 脚本文件 /usr/local/tomcat7/bin/ export JAVA_HOME=/usr/local/java/jdk7 export PATH=$JAVA_HOME/bin/:$PATH export CLASSPATH=.
4654 0
阿里云ECS云服务器初始化是指将云服务器系统恢复到最初状态的过程,阿里云的服务器初始化是通过更换系统盘来实现的,是免费的,阿里云百科网分享服务器初始化教程: 服务器初始化教程方法 本文的服务器初始化是指将ECS云服务器系统恢复到最初状态,服务器中的数据也会被清空,所以初始化之前一定要先备份好。
7361 0
4498 0