Disease Prediction: With RFC and Flask
By: Shruti Nk
Tags: Machine Learning, HTML, Flask, Python, Random Forest Classifiers

Machine learning has always fascinated me. The ability to analyse large amounts of data and then being able to obtain conclusive results effeciently is what makes it such a sought out skill. The beauty of this is however when we are actually able to put the concepts to use and implement them on real life problems.

Which brings me to this article. Here I will guide you through a model that gives a probable estimation of a disease contracted on the basis of the three entered symptoms. I trained this model using the Random Forest Classifier which I then linked to a Flask framework.

So let’s get started!

Model

The data obtained from the dataset was fairly clean and was already sorted into training and testing csv files.

train = pd.read_csv("training_data.csv",error_bad_lines = False)

test = pd.read_csv("test_data.csv",error_bad_lines = False)

The data set had 132 symptoms linked to 41 diseases.

I then proceeded to split the data into x and y.

y_test = test.prognosis

x_test = test.drop('prognosis',axis = 1)

Visualisation

To visualise the data we are dealing with, I dedcided to make a simple count plot.

f,ax = plt.subplots(figsize=(75,16))

sns.countplot(y_train,label="Count",ax = ax)

countplot for the column prognosis

On observing the data. I was able to conclude that no normalisation was required as the values were distributed evenly.

Model Training

This seemed to be a fairly starightforward classification problem so I used the Random Forest Classification algorithm to train and test my model.

clf_rf = RandomForestClassifier(random_state=43)

clr_rf = clf_rf.fit(x_train,y_train)

Accuracy

ac = accuracy_score(y_test,clf_rf.predict(x_test))

The model gives us an accuracy of 97.62%

Before we move on however, there is one important step remaining. To link the model to Flask, you should first write it into a pickle file which is then read by the Flask app.

Luckily for us, all we need to do is import the pickle library and use the dump function to write into the file.

pickle.dump(clr_rf,open('model.pkl','wb'))

And with that you are done with the first section!

Flask

Flask is one of the most popular frameworks used and is honestly quite easy to implement. Well, incase you do not know what a framework is, it is nothing but a code library that makes a developer’s life easier when building reliable, scalable and maintainable web applications by providing reusable code or extensions for common operations.

A simple flask application

Now that we have gone over the basic definition, let’s get down to creating this framework. First let’s ensure we read the data stored in the pickle file so that we can access it here as well.

model = pickle.load(open('model.pkl', 'rb'))

Routing

Next let’s draw our attention to the @app.route(). Here I will be using it twice. First for returning my HTML page and the second for returning the accuracy value.

returns the html page

Since the input type is in the form of a string, all I have done is taken in the values from the form and then convereted it into a 2D binary array so that I can feed those values into my predict function.

if request.method == 'POST':

col = x_test.columns

inputt = [str(x) for x in request.form.values()]

b = [0]*132

for x in range(0, 132):

for y in inputt:

if (col[x] == y):

b[x] = 1

returns the accuracy value in the html page

Don’t forget to add-in this last condition to ensure that you application runs smoothly.

And with that you are done with the second part!

HTML

This is by far the easiest of the three. Now all that you are left to do is design an HTML page to improve the user experience. Incase you have no prior HTML knowledge, you can just pick up one of the many templates and tweak it a bit.

1st tweak

To be done wherever you want your accuracy statement to be displayed.

If you remember, in the second routing statement, we assigned the predicted value to this variable and then returned it. This value then gets called and displayed on your HTML.

2nd tweak

Ensure you have your form with an action defined as predict and the method as post. This will help you read in your values which the user enters into the HTML and then send it to your Flask app.

And with this you have completed all the three steps and should be able to successfully link your Flask app to your ML model.

This is how mine turned out 🍕

home page with the option to enter 3 symptoms

use css and js to make your page interactive ✨

click on predict to give you a probable diagnosis!

All the codes are available on my github.

https://github.com/PotatoinPyjamas/Disease-Prediction

If you liked the article please don’t hesitate to show your ❤ .