Yash911's picture
Rename heart_disease_prediction.py to app.py
71f25c9
# -*- coding: utf-8 -*-
"""Heart_Disease_Prediction.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1cqMvzxYU8QVM8IdfByIymr-3YryFBU5p
Importing the Dependencies
"""
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
"""Data Collection and Processing"""
# loading the csv data to a Pandas DataFrame
heart_data = pd.read_csv('heart_disease_data.csv')
# print first 5 rows of the dataset
heart_data.head()
# print last 5 rows of the dataset
heart_data.tail()
# number of rows and columns in the dataset
heart_data.shape
# getting some info about the data
heart_data.info()
# checking for missing values
heart_data.isnull().sum()
# statistical measures about the data
heart_data.describe()
# checking the distribution of Target Variable
heart_data['target'].value_counts()
"""1 --> Defective Heart
0 --> Healthy Heart
Splitting the Features and Target
"""
X = heart_data.drop(columns='target', axis=1)
Y = heart_data['target']
print(X)
print(Y)
"""Splitting the Data into Training data & Test Data"""
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, stratify=Y, random_state=2)
print(X.shape, X_train.shape, X_test.shape)
"""Model Training
Logistic Regression
"""
model = LogisticRegression()
# training the LogisticRegression model with Training data
model.fit(X_train, Y_train)
"""Model Evaluation
Accuracy Score
"""
# accuracy on training data
X_train_prediction = model.predict(X_train)
training_data_accuracy = accuracy_score(X_train_prediction, Y_train)
print('Accuracy on Training data : ', training_data_accuracy)
# accuracy on test data
X_test_prediction = model.predict(X_test)
test_data_accuracy = accuracy_score(X_test_prediction, Y_test)
print('Accuracy on Test data : ', test_data_accuracy)
"""Building a Predictive System"""
def heart_predict(age,sex,cp,trestbps,chol,fbs,restecg,thalach,exang,oldpeak,slope,ca,thal):
#input_data = (62,0,0,140,268,0,0,160,0,3.6,0,2,2)
sex=""
if sex == "male":
sex = 0
else:
sex = 1
input_data = (age,sex,cp,trestbps,chol,fbs,restecg,thalach,exang,oldpeak,slope,ca,thal)
# change the input data to a numpy array
input_data_as_numpy_array= np.asarray(input_data)
# reshape the numpy array as we are predicting for only on instance
input_data_reshaped = input_data_as_numpy_array.reshape(1,-1)
prediction = model.predict(input_data_reshaped)
print(prediction)
if (prediction[0]== 0):
print('The Person does not have a Heart Disease')
return 'The Person does not have a Heart Disease'
else:
print('The Person has Heart Disease')
return 'The Person has Heart Disease'
heart_predict(62,"female",0,140,268,0,0,160,0,3.6,0,2,2)
#!pip install gradio
import gradio as gr
def heart_predict(age,sex_R,cp,trestbps,chol,fbs,restecg,thalach,exang,oldpeak,slope,ca,thal):
#input_data = (62,0,0,140,268,0,0,160,0,3.6,0,2,2)
sex=""
if sex == "male":
sex = 0
else:
sex = 1
input_data = (age,sex,cp,trestbps,chol,fbs,restecg,thalach,exang,oldpeak,slope,ca,thal)
# change the input data to a numpy array
input_data_as_numpy_array= np.asarray(input_data)
# reshape the numpy array as we are predicting for only on instance
input_data_reshaped = input_data_as_numpy_array.reshape(1,-1)
prediction = model.predict(input_data_reshaped)
print(prediction)
if (prediction[0]== 0):
print('The Person does not have a Heart Disease')
return 'The Person does not have a Heart Disease'
else:
print('The Person has Heart Disease')
return 'The Person has Heart Disease'
demo = gr.Interface(
fn=heart_predict,
inputs = [
gr.Slider(1, 100, value=62, label="age", info="Choose between 1 and 20"),
gr.Radio(["male","female"], value="male", label="sex", info="select"),
gr.Slider(1, 100, value=0, label="cp", info="Choose between 1 and 100"),
gr.Slider(1, 50, value=140, label="trestbps", info="Choose between 1 and 50"),
gr.Slider(1, 200, value=268, label="chol", info="Choose between 1 and 200"),
gr.Slider(1, 100, value=0, label="fbs", info="Choose between 1 and 100"),
gr.Slider(0.0, 2.0, value=0, label="restecg", info="Choose between 0.0 and 1.0"),
gr.Slider(1, 100, value=160, label="thalach", info="Choose between 1 and 100"),
gr.Slider(1, 100, value=0, label="exang", info="Choose between 1 and 100"),
gr.Slider(1, 100, value=3.6, label="oldpeak", info="Choose between 1 and 100"),
gr.Slider(1, 100, value=0, label="slope", info="Choose between 1 and 100"),
gr.Slider(1, 100, value=2, label="ca", info="Choose between 1 and 100"),
gr.Slider(1, 100, value=2, label="thal", info="Choose between 1 and 100"),
],
outputs = "text",
)
if __name__ == "__main__":
demo.launch()