Skip to content

Commit 192ab74

Browse files
committed
update
1 parent a816c7a commit 192ab74

File tree

82 files changed

+1629
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1629
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pandas as pd
2+
import numpy as np
3+
import pickle
4+
from sklearn.neighbors import KNeighborsClassifier
5+
6+
url = 'https://raw.githubusercontent.com/sarwansingh/Python/master/ClassExamples/data/HeartDisease_cardio_data.csv'
7+
df = pd.read_csv(url , sep = ";")
8+
print("reading file " )
9+
df['age']=df['age']/365
10+
print("cleaning data ")
11+
df =df.drop( ['id','cholesterol', 'gluc'] , axis=1)
12+
#create new column with standard values
13+
df=df.assign(heartrate='60-200')
14+
# assigning the values
15+
df.loc[df['age'] >= 20 ,'heartrate'] = '100-170'
16+
df.loc[df['age'] >= 30 ,'heartrate'] = '95-165'
17+
df.loc[df['age'] >= 40 ,'heartrate'] = '90-155'
18+
df.loc[df['age'] >= 50 ,'heartrate'] = '85-145'
19+
df.loc[df['age'] >= 60 ,'heartrate'] = '80-140'
20+
df.loc[df['age'] >= 70 ,'heartrate'] = '75-130'
21+
#codification of column heartrate
22+
df['heartrate'] = df['heartrate'].replace(['85-145', '90-155', '80-140', '95-165', '100-170' ],['1', '2', '3', '4', '5'])
23+
24+
df=df.dropna()
25+
#convert the columns to int
26+
df['weight']=df.weight.astype(int)
27+
df['age']=df.age.astype(int)
28+
df['heartrate']=df.heartrate.astype(int)
29+
#realigning the columns
30+
df = df[['age' , 'gender', 'height','weight', 'ap_hi', 'ap_lo', 'smoke', 'alco', 'active', 'heartrate', 'cardio']]
31+
print("making model" )
32+
#creating model
33+
knnmodel = KNeighborsClassifier()
34+
X = df.iloc[:,0:10 ]
35+
y = df.iloc[:, -1]
36+
#fit the model
37+
knnmodel.fit(X,y)
38+
#predict
39+
print ("using model for prediction" )
40+
res = knnmodel.predict([[48, 2, 169, 82, 150, 100, 0, 0, 1, 4 ]])
41+
print( res )
42+
res= knnmodel.predict([[52 , 2, 168, 76, 120, 80, 1, 0, 1, 4]])
43+
print( res)
44+
print("prediction successful " )
45+
print( " saving model " )
46+
pickle.dump(knnmodel, open('knnmodel.pkl', 'wb') , protocol=2)
47+
48+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: gunicorn app:app
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#app.py
2+
from flask import Flask,request, url_for, redirect, render_template
3+
import pickle
4+
import numpy as np
5+
6+
app = Flask(__name__)
7+
model=pickle.load(open('knnmodel.pkl','rb'))
8+
9+
@app.route('/')
10+
def home():
11+
return render_template("index.html")
12+
13+
@app.route('/predict',methods=['POST','GET'])
14+
def predict():
15+
# receive the values send by user in three text boxes thru request object -> requesst.form.values()
16+
17+
int_features = [int(x) for x in request.form.values()]
18+
final_features = [np.array(int_features)]
19+
20+
#print(final_features)
21+
22+
#final_features = [[52 , 2, 168, 76, 120, 80, 1, 0, 1, 4]]
23+
#[[48, 2, 169, 82, 150, 100, 0, 0, 1, 4 ]]
24+
25+
prediction=model.predict_proba(final_features)
26+
output='{0:.{1}f}'.format(prediction[0][1], 2)
27+
28+
return render_template('index.html', pred='Heart Disease probability is : {}'.format(output))
29+
#return render_template('index.html', pred= final_features)
30+
31+
if __name__ == '__main__':
32+
app.run(debug=False)
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Flask==1.1.2
2+
gunicorn==19.9.0
3+
itsdangerous==1.1.0
4+
Jinja2==2.11.2
5+
MarkupSafe==1.1.1
6+
Werkzeug==1.0.1
7+
numpy==1.16.6
8+
scipy==1.2.3
9+
scikit-learn>=0.18
10+
matplotlib>=1.4.3
11+
pandas>=0.19
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title> Heart Disease Prediction </title>
5+
</head>
6+
<body>
7+
8+
<h2>Heart Disease Prediction</h2>
9+
<h3>Predict the probability of Cardio-Vascular Disease </h3>
10+
11+
<form action='/predict' method="post">
12+
<table>
13+
<tr><td> Age
14+
<td> <input type=number name="age" placeholder="Age (in years) " >
15+
</tr>
16+
<tr><td> Gender
17+
<td> <input name="gender" type=radio value=1> Male <input name="gender" type=radio value=2> Female
18+
</tr>
19+
<tr><td> Height
20+
<td> <input type=number name="height" placeholder="Height (in cms) " >
21+
</tr>
22+
<tr><td> Weight
23+
<td> <input type=number name="weight" placeholder="Weight (in Kgs) " >
24+
</tr>
25+
<tr><td> BP (High)
26+
<td> <input type=number name="ap_hi" placeholder="BP (High) " >
27+
</tr>
28+
<tr><td> BP (Low)
29+
<td> <input type=number name="ap_lo" placeholder="BP (Low) " >
30+
</tr>
31+
<tr><td> Smoker
32+
<td> <input name="smoker" type=radio value=1> Yes <input name="smoker" type=radio value=0> No
33+
</tr>
34+
<tr><td> Alcoholic
35+
<td> <input name="alcohol" type=radio value=1> Yes <input name="alcohol" type=radio value=0> No </tr>
36+
<tr><td> Exercise Daily
37+
<td> <input name="active" type=radio value=1> Yes <input name="active" type=radio value=0> No
38+
</tr><tr><td> Heart Rate
39+
40+
<td> <select name = "heartrate">
41+
<option value =1> 85-145
42+
<option value =2> 90-155
43+
<option value =3> 80-140
44+
<option value =4> 95-165
45+
<option value =5> 100-170
46+
</select>
47+
</tr>
48+
49+
<tr>
50+
<td colspan=2 align=center><br><button type="submit">Predict Probability</button>
51+
</tr>
52+
</table>
53+
</form>
54+
55+
<br> {{pred}}<br>
56+
57+
</body>
58+
</html>
59+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: gunicorn app:app
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#app.py
2+
from flask import Flask,request, url_for, redirect, render_template
3+
import pickle
4+
import numpy as np
5+
6+
app = Flask(__name__)
7+
model=pickle.load(open('knnmodel.pkl','rb'))
8+
9+
@app.route('/')
10+
def home():
11+
return render_template("index.html")
12+
13+
@app.route('/predict',methods=['POST','GET'])
14+
def predict():
15+
# receive the values send by user in three text boxes thru request object -> requesst.form.values()
16+
17+
int_features = [int(x) for x in request.form.values()]
18+
final_features = [np.array(int_features)]
19+
20+
#print(final_features)
21+
22+
#final_features = [[52 , 2, 168, 76, 120, 80, 1, 0, 1, 4]]
23+
#[[48, 2, 169, 82, 150, 100, 0, 0, 1, 4 ]]
24+
25+
prediction=model.predict_proba(final_features)
26+
output='{0:.{1}f}'.format(prediction[0][1], 2)
27+
28+
return render_template('index.html', pred='Heart Disease probability is : {}'.format(output))
29+
#return render_template('index.html', pred= final_features)
30+
31+
if __name__ == '__main__':
32+
app.run(debug=False)
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Flask==1.1.2
2+
gunicorn==19.9.0
3+
itsdangerous==1.1.0
4+
Jinja2==2.11.2
5+
MarkupSafe==1.1.1
6+
Werkzeug==1.0.1
7+
numpy==1.16.6
8+
scipy==1.2.3
9+
scikit-learn>=0.18
10+
matplotlib>=1.4.3
11+
pandas>=0.19

0 commit comments

Comments
 (0)