<jupyter_start><jupyter_text>UCI Heart Disease Exploratory Data Analysis and Data Visualization.  Importing required Libraries<jupyter_code>import numpy as np
import pandas as pd
import pandas_profiling

# for data visualizations
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

#To ignore unncessary deprecation warnings
import warnings
warnings.filterwarnings('ignore')
# reading the data
data = pd.read_csv('heart.csv')
data.shape<jupyter_output><empty_output><jupyter_text>Exploratory Data Analysis<jupyter_code>#checking for missing values
data.isnull().sum()<jupyter_output><empty_output><jupyter_text>- There are no null values in the dataset  Data Description * age: The person's age in years* sex: The person's sex (1 = male, 0 = female)* cp: The chest pain experienced (Value 0: typical angina, Value 1: atypical angina, Value 2: non-anginal pain, Value 3: asymptomatic)* trestbps: The person's resting blood pressure (mm Hg on admission to the hospital)* chol: The person's cholesterol measurement in mg/dl* fbs: The person's fasting blood sugar (> 120 mg/dl, 1 = true; 0 = false)* restecg: Resting electrocardiographic measurement (0 = normal, 1 = having ST-T wave abnormality, 2 = showing probable or definite left ventricular hypertrophy by Estes' criteria)* thalach: The person's maximum heart rate achieved* exang: Exercise induced angina (1 = yes; 0 = no)* oldpeak: ST depression induced by exercise relative to rest ('ST' relates to positions on the ECG plot.)* slope: the slope of the peak exercise ST segment (Value 1: upsloping, Value 2: flat, Value 3: downsloping)* ca: The number of major vessels (0-3)* Thal: Result of the thallium stress test 3 = normal; 6 = fixed defect; 7 = reversible defect* target: Heart disease (0 = no, 1 = yes)<jupyter_code>#concise summary of the data
data.info()
data.head(5)
#various measures of central tendency of the dataset
data.describe()<jupyter_output><empty_output><jupyter_text>Data Visualizations >Now, we can use visualizations to better understand our data to further help us in preprocessing the data and understanding the relations between various features and the target  Heatmap to understand the correlations amongst the different attributes in the dataset<jupyter_code># Drawing a heat map

plt.rcParams['figure.figsize'] = (17, 9)
plt.style.use('ggplot')
sns.set(font_scale=1.4)
sns.heatmap(data.corr(), annot = True, annot_kws={"size": 15}, fmt='.2f', cmap = 'YlGnBu', linewidths=1, linecolor='gray')
plt.title('Heatmap for the Dataset', fontsize = 20, fontweight=30)
plt.yticks(rotation=0)
plt.xticks(rotation=90)
plt.show()<jupyter_output><empty_output><jupyter_text>> It can be seen that almost all of the features/attributes given in the dataset are very less correlated with each other. This implies we must include all of the features, as we can only eliminate those features where the correlation of two or more features are very high.  DIstributions of the attributes<jupyter_code># checking the distribution of the featurres in the dataset

plt.figure(figsize=(20,20))
data.hist(figsize=(20,20))
plt.show()<jupyter_output><empty_output><jupyter_text>>From the histograms above, Each feature has a different range of distribution. Thus, Normalizing the data before our predictions should be of great use. Also note that the categorical features do stand out. >It's always a good practice to work with a dataset where the target classes are of approximately equal size. Thus, let's check for the same.<jupyter_code># plotting the target attribute  

disease = len(data[data['target'] == 1])
non_disease = len(data[data['target'] == 0])
plt.pie(x=[disease, non_disease], explode=(0, 0), labels=['Have Disease ', 'Haven\'t disease'],autopct='%1.2f%%' ,
        shadow=True, startangle=90,colors = ['#ffac81','#aec6cf'])
