Skip to content

Instantly share code, notes, and snippets.

@drzamoramora
Created May 7, 2022 15:16
Show Gist options
  • Save drzamoramora/090b4dfaf0e403ceb919a244ec1b2a18 to your computer and use it in GitHub Desktop.
Save drzamoramora/090b4dfaf0e403ceb919a244ec1b2a18 to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
from pycaret.classification import *
# load data
data = pd.read_csv("HepatitisCdata.csv")
data = data.drop(labels='Unnamed: 0', axis=1)
data.head(10)
# clean data
# pd.Series(data["Category"], dtype="category")
# ['0=Blood Donor', '0s=suspect Blood Donor', '1=Hepatitis', '2=Fibrosis', '3=Cirrhosis']
data["Category"] = [0 if x == "0=Blood Donor" else x for x in data["Category"]]
data["Category"] = [1 if x == "0s=suspect Blood Donor" else x for x in data["Category"]]
data["Category"] = [2 if x == "1=Hepatitis" else x for x in data["Category"]]
data["Category"] = [3 if x == "2=Fibrosis" else x for x in data["Category"]]
data["Category"] = [4 if x == "3=Cirrhosis" else x for x in data["Category"]]
# pd.Series(data["Sex"], dtype="category")
# ['f', 'm']
data["Sex"] = [0 if x == "f" else 1 for x in data["Sex"]]
data.head(10)
# pycaret setup
s = setup(data, target = "Category")
# model training and selection
best = compare_models()
# model evaluation analytics
evaluate_model(best)
# save model to disk "hepatitis_model.pkl"
save_model(best, "hepatitis_model")
# load pickle file
from pycaret.classification import load_model
# load model
hepatitis_model = load_model('hepatitis_model')
# classes and sample
classes = ['Blood Donor', 'Suspect Blood Donor', 'Hepatitis', 'Fibrosis', 'Cirrhosis']
patient_data = data.iloc[10,1:]
patient_class = data.iloc[10,0]
print(patient_data, "\n")
print("Patient was labeled as:", classes[patient_class])
# perform the prediction
patient_data = pd.DataFrame()
patient_data = patient_data.append({
'Age': 32.00,
'Sex': 1.00,
'ALB': 44.30,
'ALP': 52.30,
'ALT': 21.70,
'AST': 22.40,
'BIL': 17.20,
'CHE': 4.15,
'CHOL': 3.57,
'CREA': 78.00,
'GGT': 24.10,
'PROT': 75.40
}, ignore_index=True)
# perform prediction
prediction = predict_model(hepatitis_model, data = patient_data)
print('Patient was CLASSIFIED as:', classes[prediction.Label[0]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment