# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
import matplotlib.pyplot as plt
import seaborn as sb

heart=pd.read_csv("/kaggle/input/heart-attack-analysis-prediction-dataset/heart.csv")
heart.head()
print("Total number of rows and columns",heart.shape)
"""
# Checking Null values in dataset
- No null values present 
"""
heart.isnull().sum()
"""
# Checking statistical values
"""
heart.describe()
"""
# Corelation between columns 
"""
corr=heart.corr()
corr
plt.figure(figsize=(15,10))
sb.heatmap(corr, annot=True,linewidth=5)
plt.show()
"""
# visualizing certain columns 
- age vs caa
- age vs trtbps
- age vs chol
- age vs oldpeak
"""
fig , ax =plt.subplots(2,2)
plt.subplots_adjust(left=0,right=2,bottom=1,top=2,wspace=0.2,hspace=0.4)
ax[0,0].set_title("Number of major vessels")
ax[0,0].bar(heart["age"],heart["caa"])

ax[0,1].set_title("Resting blood pressure")
ax[0,1].bar(heart["age"],heart["trtbps"])

ax[1,0].set_title("cholestoral ")
ax[1,0].bar(heart["age"],heart["chol"])

ax[1,1].set_title("oldpeak")
ax[1,1].bar(heart["age"],heart["oldpeak"])
plt.show()
plt.figure()
plt.title("sex vs thall")
sb.barplot(x="sex",y="thall",data=heart)
plt.show()
"""
# Visualizing
- exng vs oldpeak
- exng vs thall
"""
fig, (ax1,ax2) =plt.subplots(1,2)
plt.subplots_adjust(left=0,right=2,bottom=1,top=2,wspace=0.1,hspace=0.1)
ax1.set_title("exercise induced angina (1 = yes; 0 = no) vs oldpeak")
ax1.bar(heart["exng"],heart["oldpeak"])

ax2.set_title("exercise induced angina (1 = yes; 0 = no) vs thall")
ax2.bar(heart["exng"],heart["thall"])

"""
# visualizing 
- slp vs thalachh
"""
plt.figure()
plt.title("slp vs thalachh")
sb.barplot(x="slp",y="thalachh",data=heart)
plt.show()
unique_age=sorted(heart["age"].unique())
print(unique_age)
list20_40=[]
list41_50=[]
list51_60=[]
list61_70=[]
list71=[]
for i in heart["age"]:
    if i <=40:
        list20_40.append(i)
    elif 40 <i<=50:
        list41_50.append(i)
    elif 50 <i<=60:
        list51_60.append(i)
    elif 60 <i<=70:
        list61_70.append(i)
    else:
        list71.append(i)
list20_40=len(list20_40)
#print(list20_40)
list41_50=len(list41_50)
#print(list41_50)
list51_60=len(list51_60)
#print(list51_60)      
list61_70=len(list61_70)
#print(list61_70)      
list71=len(list71)
#print(list71)
age_count=[list20_40,list41_50,list51_60,list61_70,list71]
#print(age_count)
label_age=["20-40","41-50","51-60","61-70","71"]
heart["cp"].unique()
list0=[]
list1=[]
list2=[]
list3=[]
#list4=[]
for i in heart["cp"]:
    if i == 0:
        list0.append(i)
    elif i== 1:
        list1.append(i)
    elif i==2:
        list2.append(i)
    else:
        list3.append(i)

list0=len(list0)
list1=len(list1)
list2=len(list2)
list3=len(list3)
cp_list=[list0,list1,list2,list3]
label_cp=["asymptomatic","typical angina","atypical angina","non-anginal pain"]
heart["fbs"].unique()
list_0=[]
list_1=[]
for i in heart["fbs"]:
    if i == 0:
        list_0.append(i)
    else:
        list_1.append(i)

list_0=len(list_0)
list_1=len(list_1)
fbs_list=[list_0,list_1]
label_fbs=["0","1"]
heart["restecg"].unique()
li0=[]
li1=[]
li2=[]
for i in heart["restecg"]:
    if i==0:
        li0.append(i)
    elif i==1:
        li1.append(i)
    else:
        li2.append(i)