fig = plt.gcf()
fig.set_size_inches(6,6)
plt.show()<jupyter_output><empty_output><jupyter_text>> Looking at the Distribution of Target, The dataset is quite balanced with almost equal no. of Positive and Negative Classes. The two classes are not exactly 50% each but the ratio is good enough to continue without dropping/increasing our data.  Checking how the target values depend on various features<jupyter_code># tresbps vs target

plt.rcParams['figure.figsize'] = (10, 6)
sns.boxplot(data['target'], data['trestbps'], palette = 'pastel')
plt.title('Relation of Resting Blood pressure with target', fontsize = 20)
plt.xlabel('Target')
plt.xticks([0,1], ['Haven\'t disease','Have Disease'])
plt.ylabel('Resting Blood pressure')
plt.show()<jupyter_output><empty_output><jupyter_text>>The above Bivariate plot between tresbps (the resting blood pressure of a patient) and the target clearly suggests that the patients who are most likely to not suffer from the disease have a slighly greater blood pressure than the patients who have heart diseases.<jupyter_code># cholestrol vs target

plt.rcParams['figure.figsize'] = (10, 6)
sns.violinplot(data['target'], data['chol'], palette = 'pastel')
plt.title('Relation of Cholestrol with Target', fontsize = 20, fontweight = 30)
plt.xticks([0,1], ['Haven\'t disease','Have Disease'])
plt.xlabel('Target')
plt.ylabel('Cholestrol')
plt.show()<jupyter_output><empty_output><jupyter_text>> The above Bivariate plot between cholestrol levels and target suggests that the Patients likely to suffer from heart diseases are having higher cholestrol levels in comparison to the patients with target 0(likely to not suffer from the heart diseases.>> Hence, we can infer from the above plot that the cholestrol levels plays an important role in determining heart diseases. We all must keep our cholestrol levels in control as possible.<jupyter_code># Resting electrocardiographic measurement vs target
# 0:Normal, 1:ST-T wave abnormality, 2:definite left ventricular hypertrophy by Estes criteria.
plt.rcParams['figure.figsize'] = (9, 5)
sns.countplot('restecg', hue='target', data=data, palette='pastel')
plt.title('Heart Disease Frequency for restecg')
plt.xlabel('Resting electrocardiographic measurement')
plt.xticks(rotation=0)
plt.legend(["Haven't Disease", "Have Disease"])
plt.ylabel('Frequency')
plt.show()<jupyter_output><empty_output><jupyter_text>>The above plot shows that people with restecg '1' and '0' are likely to have a heart disease than with restecg '2'.<jupyter_code>#exang vs. target

plt.rcParams['figure.figsize'] = (9, 5)
sns.countplot('exang', hue='target', data=data, palette='pastel')
plt.title('Heart Disease Frequency for Exercise induced angina',fontsize = 20, fontweight = 30)
plt.xlabel('Exercise induced angina')
plt.xticks([1, 0], ["No agnina", "Angina induced"])
plt.legend(["Haven't Disease", "Have Disease"])
plt.ylabel('Frequency')
plt.show()<jupyter_output><empty_output><jupyter_text>>Significantly more patients with the disease displayed exercise induced angina. Hence, This feature is strongly predictive.<jupyter_code># slope: the slope of the peak exercise ST segment (Value 0: upsloping, Value 1: flat, Value 2: downsloping)
# slope vs. target

plt.rcParams['figure.figsize'] = (9, 5)
sns.boxenplot(data['target'], data['slope'], palette = 'pastel')
plt.title('Relation between Peak Exercise and Target', fontsize = 20, fontweight = 30)
plt.xticks([0,1], ['Haven\'t disease','Have Disease'])
plt.xlabel('Target')
plt.ylabel('Slope')
plt.show()<jupyter_output><empty_output><jupyter_text>> Slope : 0 refers to upsloping, 1 refers to flat Exercises pattern, and 2 refers to down sloping.>> We observe, that Slope '2' causes heart disease much more than Slope '0' and '1'<jupyter_code>#ca: The number of major vessels (0-3)
# ca vs. target

plt.rcParams['figure.figsize'] = (9, 5)
sns.boxenplot(data['target'], data['ca'], palette = 'pastel')
plt.title('Relation between no. of major vessels and target', fontsize = 20, fontweight = 30)
plt.xlabel('Target')
plt.ylabel('The number of major vessels')
plt.xticks([0,1], ['Haven\'t disease','Have Disease'])
plt.show()<jupyter_output><empty_output><jupyter_text>> The above Bivariate plot between Target and Number of Major Vessels, shows that the patients who are more likely to suffer from Heart diseases are having high values of Major Vessels wheras the patiets who are very less likely to suffer from any kind of heart diseases have very low values of Major Vessels.<jupyter_code># age vs. target

plt.rcParams['figure.figsize'] = (9, 5)
sns.swarmplot(data['target'], data['age'], palette = 'pastel', size = 10)
plt.title('Relation of Age and target', fontsize = 20, fontweight = 30)
plt.xticks([0,1], ['Haven\'t disease','Have Disease'])
plt.xlabel('Target')
plt.ylabel('Age')
plt.show()<jupyter_output><empty_output><jupyter_text>>From the above Swarm plot between the target and the age of the patients, we are not able to find any clue or pattern, so age is not a very good attribute to determine the heart disease of a patient as a patient of heart diseases range from 30-70, whereas it is not important that all of the people lying in that same age group are bound to suffer from the heart diseases. Further, we know from domain knowledge that older patients are more likely to suffer from heart disease.<jupyter_code>#cp(chest pain) vs. target

plt.rcParams['figure.figsize'] = (9, 5)
sns.countplot('cp',hue='target', data=data, palette='pastel')
plt.title('Heart Disease Frequency for chest pain type')
plt.xlabel('Chest pain type')
plt.xticks([0, 1, 2, 3], ["Typical angina", "Atypical angina","non-anginal pain","asymptomatic"])
plt.legend(["Haven't Disease", "Have Disease"])
plt.ylabel('Frequency')
plt.show()<jupyter_output><empty_output><jupyter_text>> The above frequency plot shows that Patients suffering from heart disease have more probability of having asymptomatic and non-anginal chest pain than patient not having heart disease.<jupyter_code># thal vs. target | thal: A blood disorder called thalassemia (3 = normal; 6 = fixed defect; 7 = reversible defect)

plt.rcParams['figure.figsize'] = (9, 5)
sns.countplot('thal',hue='target', data=data, palette='pastel')
plt.title('Heart Disease Frequency for Thalassemia Disorder')
plt.xlabel('Thalassemia')
plt.xticks([0, 1, 2, 3], ["No", "Normal","Fixed defect","Reversible defect"])
plt.legend(["Haven't Disease", "Have Disease"])
plt.ylabel('Frequency')
plt.show()<jupyter_output><empty_output><jupyter_text>>In the above Boxen plot between Target and a Blood disorder called Thalessemia, It can be easily inferred that the patients with disease have high probability of having fixed defect or reversible defect of thalassemia. Hence, It is also a strong predictive feature.<jupyter_code># multi-variate analysis

plt.rcParams['figure.figsize'] = (9, 5)
sns.boxplot(x = data['target'], y = data['trestbps'], hue = data['sex'], palette = 'pastel')
plt.title('Checking relation of Resting blood pressure with genders to target', fontsize = 20, fontweight = 30)
plt.xticks([0,1], ['Haven\'t disease','Have Disease'])
plt.xlabel('Target')
plt.ylabel('Resting Blood Pressure')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11291345718169461, 'fraction_numerical': 0.01486180335296783, 'mean_word_length': 6.22724296005239, 'pattern_counts': {'":': 1, '<': 65, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '593194', 'license': 'Unlicense', 'n_tokens_mistral': 3601, 'n_tokens_neox': 3255, 'n_words': 1256, 'path': 'Exploratory Data Analysis.ipynb', 'repo_name': 'SuryaPradeepM/ML-Heart-disease-prediction', 'size': '11035'}