Create Your first basic machine learning project using the 'Logistic
Regression model' which predicts the probability of Corona Virus infection
through Symptoms.
In this article, I will explain to you how to create a basic Logistic Regression model from scratch which predicts the probability of Corona Virus infection through Symptoms.
Step By Step:-
-
First, we need to understand the basic concepts of Logistic Regression.
Logistic Regression:-
Logistic regression is a simple classification machine learning algorithm
it is used to predict For example,
-
To predict whether an message or email is a spam (1) or (0)
- To predict whether a person infected to corona virus (1) or not (0)
-
To predict whether a person has any heart disease (1) or not (0)
-
Now We need the dataset, here I have the sample dataset.
-
I am using the “spyder” editor for creating a machine learning model. Now, we need to import all the necessary packages.
import numpy as np
import pandas as pd
import pickle
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
-
We need pandas to read all data from the CSV file.
-
pickle package to create .pkl file, where we store our model, so we don’t need to run the model at every time, we just use pickle file because it contains our model.
-
We require train_test_split method to split the dataset into X_train, X_test,Y_train,Y_test.
-
StandardScalar for scaling the input features.
-
LogisticRegression for predicting the coronavirus infection through symptoms.
-
After importing packages we need to split the dataset into inputs and output.
dataset=pd.read_csv('corona.csv')
X=dataset.iloc[:,:-1].values
Y=dataset.iloc[:,5].values
-
Now split the dataset into X_train, X_test, Y_train, Y_test using the following code.
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.1,random_state=0)
-
Now, It’s time to scale input features using StandardScalar.
sc=StandardScaler()
X_train=sc.fit_transform(X_train)
X_test=sc.transform(X_test)
-
Fit the training dataset into the model.
classifier=LogisticRegression(random_state=0)
classifier.fit(X_train,Y_train)
-
By using pickle load your model into the model.pkl file.
pickle.dump(classifier,open('model.pkl','wb'))
-
To predict the test dataset use following code:-
y_pred_ans=classifier.predict(X_test)
You may also like:- Create your first API using 'Deno' and fetch all the data from API in the Flutter app using 'http' package !!!
The output of y_pred_ans:-
Now, we are completed with our model, its time to create crazy amazing
stuff with “Flask”.
-
We need to create this amazing UI in HTML here is the code:-
-
Code for HTML File:-
<!DOCTYPE
html>
<html
lang="en">
<head>
<meta
charset="UTF-8">
<meta
name="viewport"
content="width=device-width, initial-scale=1.0">
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous">
<script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
<title>CoronaVirus Predection through Symptoms</title>
</head>
<body>
<header
style="background-color: #3AAFa9;"
>
<div
class="container">
<div
class="row">
<h2
class="p-3"
style="color:white;">ML Based CoronaVirus Prediction through Symptoms</h2>
</div>
</div>
</header>
<div
class="container">
<div
class="col-sm-offset-4 col-sm-8"
>
<h4
class="p-2 badge-danger"
style="color:white">
{{ predection_text }} </h4>
</div>
<form
class="form-horizontal mt-2"
action="{{ url_for('predict') }}"
method="POST">
<div
class="form-group">
<label
class="control-label col-sm-12"
for="fever">Enter Fever Value between( 98 to 104 )</label>
<div
class="col-sm-8">
<input
type="number"
class="form-control"
id="fever"
placeholder="Enter Fever Value between( 98 to 104 )"
name="fever"
required>
</div>
</div>
<div
class="form-group">
<label
class="control-label col-sm-12"
for="bodypain">Body Pain ?</label>
<div
class="col-sm-8">
<select
class="form-control"
id="bodypain"
name="bodypain"
required>
<option
value="0">Please Select Option</option>
<option
value="0">
No </option>
<option
value="1">
YES </option>
</select>
</div>
</div>
<div
class="form-group">
<label
class="control-label col-sm-12"
for="age">Enter Your Age</label>
<div
class="col-sm-8">
<input
type="number"
class="form-control"
id="age"
placeholder="Enter Your Age"
name="age"
required>
</div>
</div>
<div
class="form-group">
<label
class="control-label col-sm-12"
for="runnynose">Runny Nose ?</label>
<div
class="col-sm-8">
<select
class="form-control"
id="runnynose"
name="runnynose"
required>
<option
value="0">Please Select Option</option>
<option
value="0">
No </option>
<option
value="1">
YES </option>
</select>
</div>
</div>
<div
class="form-group">
<label
class="control-label col-sm-12"
for="breathing">Do you have Difficulty in Breathing ?</label>
<div
class="col-sm-8">
<select
class="form-control"
id="breathing"
name="breathing"
required>
<option
value="0">Please Select Option</option>
<option
value="0">
No </option>
<option
value="1">
YES </option>
</select>
</div>
</div>
<div
class="form-group">
<div
class="col-sm-offset-12 col-sm-12">
<button
type="submit"
class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
</div>
<footer
class="page-footer font-small pt-4 mt-5"
style="background-color: rgb(24, 22, 22);">
<div
class="container-fluid text-center text-md-left">
</div>
<div
class="footer-copyright text-center pb-4"
style="color: rgb(167, 158, 158);">© 2020 Copyright: Gaurang.keluskar
</div>
</footer>
</body>
</html>
-
Code for app.py flask file:-
import
numpy as
np
from
flask import
Flask,request,jsonify,render_template
import
pickle
app=Flask(__name__)
model=pickle.load(open('model.pkl','rb'))
@app.route('/')
def
home():
return
render_template('index.html')
@app.route('/predict',methods=['POST'])
def
predict():
str=''
mydict=request.form
fever=int(mydict['fever'])
bodypain=int(mydict['bodypain'])
runnynose=int(mydict['runnynose'])
breathing=int(mydict['breathing'])
age=int(mydict['age'])
final_features=[[fever,bodypain,age,runnynose,breathing]]
prediction=model.predict(final_features)
if(prediction<0.5):
str="You are safe "
else:
str="There is possibility of coronavirus infection by your symptoms"
return
render_template('index.html',predection_text=str)
if
__name__=="__main__":
app.run(debug=True)
-
To run the flask project goto the terminal and type “python app.py” command:-
You May Also like:- How to create Tinder Swipe Cards in Flutter using 'flutter_tindercard'
package ?
1 Comments
Nice code 👍👍
ReplyDelete