Build A Machine Learning Web App Using Python

Machine Learning Diabetes Detection Web App Using Python & Streamlit

Image for post
Image for post

In this article, I will create a machine learning web app to detect if a person has diabetes using Python & Streamlit. Apps like this could someday help the people in the medical field to make more informed diagnosis or possibly help the average person that can’t afford medical care to get a diagnosis more cheaply.

Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python’s design philosophy emphasizes code readability with its notable use of significant whitespace.

Streamlit is an open-source app framework for Machine Learning and Data Science teams. Create beautiful data apps in hours, not weeks. All in pure Python.

If you prefer not to read this article and would like a video representation of it, you can check out the YouTube Video below. It goes through everything in this article with a little more detail, and will help make it easy for you to start programming even if you don’t have the programming language Python installed on your computer. Or you can use both as supplementary materials for learning !

Install Prerequisite Libraries

In this tutorial, we will be using the Python library streamlit , pilandsklearn . You can easily install them using the pip install command as follows to install streamlit , pilandsklearn:

pip install streamlit scikit-learn pil

Check if streamlit is working properly

streamlit hello

Once you know the program is installed properly and running, then you can start programming !

Start Programming

We will start by creating the python file, then writing some code in the file and finally running the program. Let’s begin !

Create a new python file in notepad or some other text editor application. Give the file a name, I named my file Then save it in a location that you won’t forget, I saved my application on my Desktop.

Image for post
Image for post

Next Edit the newly created Python file and import the libraries.

import pandas as pd
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from PIL import Image
import streamlit as st

Add a title and an image.

# Diabetes Detection
Detect if someone has diabetes using machine learning and python !
image ='Diabetes Detection Web App.png')
st.image(image, caption='ML',use_column_width=True)
Image for post
Image for post
Picture of the title and image

Add and show some data.

#Get the data
df = pd.read_csv("Diabetes.csv")
st.subheader('Data Information:')#Show the data as a table (you can also use st.write(df))
#Get statistics on the data
# Show the data as a chart.
chart = st.line_chart(df)
Image for post
Image for post
An image showing the data

Once you show the data, then you can see that this data set contains the columns:

Pregnancies : The number of times the person was pregnant
Glucose: Plasma glucose concentration
Blood Pressure: Diastolic blood pressure (mm Hg)
SkinThickness: Triceps skin fold thickness (mm)
Insulin: 2-Hour serum insulin (mu U/ml)
BMI: Body mass index (weight in kg/(height in m)²)
DiabetesPedigreeFunction: Diabetes pedigree function
Age: Age (years)
Class variable: (0-no diabetes, 1-has diabetes)

Split the data set into independent features and dependent target data sets, then split the data again into 75% training set and 25% testing set.

#Split the data into independent 'X' and dependent 'Y' variables
X = df.iloc[:, 0:8].values
Y= df.iloc[:,-1].values
# Split the dataset into 75% Training set and 25% Testing set
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.25, random_state = 0)

Get and show the feature input of the person from the user. Remember that we will use this information to determine if the person has diabetes or not.

#Get the feature input from the user
def get_user_input():
pregnancies = st.sidebar.slider('pregnancies', 0, 17,3)
glucose = st.sidebar.slider('glucose', 0, 199, 117)
blood_pressure = st.sidebar.slider('blood_pressure',0, 122,72)
skin_thickness = st.sidebar.slider('skin_thickness', 0, 99, 23)
insulin = st.sidebar.slider('insulin', 0.0, 846.0, 30.5)
BMI = st.sidebar.slider('BMI', 0.0, 67.1, 32.0)
DPF = st.sidebar.slider('DPF', 0.078, 2.42, 0.3725)
age = st.sidebar.slider('age', 21, 81, 29)

user_data = {'pregnancies': pregnancies,
'glucose': glucose,
'blood_pressure': blood_pressure,
'skin_thickness': skin_thickness,
'insulin': insulin,
'age': age
features = pd.DataFrame(user_data, index=[0])
return features
user_input = get_user_input()st.subheader('User Input :')
Image for post
Image for post
The user slider input
Image for post
Image for post
Displaying the users input

Create the model for the classification of the individual.

RandomForestClassifier = RandomForestClassifier(), Y_train)

Show the models metrics and classification.

#Show the models metrics
st.subheader('Model Test Accuracy Score')
st.write( str(accuracy_score(Y_test, RandomForestClassifier.predict(X_test)) * 100) + '%' )
prediction = RandomForestClassifier.predict(user_input)st.subheader('Classification: ')
Image for post
Image for post
Displaying the models metrics

Now, we can see based on the users input from the side bar, the model believes this individual does not have diabetes, which is noted by the number 0. We can also see that this model is about 77.6% accurate.

How To Run The Program

Go to command prompt or your terminal.

Image for post
Image for post

Type the following command.

streamlit run "C:\<Directory>\"

Note: <Directory> is a place holder for the file location.

The app should open a browser automatically and start working !

Image for post
Image for post

If you are interested in reading more on machine learning to immediately get started with problems and examples then I strongly recommend you check out Hands-On Machine Learning with Scikit-Learn and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems. It is a great book for helping beginners learn how to write machine learning programs, and understanding machine learning concepts.

Image for post
Image for post

Hands-On Machine Learning with Scikit-Learn and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems

Thanks for reading this article I hope its helpful to you all ! If you enjoyed this article and found it helpful please leave some claps to show your appreciation. Keep up the learning, and if you like machine learning, mathematics, computer science, programming or algorithm analysis, please visit and subscribe to my YouTube channels (randerson112358 & computer science).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store