Dr. Ateendra Jha
8
Python Web Application - Hypertension Status Prediction
Python web application was developed using flask with 70,000 subject data to predict the Hypertension status with the help of demographic and lifestyle details
Web Application
February 27, 2022
Python Web Application - Hypertension Status Prediction
Introduction :
Hypertension ̶ or elevated blood pressure ̶ is a serious medical condition that significantly increases the risks of heart, brain, kidney and other diseases.
An estimated 1.28 billion adults aged 30-79 years worldwide have hypertension, most (two-thirds) living in low- and middle-income countries
An estimated 46% of adults with hypertension are unaware that they have the condition.
Less than half of adults (42%) with hypertension are diagnosed and treated.
Approximately 1 in 5 adults (21%) with hypertension have it under control.
Hypertension is a major cause of premature death worldwide.
One of the global targets for noncommunicable diseases is to reduce the prevalence of hypertension by 33% between 2010 and 2030.
Source : WHO
Impact :
Early detection of hypertension can be a very effective measure to prevent any coplications associated with non managed increasing blood pressure. It can not only reduce the complication as well as unwanted medical treatment but also will prevent the degrading health statistics .
Problem Statement :
To develop an anlgorithm to predict the blood pressure status.
To use the demographic and life style data to flag the subject who may get hypertension.
Approach :
ML model was developed
Data Understanding
Data Cleaning and imputation
Data Vizualization
Data Preperation
Model building
Model Evaluation
HTML page was designed to get the user input
HTML page and Python was integrated to to execute Model on the data entered.
Output was displayed in HTML page as either of the following:
High Chance of Hypertension
Low Chance of Hypertension
Solution :
Once flagged with high chance of hypertension, subject can go under non pharmacological management to prevent any further complication as well as medical treatment.
Non pharmacological management includes
Lifestyle modification
Eating habit
Managing risk factors
Links :
import pandas as pd, numpy as np, seaborn as sns, matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
import statsmodels.api as sm
from flask import Flask, render_template, request
from sklearn.metrics import confusion_matrix
from sklearn import metrics
import webbrowser
from threading import Timer
app = Flask(__name__)
@app.route("/")
@app.route("/home")
def home():
return render_template("health_index.html")
@app.route("/result", methods= ["POST", 'GET'])
def result():
Rawdf = pd.read_csv("https://97221248-9bb3-4fc9-9d37-d1ad34bc7154.usrfiles.com/ugd/972212_590fd873a65640c1843bb2e745ec52fc.csv", sep=";")
Rawdf['AgeinYr'] = (Rawdf.age)//365
Rawdf['BP_Status'] = [1 if i > 140 or j > 90 else 0 for i,j in zip(Rawdf.ap_hi, Rawdf.ap_lo) ]
Finaldf = Rawdf[['gender','height','weight','cholesterol','alco','active','cardio','AgeinYr', 'BP_Status']]
Finaldf['gender'] = Finaldf.gender.map({1:0,2:1}) # 1 - Male 0 - Female
X_train = Finaldf.drop('BP_Status', axis=1)
y_train = Finaldf[['BP_Status']]
lr1 = sm.GLM(y_train, X_train, family= sm.families.Binomial())
lm1 = lr1.fit()
y_train_predict = lm1.predict(X_train)
Pred_DF = pd.DataFrame({'Actual':y_train.values, 'Predict' : y_train_predict})
Matrix_df = pd.DataFrame(columns=['Cut_off', 'accuracy', 'Sensitivity', 'Specificity'])
cutt_off = list(np.arange(0,1,0.01))
for i in cutt_off:
val = [1 if j > i else 0 for j in Pred_DF.Predict]
Pred_DF[i] = val
for i in cutt_off:
cm = metrics.confusion_matrix(Pred_DF.Actual,Pred_DF[i])
total_val = sum(sum(cm))
TP = cm[1,1]
TN = cm[0,0]
FP = cm[0,1]
FN = cm[1,0]
accuracy = (TP+TN)/total_val
Sensitivity = TP / (TP+FN)
Specificity = TN / (TN+FP)
Matrix_df.loc[i] = [i, accuracy, Sensitivity, Specificity]
Matrix_df['Dif'] = abs(Matrix_df.Sensitivity - Matrix_df.Specificity)
Matrix_df = Matrix_df.sort_values(by = 'Dif', ascending=False)
Optimum_cutoff = Matrix_df[Matrix_df.Dif == Matrix_df.Dif.min()]
ct_off = Optimum_cutoff['Cut_off'].values[0]
output = request.form.to_dict()
X_test = pd.DataFrame({'gender':int(output['gender']), 'height':int(output['height']), 'weight':int(output['weight']),
'cholesterol':int(output['chol']), 'alco':int(output['alcohol']), 'active':int(output['active']),
'cardio':int(output['cardio']), 'AgeinYr':int(output['age'])}, index=[0])
y_test_pred = lm1.predict(X_test)
if y_test_pred.values > 0.218:
prediction = 'Chances of Hypertension'
else:
prediction = 'Low Chance of Hypertension'
return render_template("health_index.html", prediction = prediction)
def open_browser():
webbrowser.open_new('http://127.0.0.1:5000/')
if __name__ == '__main__':
Timer(1, open_browser).start()
app.run(port=5000)