Liver Disease Prediction Using Machine Learning
Table of Contents
Introduction
Liver diseases affect millions globally, posing a significant burden on healthcare systems and individual lives. Liver diseases consists a spectrum of conditions ranging from fatty liver disease to cirrhosis, posing significant challenges to healthcare systems worldwide.
Traditional methods of diagnosing liver diseases often rely on different procedures, such as liver biopsies or expensive imaging techniques, leading to delays in diagnosis and substantial healthcare costs.
Furthermore, these methods may not provide accurate or timely results, highlighting the urgent need for more efficient and reliable diagnostic tools.
In recent years, the integration of machine learning (ML) algorithms in healthcare has revolutionized disease diagnosis.
By leveraging vast amounts of medical data, ML algorithms can analyze complex patterns and relationships that are very difficult for doctors to find.
In this blog, we will be predicting whether a person has liver disease or not using the Kaggle Dataset. We will be using a random forest classifier algorithm for building our machine learning model.
Let's start!
Steps to model building
- First, we will import all the required libraries.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
2. Now we will read the CSV file using the Pandas library.
patients = pd.read_csv('indian_liver_patient.csv')
patients.head()
3. Now let’s check all the features and their data types present in the dataset.
df_liver.info()
We can see from the above output that Gender column is an object datatype, For our machine learning model, we need all the
4. We will replace female with 0 and male with 1 to convert the gender column into integer category.
df_liver['Gender'] = df_liver['Gender'].replace({'Male': 1, 'Female': 0})
df_liver.head()
5. Now let’s check for any null values present in the dataset and drop them.
df_liver.isnull().sum()
df_liver = df_liver.dropna()
6. Now let’s draw a heatmap to see the correlation between all the features.
corr = df_liver.corr()
plt.figure(figsize=(20, 10))
sns.heatmap(corr,annot=True,cmap='Blues')
7. Now let’s assign all the features to the X variable and the target variable to 'Y'.
X = df_liver.drop(columns='Dataset', axis=1)
Y = df_liver['Dataset']
Now let’s split our dataset into train and test in the ratio of 75:25.
X_train,X_test,y_train,y_test = train_test_split(X,Y,test_size=0.25,random_state=42)
8. We will be using Random Forest Classifier for training our model.
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, confusion_matrix,classification_report
model = RandomForestClassifier()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
print("Classification Report:")
print(classification_report(y_test, y_pred))
Our Model has achieved an accuracy of 72.41%
Conclusion
In conclusion, using machine learning to detect liver disease is a big step forward in healthcare. By looking at different types of information, machine learning models can assist doctors in accurately identifying individuals at risk of liver disease at an early stage, This would allow the patients to get treatment sooner, which often leads to better outcomes.
Frequently Asked Questions
Q1) What is the role of machine learning in liver disease detection?
Machine learning algorithms can analyze large amounts of patient's records, such as age, gender, protein, albumin, etc., to identify patterns and predict the presence of liver diseases.
Q2) What machine learning techniques are used to predict liver disease risk?
Some of the main machine learning algorithms used to predict the occurrence of liver diseases are decision trees, K-nearest neighbor, random forests, and support vector machines.
Q3) Why is predicting liver disease important?
Liver diseases often progress silently without noticeable symptoms until they reach advanced stages. Early prediction allows doctors to implement preventive measures, such as lifestyle modification or different medical treatments.