File size: 4,990 Bytes
6e4332e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8edbcb4
6e4332e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# -*- 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()