li0=len(li0)
li1=len(li1)
li2=len(li2)
restecg_list=[li0,li1,li2]
label_restecg=["normal","having ST-T wave abnormality","showing probable"]
heart["exng"].unique()
l0=[]
l1=[]
for i in heart["exng"]:
    if i==0:
        l0.append(i)
    else:
        l1.append(i)
l0=len(l0)
l1=len(l1)
exng_list=[l0,l1]
label_exng=["no","yes"]
heart["caa"].unique()
lis0=[]
lis1=[]
lis2=[]
lis3=[]
lis4=[]
for i in heart["caa"]:
    if i ==0:
        lis0.append(i)
    elif i==1:
        lis1.append(i)
    elif i==2:
        lis2.append(i)
    elif i==3:
        lis3.append(i)
    else:
        lis4.append(i)
lis0=len(lis0)
lis1=len(lis1)
lis2=len(lis2)
lis3=len(lis3)
lis4=len(lis4)
caa_list=[lis0,lis1,lis2,lis3,lis4]
label_caa=["0","1","2","3","4"]
"""
# Pie chart
- Age :Age of the patient 
- cp :Chest Pain type chest pain type
    - Value 1: typical angina
    - Value 2: atypical angina
    - Value 3: non-anginal pain
    - Value 4: asymptomatic
- fbs : (fasting blood sugar > 120 mg/dl) (1 = true; 0 = false)
- restecg : resting electrocardiographic results
    - Value 0: normal
    - Value 1: having ST-T wave abnormality (T wave inversions and/or ST elevation or depression of > 0.05 mV)
    - Value 2: showing probable or definite left ventricular hypertrophy by Estes' criteria
- exng :exercise induced angina (1 = yes; 0 = no)
- caa : number of major vessels (0-3)
"""
fig,ax=plt.subplots(2,3)
plt.subplots_adjust(left=0,right=2,bottom=2,top=4,wspace=0.4,hspace=0.4)

ax[0,0].set_title("Age of the patient")
ax[0,0].pie(age_count,labels=label_age,autopct='%1.2f%%')

ax[0,1].set_title("Chest Pain type chest pain type")
ax[0,1].pie(cp_list,labels=label_cp,autopct='%1.2f%%')

ax[1,0].set_title("fasting blood sugar > 120 mg/dl (1 = true; 0 = false)")
ax[1,0].pie(fbs_list,labels=label_fbs,autopct='%1.2f%%')

ax[1,1].set_title("resting electrocardiographic results")
ax[1,1].pie(restecg_list,labels=label_restecg,autopct='%1.2f%%')

ax[0,2].set_title(" exercise induced angina (1 = yes; 0 = no)")
ax[0,2].pie(exng_list,labels=label_exng,autopct='%1.2f%%')

ax[1,2].set_title("number of major vessels (0-3)")
ax[1,2].pie(caa_list,labels=label_caa,autopct='%1.2f%%')
"""
# Splitting data
"""
train=heart.iloc[:,:-1]
print(train.shape)
test=heart.iloc[:,-1:]
print(test.shape)
x_train,x_test,y_train,y_test=train_test_split(train,test,test_size=0.3,random_state=100)
print("The size of x_train is :",x_train.shape)
print("The size of x_test is :",x_test.shape)
print("The size of y_train is : ",y_train.shape)
print("The size of y_test is : ",y_test.shape)
"""
# Performing Standard Scaling 
"""
scaler=StandardScaler()
x_train=scaler.fit_transform(x_train)
x_test=scaler.fit_transform(x_test)
"""
# Performing Logistic Regression and prediction
"""
lr=LogisticRegression()
lr.fit(x_train,y_train)
y_pred=lr.predict(x_test)
y_pred
"""
# Accuracy check
"""
accuracy_score(y_pred,y_test)