File size: 3,710 Bytes
79eea61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b57d24
f9a1aee
1030a98
f9a1aee
79eea61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f98842
79eea61
 
 
 
 
 
 
 
 
 
 
4f98842
79eea61
 
 
 
 
 
 
 
4f98842
79eea61
 
 
 
 
 
 
998be39
79eea61
 
6451fb4
79eea61
 
6451fb4
79eea61
 
 
 
6451fb4
79eea61
 
6451fb4
79eea61
6451fb4
79eea61
 
 
 
4f98842
79eea61
 
 
 
 
 
 
 
 
 
 
 
4f98842
79eea61
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import gradio as gr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

import seaborn as sns

df = pd.read_csv("diabetes.csv")
df.info()

fig, axs = plt.subplots(4, 2, figsize=(15, 12))
axs = axs.flatten()
sns.distplot(df["Pregnancies"], rug=True, color="#38b000", ax=axs[0])
sns.distplot(df["Glucose"], rug=True, color="#FF9933", ax=axs[1])
sns.distplot(df["BloodPressure"], rug=True, color="#522500", ax=axs[2])
sns.distplot(df["SkinThickness"], rug=True, color="#66b3ff", ax=axs[3])
sns.distplot(df["Insulin"], rug=True, color="#FF6699", ax=axs[4])
sns.distplot(df["BMI"], color="#e76f51", rug=True, ax=axs[5])
sns.distplot(df["DiabetesPedigreeFunction"], color="#03045e", rug=True, ax=axs[6])
sns.distplot(df["Age"], rug=True, color="#333533", ax=axs[7])
plt.show()

bmi_outliers = df[df["BMI"] > 40]
bmi_outliers["BMI"].shape
df["Diabetes"] = df["Outcome"].apply(lambda x: "Diabetic" if x == 1 else "Not_Diabetic")
df["BMI"] = df["BMI"].apply(lambda x: df.BMI.mean() if x > 40 else x)

from pycaret.classification import *
print("Hello 1")
#Diab = setup(data = df,target = 'Diabetes',normalize=True, session_id=1,fix_imbalance = True)
print("2")
Diab = setup(data=df,target="Diabetes",normalize=True,session_id=1,fix_imbalance=True,numeric_features=["Pregnancies"],log_experiment=True,experiment_name="diabetes",ignore_features=["Outcome"],silent=True)

best = compare_models(n_select=15)
compare_model_results = pull()

rf = create_model("rf", fold=10)

tuned_rf = tune_model(rf)

# creating a predict function to be passed into gradio UI
def predict(
    model,
    Pregnancies,
    Glucose,
    BloodPressure,
    BMI,
    SkinThickness,
    Insulin,
    Age,
    DiabetesPedigreeFunction
):

    df = pd.DataFrame.from_dict(
        {
            "Pregnancies": [Pregnancies],
            "Glucose": [Glucose],
            "BloodPressure": [BloodPressure],
            "BMI": [BMI],
            "SkinThickness": [SkinThickness],
            "Insulin": [Insulin],
            "Age": [Age],
            "DiabetesPedigreeFunction": [DiabetesPedigreeFunction]
        }
    )

    model_index = list(compare_model_results["Model"]).index(model)
    model = best[model_index]
    pred = predict_model(model, df, raw_score=True)
    return {
        "Not Diabetic": pred["Score_Not_Diabetic"][0].astype("float64"),
        "Diabetic": pred["Score_Diabetic"][0].astype("float64")
    }


import gradio as gr

model = gr.inputs.Dropdown(list(compare_model_results["Model"]), label="Models")
Pregnancies = gr.inputs.Slider(
    minimum=1, maximum=20,step=1, default=df["Pregnancies"].mean(), label="Pregnancies"
)
Glucose = gr.inputs.Slider(
    minimum=0, maximum=200,step=1 ,default=df["Glucose"].mean(), label="Glucose"
)
BloodPressure = gr.inputs.Slider(
    minimum=0, maximum=140,step=1 ,default=df["BloodPressure"].mean(), label="BloodPressure"
)
BMI = gr.inputs.Slider(minimum=0, maximum=70, default=df["BMI"].mean(), label="BMI")

SkinThickness = gr.inputs.Slider(
    minimum=0, maximum=100,step=1,default=df["SkinThickness"].mean(), label="SkinThickness"
)
Insulin = gr.inputs.Slider(
    minimum=0, maximum=1000,step=1,default=df["Insulin"].mean(), label="Insulin"
)
Age = gr.inputs.Slider(minimum=0,maximum=100,step=1,default=df["Age"].mean(), label="Age")
DiabetesPedigreeFunction = gr.inputs.Slider(
    minimum=0,
    maximum=1,
    default=df["DiabetesPedigreeFunction"].mean(),
    label="DiabetesPedigreeFunction"
)
gr.Interface(
    predict,
    [
        model,
        Pregnancies,
        Glucose,
        BloodPressure,
        BMI,
        SkinThickness,
        Insulin,
        Age,
        DiabetesPedigreeFunction
    ],
    "label",
    live=True,
).launch()