Disease Prediction Web Application using Machine Learning
Introduction
In today’s digital age, healthcare technology is advancing rapidly, and machine learning plays a crucial role in medical diagnosis and prediction. A Disease Prediction Web Application is an excellent project for students learning Python and machine learning concepts. This project demonstrates how artificial intelligence can be applied to healthcare by predicting various diseases based on user input symptoms and test results.
Machine learning algorithms can analyze patterns in medical data and make predictions about potential health conditions. By building this web application, students will learn fundamental concepts of data science, web development with Streamlit, and practical implementation of machine learning models in healthcare.
This project is particularly valuable because it combines multiple technologies – Python programming, machine learning libraries like scikit-learn, data preprocessing techniques, and web application development using Streamlit. It’s perfect for computer science students, aspiring data scientists, or anyone interested in healthcare technology.
Overview
Multiple Disease Prediction Web App is a health-related project made using Python and Streamlit. It allows users to enter their health details like symptoms or test results and get predictions for diseases such as Diabetes, Heart Disease, Parkinson’s, Liver Disease, Hepatitis, and Jaundice. The app uses machine learning models trained on real health data. Users can pick a disease from the sidebar, fill out the form, and get instant prediction results. This project is perfect for students, beginners in AI/ML, or anyone looking to build a health-focused project using Python.
Project Information
| Project Name | Multiple Disease Prediction Webapp |
| Language/s Used | Python, Streamlit |
| Python Version (Recommended) | 3.8+ |
| Type | Web Application |
Step-by-Step Implementation Guide
Step 1: Setting Up the Environment
First, install the required libraries. Open your command prompt or terminal and run:
pip install streamlit pandas numpy scikit-learn pickle5
Step 2: Preparing the Dataset
For this project, you’ll need datasets for different diseases. Popular sources include Kaggle, UCI Machine Learning Repository, or medical databases. Each dataset should contain features relevant to the specific disease and a target column indicating the presence or absence of the disease.
Step 3: Training Machine Learning Models
Before creating the web app, you need to train and save machine learning models for each disease. Here’s an example for diabetes prediction:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import pickle
# Load diabetes dataset
data = pd.read_csv('diabetes.csv')
# Separate features and target
X = data.drop('Outcome', axis=1)
y = data['Outcome']
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train the model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# Save the trained model
with open('diabetes_model.pkl', 'wb') as file:
pickle.dump(model, file)
Step 4: Creating the Streamlit Web Application
Now, let’s create the main web application file. Create a new file called app.py:
import streamlit as st
import pickle
import numpy as np
from PIL import Image
# Configure the page
st.set_page_config(
page_title="Disease Prediction System",
page_icon="",
layout="wide"
)
# Load trained models
@st.cache_resource
def load_models():
models = {}
try:
with open('diabetes_model.pkl', 'rb') as file:
models['diabetes'] = pickle.load(file)
with open('heart_model.pkl', 'rb') as file:
models['heart'] = pickle.load(file)
# Add more models as needed
except FileNotFoundError:
st.error("Model files not found. Please ensure all model files are available.")
return models
# Main title
st.title(" Multiple Disease Prediction System")
st.markdown("---")
# Sidebar for navigation
st.sidebar.title("Select Disease")
disease_option = st.sidebar.selectbox(
"Choose a disease to predict:",
["Diabetes", "Heart Disease", "Parkinson's Disease", "Liver Disease"]
)
# Load models
models = load_models()
# Diabetes Prediction Page
if disease_option == "Diabetes":
st.header(" Diabetes Prediction")
st.write("Please enter the following information:")
col1, col2 = st.columns(2)
with col1:
pregnancies = st.number_input("Number of Pregnancies", min_value=0, max_value=20, value=0)
glucose = st.number_input("Glucose Level", min_value=0, max_value=300, value=120)
blood_pressure = st.number_input("Blood Pressure", min_value=0, max_value=200, value=80)
skin_thickness = st.number_input("Skin Thickness", min_value=0, max_value=100, value=20)
with col2:
insulin = st.number_input("Insulin Level", min_value=0, max_value=900, value=80)
bmi = st.number_input("BMI", min_value=0.0, max_value=70.0, value=25.0)
diabetes_pedigree = st.number_input("Diabetes Pedigree Function", min_value=0.0, max_value=3.0, value=0.5)
age = st.number_input("Age", min_value=1, max_value=120, value=25)
# Prediction button
if st.button("Predict Diabetes", key="diabetes_predict"):
if 'diabetes' in models:
# Prepare input data
input_data = np.array([[pregnancies, glucose, blood_pressure, skin_thickness,
insulin, bmi, diabetes_pedigree, age]])
# Make prediction
prediction = models['diabetes'].predict(input_data)
probability = models['diabetes'].predict_proba(input_data)
# Display result
if prediction[0] == 1:
st.error(" High risk of diabetes detected!")
st.write(f"Probability: {probability[0][1]:.2%}")
else:
st.success(" Low risk of diabetes")
st.write(f"Probability: {probability[0][0]:.2%}")
else:
st.error("Diabetes model not available")
# Heart Disease Prediction Page
elif disease_option == "Heart Disease":
st.header(" Heart Disease Prediction")
st.write("Please enter your cardiac health information:")
col1, col2, col3 = st.columns(3)
with col1:
age = st.number_input("Age", min_value=1, max_value=120, value=50)
sex = st.selectbox("Sex", ["Male", "Female"])
chest_pain = st.selectbox("Chest Pain Type", [0, 1, 2, 3])
resting_bp = st.number_input("Resting Blood Pressure", min_value=80, max_value=200, value=120)