423 KiB
423 KiB
In [2]:
import pickle
import pandas as pd
from sklearn import tree
df = pd.read_csv("data-cardio/cardio_clear.csv", index_col="id")
model = pickle.load(open("data-cardio//cardio.model.sav", "rb"))
features = (
df
.drop(["cardio"], axis=1)
.columns.values.tolist()
)
rules = tree.export_text(model, feature_names=features)
print(rules)
|--- ap_hi <= 129.50 | |--- age <= 54.65 | | |--- cholesterol <= 2.50 | | | |--- age <= 43.79 | | | | |--- cholesterol <= 1.50 | | | | | |--- ap_hi <= 114.50 | | | | | | |--- class: 0 | | | | | |--- ap_hi > 114.50 | | | | | | |--- class: 0 | | | | |--- cholesterol > 1.50 | | | | | |--- bmi <= 28.87 | | | | | | |--- class: 0 | | | | | |--- bmi > 28.87 | | | | | | |--- class: 0 | | | |--- age > 43.79 | | | | |--- ap_hi <= 119.50 | | | | | |--- bmi <= 22.05 | | | | | | |--- class: 0 | | | | | |--- bmi > 22.05 | | | | | | |--- class: 0 | | | | |--- ap_hi > 119.50 | | | | | |--- bmi <= 27.71 | | | | | | |--- class: 0 | | | | | |--- bmi > 27.71 | | | | | | |--- class: 0 | | |--- cholesterol > 2.50 | | | |--- bmi <= 29.04 | | | | |--- age <= 41.60 | | | | | |--- ap_hi <= 115.00 | | | | | | |--- class: 0 | | | | | |--- ap_hi > 115.00 | | | | | | |--- class: 0 | | | | |--- age > 41.60 | | | | | |--- age <= 54.17 | | | | | | |--- class: 1 | | | | | |--- age > 54.17 | | | | | | |--- class: 0 | | | |--- bmi > 29.04 | | | | |--- age <= 54.01 | | | | | |--- age <= 39.75 | | | | | | |--- class: 0 | | | | | |--- age > 39.75 | | | | | | |--- class: 1 | | | | |--- age > 54.01 | | | | | |--- bmi <= 35.02 | | | | | | |--- class: 0 | | | | | |--- bmi > 35.02 | | | | | | |--- class: 1 | |--- age > 54.65 | | |--- cholesterol <= 2.50 | | | |--- age <= 60.71 | | | | |--- ap_hi <= 118.50 | | | | | |--- bmi <= 23.33 | | | | | | |--- class: 0 | | | | | |--- bmi > 23.33 | | | | | | |--- class: 0 | | | | |--- ap_hi > 118.50 | | | | | |--- bmi <= 32.89 | | | | | | |--- class: 0 | | | | | |--- bmi > 32.89 | | | | | | |--- class: 1 | | | |--- age > 60.71 | | | | |--- bmi <= 20.51 | | | | | |--- age <= 64.31 | | | | | | |--- class: 0 | | | | | |--- age > 64.31 | | | | | | |--- class: 1 | | | | |--- bmi > 20.51 | | | | | |--- ap_hi <= 115.50 | | | | | | |--- class: 0 | | | | | |--- ap_hi > 115.50 | | | | | | |--- class: 1 | | |--- cholesterol > 2.50 | | | |--- bmi <= 26.03 | | | | |--- age <= 60.89 | | | | | |--- age <= 60.48 | | | | | | |--- class: 1 | | | | | |--- age > 60.48 | | | | | | |--- class: 0 | | | | |--- age > 60.89 | | | | | |--- bmi <= 25.91 | | | | | | |--- class: 1 | | | | | |--- bmi > 25.91 | | | | | | |--- class: 0 | | | |--- bmi > 26.03 | | | | |--- age <= 59.39 | | | | | |--- bmi <= 35.93 | | | | | | |--- class: 1 | | | | | |--- bmi > 35.93 | | | | | | |--- class: 1 | | | | |--- age > 59.39 | | | | | |--- bmi <= 35.12 | | | | | | |--- class: 1 | | | | | |--- bmi > 35.12 | | | | | | |--- class: 1 |--- ap_hi > 129.50 | |--- ap_hi <= 138.50 | | |--- cholesterol <= 2.50 | | | |--- age <= 59.54 | | | | |--- bmi <= 21.64 | | | | | |--- bmi <= 17.30 | | | | | | |--- class: 1 | | | | | |--- bmi > 17.30 | | | | | | |--- class: 0 | | | | |--- bmi > 21.64 | | | | | |--- age <= 39.99 | | | | | | |--- class: 0 | | | | | |--- age > 39.99 | | | | | | |--- class: 1 | | | |--- age > 59.54 | | | | |--- age <= 62.46 | | | | | |--- bmi <= 20.61 | | | | | | |--- class: 0 | | | | | |--- bmi > 20.61 | | | | | | |--- class: 1 | | | | |--- age > 62.46 | | | | | |--- age <= 64.00 | | | | | | |--- class: 1 | | | | | |--- age > 64.00 | | | | | | |--- class: 1 | | |--- cholesterol > 2.50 | | | |--- bmi <= 30.74 | | | | |--- bmi <= 30.06 | | | | | |--- bmi <= 23.93 | | | | | | |--- class: 1 | | | | | |--- bmi > 23.93 | | | | | | |--- class: 1 | | | | |--- bmi > 30.06 | | | | | |--- bmi <= 30.69 | | | | | | |--- class: 1 | | | | | |--- bmi > 30.69 | | | | | | |--- class: 0 | | | |--- bmi > 30.74 | | | | |--- bmi <= 32.05 | | | | | |--- age <= 43.63 | | | | | | |--- class: 0 | | | | | |--- age > 43.63 | | | | | | |--- class: 1 | | | | |--- bmi > 32.05 | | | | | |--- bmi <= 32.34 | | | | | | |--- class: 1 | | | | | |--- bmi > 32.34 | | | | | | |--- class: 1 | |--- ap_hi > 138.50 | | |--- ap_hi <= 149.50 | | | |--- age <= 39.56 | | | | |--- bmi <= 38.19 | | | | | |--- age <= 39.54 | | | | | | |--- class: 1 | | | | | |--- age > 39.54 | | | | | | |--- class: 0 | | | | |--- bmi > 38.19 | | | | | |--- bmi <= 50.55 | | | | | | |--- class: 0 | | | | | |--- bmi > 50.55 | | | | | | |--- class: 1 | | | |--- age > 39.56 | | | | |--- age <= 47.57 | | | | | |--- bmi <= 19.23 | | | | | | |--- class: 0 | | | | | |--- bmi > 19.23 | | | | | | |--- class: 1 | | | | |--- age > 47.57 | | | | | |--- age <= 61.57 | | | | | | |--- class: 1 | | | | | |--- age > 61.57 | | | | | | |--- class: 1 | | |--- ap_hi > 149.50 | | | |--- bmi <= 20.48 | | | | |--- age <= 64.27 | | | | | |--- age <= 55.82 | | | | | | |--- class: 1 | | | | | |--- age > 55.82 | | | | | | |--- class: 1 | | | | |--- age > 64.27 | | | | | |--- class: 0 | | | |--- bmi > 20.48 | | | | |--- age <= 64.35 | | | | | |--- age <= 49.82 | | | | | | |--- class: 1 | | | | | |--- age > 49.82 | | | | | | |--- class: 1 | | | | |--- age > 64.35 | | | | | |--- bmi <= 36.80 | | | | | | |--- class: 1 | | | | | |--- bmi > 36.80 | | | | | | |--- class: 0
In [3]:
from src.rules import get_rules
rules = get_rules(model, features, [0, 1])
display(len(rules))
rules
63
Out[3]:
[if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (age <= 43.792) and (cholesterol <= 1.5) and (ap_hi <= 114.5) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (age <= 43.792) and (cholesterol <= 1.5) and (ap_hi > 114.5) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (age <= 43.792) and (cholesterol > 1.5) and (bmi <= 28.874) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (age <= 43.792) and (cholesterol > 1.5) and (bmi > 28.874) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (age > 43.792) and (ap_hi <= 119.5) and (bmi <= 22.045) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (age > 43.792) and (ap_hi <= 119.5) and (bmi > 22.045) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (age > 43.792) and (ap_hi > 119.5) and (bmi <= 27.71) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (age > 43.792) and (ap_hi > 119.5) and (bmi > 27.71) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi <= 29.043) and (age <= 41.599) and (ap_hi <= 115.0) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi <= 29.043) and (age <= 41.599) and (ap_hi > 115.0) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi <= 29.043) and (age > 41.599) and (age <= 54.167) -> 1, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi <= 29.043) and (age > 41.599) and (age > 54.167) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi > 29.043) and (age <= 54.008) and (age <= 39.751) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi > 29.043) and (age <= 54.008) and (age > 39.751) -> 1, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi > 29.043) and (age > 54.008) and (bmi <= 35.021) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi > 29.043) and (age > 54.008) and (bmi > 35.021) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (age <= 60.707) and (ap_hi <= 118.5) and (bmi <= 23.329) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (age <= 60.707) and (ap_hi <= 118.5) and (bmi > 23.329) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (age <= 60.707) and (ap_hi > 118.5) and (bmi <= 32.886) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (age <= 60.707) and (ap_hi > 118.5) and (bmi > 32.886) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (age > 60.707) and (bmi <= 20.512) and (age <= 64.308) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (age > 60.707) and (bmi <= 20.512) and (age > 64.308) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (age > 60.707) and (bmi > 20.512) and (ap_hi <= 115.5) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (age > 60.707) and (bmi > 20.512) and (ap_hi > 115.5) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi <= 26.032) and (age <= 60.891) and (age <= 60.479) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi <= 26.032) and (age <= 60.891) and (age > 60.479) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi <= 26.032) and (age > 60.891) and (bmi <= 25.912) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi <= 26.032) and (age > 60.891) and (bmi > 25.912) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi > 26.032) and (age <= 59.39) and (bmi <= 35.932) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi > 26.032) and (age <= 59.39) and (bmi > 35.932) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi > 26.032) and (age > 59.39) and (bmi <= 35.121) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi > 26.032) and (age > 59.39) and (bmi > 35.121) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi <= 21.637) and (bmi <= 17.3) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi <= 21.637) and (bmi > 17.3) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi > 21.637) and (age <= 39.989) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi > 21.637) and (age > 39.989) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 62.463) and (bmi <= 20.614) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 62.463) and (bmi > 20.614) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age > 62.463) and (age <= 63.998) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age > 62.463) and (age > 63.998) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) and (bmi <= 30.056) and (bmi <= 23.927) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) and (bmi <= 30.056) and (bmi > 23.927) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) and (bmi > 30.056) and (bmi <= 30.69) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) and (bmi > 30.056) and (bmi > 30.69) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.049) and (age <= 43.632) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.049) and (age > 43.632) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi > 32.049) and (bmi <= 32.337) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi > 32.049) and (bmi > 32.337) -> 1, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi <= 38.186) and (age <= 39.538) -> 1, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi <= 38.186) and (age > 39.538) -> 0, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi > 38.186) and (bmi <= 50.547) -> 0, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi > 38.186) and (bmi > 50.547) -> 1, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 47.569) and (bmi <= 19.231) -> 0, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 47.569) and (bmi > 19.231) -> 1, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi <= 149.5) and (age > 39.558) and (age > 47.569) and (age <= 61.572) -> 1, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi <= 149.5) and (age > 39.558) and (age > 47.569) and (age > 61.572) -> 1, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi > 149.5) and (bmi <= 20.482) and (age <= 64.269) and (age <= 55.817) -> 1, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi > 149.5) and (bmi <= 20.482) and (age <= 64.269) and (age > 55.817) -> 1, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi > 149.5) and (bmi <= 20.482) and (age > 64.269) -> 0, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi > 149.5) and (bmi > 20.482) and (age <= 64.351) and (age <= 49.818) -> 1, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi > 149.5) and (bmi > 20.482) and (age <= 64.351) and (age > 49.818) -> 1, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi > 149.5) and (bmi > 20.482) and (age > 64.351) and (bmi <= 36.796) -> 1, if (ap_hi > 129.5) and (ap_hi > 138.5) and (ap_hi > 149.5) and (bmi > 20.482) and (age > 64.351) and (bmi > 36.796) -> 0]
In [4]:
from src.rules import normalise_rules
rules = normalise_rules(rules)
display(len(rules))
rules
63
Out[4]:
[if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) -> 0, if (ap_hi <= 129.5) and (ap_hi > 114.5) and (age <= 54.65) and (cholesterol <= 2.5) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (cholesterol > 1.5) and (bmi <= 28.874) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (cholesterol > 1.5) and (bmi > 28.874) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi <= 22.045) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi > 22.045) -> 0, if (ap_hi <= 129.5) and (ap_hi > 119.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi <= 27.71) -> 0, if (ap_hi <= 129.5) and (ap_hi > 119.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi > 27.71) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi <= 29.043) -> 0, if (ap_hi <= 129.5) and (ap_hi > 115.0) and (age <= 54.65) and (cholesterol > 2.5) and (bmi <= 29.043) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 41.599) and (cholesterol > 2.5) and (bmi <= 29.043) -> 1, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 41.599) and (cholesterol > 2.5) and (bmi <= 29.043) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi > 29.043) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 39.751) and (cholesterol > 2.5) and (bmi > 29.043) -> 1, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 54.008) and (cholesterol > 2.5) and (bmi > 29.043) and (bmi <= 35.021) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 54.008) and (cholesterol > 2.5) and (bmi > 29.043) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi <= 23.329) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi > 23.329) -> 0, if (ap_hi <= 129.5) and (ap_hi > 118.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi <= 32.886) -> 0, if (ap_hi <= 129.5) and (ap_hi > 118.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi > 32.886) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 64.308) and (cholesterol <= 2.5) and (bmi <= 20.512) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (bmi <= 20.512) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (bmi > 20.512) -> 0, if (ap_hi <= 129.5) and (ap_hi > 115.5) and (age > 54.65) and (cholesterol <= 2.5) and (bmi > 20.512) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 60.891) and (cholesterol > 2.5) and (bmi <= 26.032) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 60.891) and (cholesterol > 2.5) and (bmi <= 26.032) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi <= 26.032) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi <= 26.032) and (bmi > 25.912) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 59.39) and (cholesterol > 2.5) and (bmi > 26.032) and (bmi <= 35.932) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 59.39) and (cholesterol > 2.5) and (bmi > 26.032) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi > 26.032) and (bmi <= 35.121) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi > 26.032) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi <= 21.637) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi <= 21.637) and (bmi > 17.3) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi > 21.637) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (age > 39.989) and (bmi > 21.637) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 62.463) and (bmi <= 20.614) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 62.463) and (bmi > 20.614) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 63.998) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) and (bmi > 23.927) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) and (bmi > 30.056) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) and (bmi > 30.056) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.049) and (age <= 43.632) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.049) and (age > 43.632) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.337) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi <= 38.186) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (age > 39.538) and (bmi <= 38.186) -> 0, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi > 38.186) and (bmi <= 50.547) -> 0, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi > 38.186) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 47.569) and (bmi <= 19.231) -> 0, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 47.569) and (bmi > 19.231) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 61.572) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) -> 1, if (ap_hi > 129.5) and (bmi <= 20.482) and (age <= 64.269) -> 1, if (ap_hi > 129.5) and (bmi <= 20.482) and (age <= 64.269) and (age > 55.817) -> 1, if (ap_hi > 129.5) and (bmi <= 20.482) and (age > 64.269) -> 0, if (ap_hi > 129.5) and (bmi > 20.482) and (age <= 64.351) -> 1, if (ap_hi > 129.5) and (bmi > 20.482) and (age <= 64.351) and (age > 49.818) -> 1, if (ap_hi > 129.5) and (bmi > 20.482) and (bmi <= 36.796) and (age > 64.351) -> 1, if (ap_hi > 129.5) and (bmi > 20.482) and (age > 64.351) -> 0]
In [5]:
from src.rules import delete_same_rules
rules = delete_same_rules(rules)
display(len(rules))
rules
60
Out[5]:
[if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) -> 0, if (ap_hi <= 129.5) and (ap_hi > 114.5) and (age <= 54.65) and (cholesterol <= 2.5) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (cholesterol > 1.5) and (bmi <= 28.874) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (cholesterol > 1.5) and (bmi > 28.874) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi <= 22.045) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi > 22.045) -> 0, if (ap_hi <= 129.5) and (ap_hi > 119.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi <= 27.71) -> 0, if (ap_hi <= 129.5) and (ap_hi > 119.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi > 27.71) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi <= 29.043) -> 0, if (ap_hi <= 129.5) and (ap_hi > 115.0) and (age <= 54.65) and (cholesterol > 2.5) and (bmi <= 29.043) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 41.599) and (cholesterol > 2.5) and (bmi <= 29.043) -> 0.5, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi > 29.043) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 39.751) and (cholesterol > 2.5) and (bmi > 29.043) -> 1, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 54.008) and (cholesterol > 2.5) and (bmi > 29.043) and (bmi <= 35.021) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 54.008) and (cholesterol > 2.5) and (bmi > 29.043) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi <= 23.329) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi > 23.329) -> 0, if (ap_hi <= 129.5) and (ap_hi > 118.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi <= 32.886) -> 0, if (ap_hi <= 129.5) and (ap_hi > 118.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi > 32.886) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 64.308) and (cholesterol <= 2.5) and (bmi <= 20.512) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (bmi <= 20.512) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (bmi > 20.512) -> 0, if (ap_hi <= 129.5) and (ap_hi > 115.5) and (age > 54.65) and (cholesterol <= 2.5) and (bmi > 20.512) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 60.891) and (cholesterol > 2.5) and (bmi <= 26.032) -> 0.5, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi <= 26.032) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi <= 26.032) and (bmi > 25.912) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 59.39) and (cholesterol > 2.5) and (bmi > 26.032) and (bmi <= 35.932) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 59.39) and (cholesterol > 2.5) and (bmi > 26.032) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi > 26.032) and (bmi <= 35.121) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi > 26.032) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi <= 21.637) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi <= 21.637) and (bmi > 17.3) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi > 21.637) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (age > 39.989) and (bmi > 21.637) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 62.463) and (bmi <= 20.614) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 62.463) and (bmi > 20.614) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 63.998) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) and (bmi > 23.927) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) and (bmi > 30.056) -> 0.5, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.049) and (age <= 43.632) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.049) and (age > 43.632) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.337) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi <= 38.186) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (age > 39.538) and (bmi <= 38.186) -> 0, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi > 38.186) and (bmi <= 50.547) -> 0, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi > 38.186) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 47.569) and (bmi <= 19.231) -> 0, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 47.569) and (bmi > 19.231) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 61.572) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) -> 1, if (ap_hi > 129.5) and (bmi <= 20.482) and (age <= 64.269) -> 1, if (ap_hi > 129.5) and (bmi <= 20.482) and (age <= 64.269) and (age > 55.817) -> 1, if (ap_hi > 129.5) and (bmi <= 20.482) and (age > 64.269) -> 0, if (ap_hi > 129.5) and (bmi > 20.482) and (age <= 64.351) -> 1, if (ap_hi > 129.5) and (bmi > 20.482) and (age <= 64.351) and (age > 49.818) -> 1, if (ap_hi > 129.5) and (bmi > 20.482) and (bmi <= 36.796) and (age > 64.351) -> 1, if (ap_hi > 129.5) and (bmi > 20.482) and (age > 64.351) -> 0]
In [102]:
rules = [
rule for rule in rules if rule.get_consequent() == 0 or rule.get_consequent() == 1
]
display(len(rules))
rules
57
Out[102]:
[if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) -> 0, if (ap_hi <= 129.5) and (ap_hi > 114.5) and (age <= 54.65) and (cholesterol <= 2.5) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (cholesterol > 1.5) and (bmi <= 28.874) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (cholesterol > 1.5) and (bmi > 28.874) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi <= 22.045) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi > 22.045) -> 0, if (ap_hi <= 129.5) and (ap_hi > 119.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi <= 27.71) -> 0, if (ap_hi <= 129.5) and (ap_hi > 119.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi > 27.71) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi <= 29.043) -> 0, if (ap_hi <= 129.5) and (ap_hi > 115.0) and (age <= 54.65) and (cholesterol > 2.5) and (bmi <= 29.043) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi > 29.043) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 39.751) and (cholesterol > 2.5) and (bmi > 29.043) -> 1, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 54.008) and (cholesterol > 2.5) and (bmi > 29.043) and (bmi <= 35.021) -> 0, if (ap_hi <= 129.5) and (age <= 54.65) and (age > 54.008) and (cholesterol > 2.5) and (bmi > 29.043) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi <= 23.329) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi > 23.329) -> 0, if (ap_hi <= 129.5) and (ap_hi > 118.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi <= 32.886) -> 0, if (ap_hi <= 129.5) and (ap_hi > 118.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi > 32.886) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 64.308) and (cholesterol <= 2.5) and (bmi <= 20.512) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (bmi <= 20.512) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (bmi > 20.512) -> 0, if (ap_hi <= 129.5) and (ap_hi > 115.5) and (age > 54.65) and (cholesterol <= 2.5) and (bmi > 20.512) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi <= 26.032) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi <= 26.032) and (bmi > 25.912) -> 0, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 59.39) and (cholesterol > 2.5) and (bmi > 26.032) and (bmi <= 35.932) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (age <= 59.39) and (cholesterol > 2.5) and (bmi > 26.032) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi > 26.032) and (bmi <= 35.121) -> 1, if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi > 26.032) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi <= 21.637) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi <= 21.637) and (bmi > 17.3) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi > 21.637) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (age > 39.989) and (bmi > 21.637) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 62.463) and (bmi <= 20.614) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 62.463) and (bmi > 20.614) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 63.998) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) and (bmi > 23.927) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.049) and (age <= 43.632) -> 0, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.049) and (age > 43.632) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.337) -> 1, if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi <= 38.186) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (age > 39.538) and (bmi <= 38.186) -> 0, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi > 38.186) and (bmi <= 50.547) -> 0, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi > 38.186) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 47.569) and (bmi <= 19.231) -> 0, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 47.569) and (bmi > 19.231) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 61.572) -> 1, if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) -> 1, if (ap_hi > 129.5) and (bmi <= 20.482) and (age <= 64.269) -> 1, if (ap_hi > 129.5) and (bmi <= 20.482) and (age <= 64.269) and (age > 55.817) -> 1, if (ap_hi > 129.5) and (bmi <= 20.482) and (age > 64.269) -> 0, if (ap_hi > 129.5) and (bmi > 20.482) and (age <= 64.351) -> 1, if (ap_hi > 129.5) and (bmi > 20.482) and (age <= 64.351) and (age > 49.818) -> 1, if (ap_hi > 129.5) and (bmi > 20.482) and (bmi <= 36.796) and (age > 64.351) -> 1, if (ap_hi > 129.5) and (bmi > 20.482) and (age > 64.351) -> 0]
In [103]:
from src.rules import get_features, vectorize_rules
features = get_features(rules, [])
print(features)
df_rules = vectorize_rules(rules, features)
df_rules.head(5)
['(age <= 39.558)', '(age <= 43.632)', '(age <= 47.569)', '(age <= 54.65)', '(age <= 59.39)', '(age <= 59.536)', '(age <= 60.707)', '(age <= 61.572)', '(age <= 62.463)', '(age <= 63.998)', '(age <= 64.269)', '(age <= 64.308)', '(age <= 64.351)', '(age > 39.538)', '(age > 39.558)', '(age > 39.751)', '(age > 39.989)', '(age > 43.632)', '(age > 43.792)', '(age > 49.818)', '(age > 54.008)', '(age > 54.65)', '(age > 55.817)', '(age > 59.536)', '(age > 64.269)', '(age > 64.351)', '(ap_hi <= 129.5)', '(ap_hi <= 138.5)', '(ap_hi <= 149.5)', '(ap_hi > 114.5)', '(ap_hi > 115.0)', '(ap_hi > 115.5)', '(ap_hi > 118.5)', '(ap_hi > 119.5)', '(ap_hi > 129.5)', '(bmi <= 19.231)', '(bmi <= 20.482)', '(bmi <= 20.512)', '(bmi <= 20.614)', '(bmi <= 21.637)', '(bmi <= 22.045)', '(bmi <= 23.329)', '(bmi <= 26.032)', '(bmi <= 27.71)', '(bmi <= 28.874)', '(bmi <= 29.043)', '(bmi <= 30.744)', '(bmi <= 32.049)', '(bmi <= 32.337)', '(bmi <= 32.886)', '(bmi <= 35.021)', '(bmi <= 35.121)', '(bmi <= 35.932)', '(bmi <= 36.796)', '(bmi <= 38.186)', '(bmi <= 50.547)', '(bmi > 17.3)', '(bmi > 19.231)', '(bmi > 20.482)', '(bmi > 20.512)', '(bmi > 20.614)', '(bmi > 21.637)', '(bmi > 22.045)', '(bmi > 23.329)', '(bmi > 23.927)', '(bmi > 25.912)', '(bmi > 26.032)', '(bmi > 27.71)', '(bmi > 28.874)', '(bmi > 29.043)', '(bmi > 30.744)', '(bmi > 32.886)', '(bmi > 38.186)', '(cholesterol <= 2.5)', '(cholesterol > 1.5)', '(cholesterol > 2.5)']
Out[103]:
(age <= 39.558) | (age <= 43.632) | (age <= 47.569) | (age <= 54.65) | (age <= 59.39) | (age <= 59.536) | (age <= 60.707) | (age <= 61.572) | (age <= 62.463) | (age <= 63.998) | ... | (bmi > 27.71) | (bmi > 28.874) | (bmi > 29.043) | (bmi > 30.744) | (bmi > 32.886) | (bmi > 38.186) | (cholesterol <= 2.5) | (cholesterol > 1.5) | (cholesterol > 2.5) | consequent | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
rule | |||||||||||||||||||||
if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) -> 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
if (ap_hi <= 129.5) and (ap_hi > 114.5) and (age <= 54.65) and (cholesterol <= 2.5) -> 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (cholesterol > 1.5) and (bmi <= 28.874) -> 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |
if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (cholesterol > 1.5) and (bmi > 28.874) -> 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 |
if (ap_hi <= 129.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi <= 22.045) -> 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
5 rows × 77 columns
In [104]:
from src.cluster_helper import draw_best_clusters_plot, get_best_clusters_num
random_state = 9
X = df_rules.copy()
X = X.drop(["consequent"], axis=1)
clusters_score = get_best_clusters_num(X, random_state)
display(clusters_score)
draw_best_clusters_plot(clusters_score)
clusters_num = sorted(clusters_score.items(), key=lambda x: x[1], reverse=True)[0][0]
display(f"The best clusters count is {clusters_num}")
{2: 0.2028684211063448, 3: 0.16350739364416753, 4: 0.17115418740422497, 5: 0.18051062435509244, 6: 0.17312188913678084, 7: 0.20265014439953413, 8: 0.2470239144182239, 9: 0.26319032892830624}
'The best clusters count is 9'
In [106]:
from sklearn import cluster
from src.cluster_helper import print_cluster_result
kmeans = cluster.KMeans(n_clusters=clusters_num, random_state=random_state)
kmeans.fit(X)
print_cluster_result(X, clusters_num, kmeans.labels_)
Кластер 1 (16): if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) -> 0; if (ap_hi <= 129.5) and (ap_hi > 114.5) and (age <= 54.65) and (cholesterol <= 2.5) -> 0; if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (cholesterol > 1.5) and (bmi <= 28.874) -> 0; if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol <= 2.5) and (cholesterol > 1.5) and (bmi > 28.874) -> 0; if (ap_hi <= 129.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi <= 22.045) -> 0; if (ap_hi <= 129.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi > 22.045) -> 0; if (ap_hi <= 129.5) and (ap_hi > 119.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi <= 27.71) -> 0; if (ap_hi <= 129.5) and (ap_hi > 119.5) and (age <= 54.65) and (age > 43.792) and (cholesterol <= 2.5) and (bmi > 27.71) -> 0; if (ap_hi <= 129.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi <= 23.329) -> 0; if (ap_hi <= 129.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi > 23.329) -> 0; if (ap_hi <= 129.5) and (ap_hi > 118.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi <= 32.886) -> 0; if (ap_hi <= 129.5) and (ap_hi > 118.5) and (age > 54.65) and (age <= 60.707) and (cholesterol <= 2.5) and (bmi > 32.886) -> 1; if (ap_hi <= 129.5) and (age > 54.65) and (age <= 64.308) and (cholesterol <= 2.5) and (bmi <= 20.512) -> 0; if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (bmi <= 20.512) -> 1; if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol <= 2.5) and (bmi > 20.512) -> 0; if (ap_hi <= 129.5) and (ap_hi > 115.5) and (age > 54.65) and (cholesterol <= 2.5) and (bmi > 20.512) -> 1 -------- Кластер 2 (8): if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi <= 21.637) -> 1; if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi <= 21.637) and (bmi > 17.3) -> 0; if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (bmi > 21.637) -> 0; if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age <= 59.536) and (age > 39.989) and (bmi > 21.637) -> 1; if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 62.463) and (bmi <= 20.614) -> 0; if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 62.463) and (bmi > 20.614) -> 1; if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) and (age <= 63.998) -> 1; if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol <= 2.5) and (age > 59.536) -> 1 -------- Кластер 3 (6): if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi <= 26.032) -> 1; if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi <= 26.032) and (bmi > 25.912) -> 0; if (ap_hi <= 129.5) and (age > 54.65) and (age <= 59.39) and (cholesterol > 2.5) and (bmi > 26.032) and (bmi <= 35.932) -> 1; if (ap_hi <= 129.5) and (age > 54.65) and (age <= 59.39) and (cholesterol > 2.5) and (bmi > 26.032) -> 1; if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi > 26.032) and (bmi <= 35.121) -> 1; if (ap_hi <= 129.5) and (age > 54.65) and (cholesterol > 2.5) and (bmi > 26.032) -> 1 -------- Кластер 4 (3): if (ap_hi > 129.5) and (bmi <= 20.482) and (age <= 64.269) -> 1; if (ap_hi > 129.5) and (bmi <= 20.482) and (age <= 64.269) and (age > 55.817) -> 1; if (ap_hi > 129.5) and (bmi <= 20.482) and (age > 64.269) -> 0 -------- Кластер 5 (4): if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi <= 38.186) -> 1; if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (age > 39.538) and (bmi <= 38.186) -> 0; if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi > 38.186) and (bmi <= 50.547) -> 0; if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age <= 39.558) and (bmi > 38.186) -> 1 -------- Кластер 6 (6): if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) -> 1; if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi <= 30.744) and (bmi > 23.927) -> 1; if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.049) and (age <= 43.632) -> 0; if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.049) and (age > 43.632) -> 1; if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) and (bmi <= 32.337) -> 1; if (ap_hi > 129.5) and (ap_hi <= 138.5) and (cholesterol > 2.5) and (bmi > 30.744) -> 1 -------- Кластер 7 (4): if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 47.569) and (bmi <= 19.231) -> 0; if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 47.569) and (bmi > 19.231) -> 1; if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) and (age <= 61.572) -> 1; if (ap_hi > 129.5) and (ap_hi <= 149.5) and (age > 39.558) -> 1 -------- Кластер 8 (6): if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi <= 29.043) -> 0; if (ap_hi <= 129.5) and (ap_hi > 115.0) and (age <= 54.65) and (cholesterol > 2.5) and (bmi <= 29.043) -> 0; if (ap_hi <= 129.5) and (age <= 54.65) and (cholesterol > 2.5) and (bmi > 29.043) -> 0; if (ap_hi <= 129.5) and (age <= 54.65) and (age > 39.751) and (cholesterol > 2.5) and (bmi > 29.043) -> 1; if (ap_hi <= 129.5) and (age <= 54.65) and (age > 54.008) and (cholesterol > 2.5) and (bmi > 29.043) and (bmi <= 35.021) -> 0; if (ap_hi <= 129.5) and (age <= 54.65) and (age > 54.008) and (cholesterol > 2.5) and (bmi > 29.043) -> 1 -------- Кластер 9 (4): if (ap_hi > 129.5) and (bmi > 20.482) and (age <= 64.351) -> 1; if (ap_hi > 129.5) and (bmi > 20.482) and (age <= 64.351) and (age > 49.818) -> 1; if (ap_hi > 129.5) and (bmi > 20.482) and (bmi <= 36.796) and (age > 64.351) -> 1; if (ap_hi > 129.5) and (bmi > 20.482) and (age > 64.351) -> 0 --------
In [107]:
from src.rules import simplify_and_group_rules
clustered_rules = simplify_and_group_rules(
df, rules, clusters_num, kmeans.labels_
)
clustered_rules
Out[107]:
[[if (ap_hi = 7) and (age = 29.564) and (cholesterol = 1) -> 0, if (ap_hi = 122.0) and (age = 29.564) and (cholesterol = 1) -> 0, if (ap_hi = 7) and (age = 29.564) and (cholesterol = 2.0) and (bmi = 3.472) -> 0, if (ap_hi = 7) and (age = 29.564) and (cholesterol = 2.0) and (bmi = 298.667) -> 0, if (ap_hi = 7) and (age = 49.221) and (cholesterol = 1) and (bmi = 3.472) -> 0, if (ap_hi = 7) and (age = 49.221) and (cholesterol = 1) and (bmi = 298.667) -> 0, if (ap_hi = 124.5) and (age = 49.221) and (cholesterol = 1) and (bmi = 3.472) -> 0, if (ap_hi = 124.5) and (age = 49.221) and (cholesterol = 1) and (bmi = 298.667) -> 0, if (ap_hi = 7) and (age = 57.679) and (cholesterol = 1) and (bmi = 3.472) -> 0, if (ap_hi = 7) and (age = 57.679) and (cholesterol = 1) and (bmi = 298.667) -> 0, if (ap_hi = 124.0) and (age = 57.679) and (cholesterol = 1) and (bmi = 3.472) -> 0, if (ap_hi = 124.0) and (age = 57.679) and (cholesterol = 1) and (bmi = 298.667) -> 1, if (ap_hi = 7) and (age = 59.479) and (cholesterol = 1) and (bmi = 3.472) -> 0, if (ap_hi = 7) and (age = 64.924) and (cholesterol = 1) and (bmi = 3.472) -> 1, if (ap_hi = 7) and (age = 64.924) and (cholesterol = 1) and (bmi = 298.667) -> 0, if (ap_hi = 122.5) and (age = 64.924) and (cholesterol = 1) and (bmi = 298.667) -> 1], [if (ap_hi = 134.0) and (cholesterol = 1) and (age = 29.564) and (bmi = 3.472) -> 1, if (ap_hi = 134.0) and (cholesterol = 1) and (age = 29.564) and (bmi = 19.469) -> 0, if (ap_hi = 134.0) and (cholesterol = 1) and (age = 29.564) and (bmi = 298.667) -> 0, if (ap_hi = 134.0) and (cholesterol = 1) and (age = 49.762) and (bmi = 298.667) -> 1, if (ap_hi = 134.0) and (cholesterol = 1) and (age = 61.0) and (bmi = 3.472) -> 0, if (ap_hi = 134.0) and (cholesterol = 1) and (age = 61.0) and (bmi = 298.667) -> 1, if (ap_hi = 134.0) and (cholesterol = 1) and (age = 61.767) -> 1, if (ap_hi = 134.0) and (cholesterol = 1) and (age = 64.924) -> 1], [if (ap_hi = 7) and (age = 64.924) and (cholesterol = 3) and (bmi = 3.472) -> 1, if (ap_hi = 7) and (age = 64.924) and (cholesterol = 3) and (bmi = 25.972) -> 0, if (ap_hi = 7) and (age = 57.02) and (cholesterol = 3) and (bmi = 30.982) -> 1, if (ap_hi = 7) and (age = 57.02) and (cholesterol = 3) and (bmi = 298.667) -> 1, if (ap_hi = 7) and (age = 64.924) and (cholesterol = 3) and (bmi = 30.577) -> 1, if (ap_hi = 7) and (age = 64.924) and (cholesterol = 3) and (bmi = 298.667) -> 1], [if (ap_hi = 240) and (bmi = 3.472) and (age = 29.564) -> 1, if (ap_hi = 240) and (bmi = 3.472) and (age = 60.043) -> 1, if (ap_hi = 240) and (bmi = 3.472) and (age = 64.924) -> 0], [if (ap_hi = 139.5) and (age = 29.564) and (bmi = 3.472) -> 1, if (ap_hi = 139.5) and (age = 39.548) and (bmi = 3.472) -> 0, if (ap_hi = 139.5) and (age = 29.564) and (bmi = 44.367) -> 0, if (ap_hi = 139.5) and (age = 29.564) and (bmi = 298.667) -> 1], [if (ap_hi = 134.0) and (cholesterol = 3) and (bmi = 3.472) -> 1, if (ap_hi = 134.0) and (cholesterol = 3) and (bmi = 27.336) -> 1, if (ap_hi = 134.0) and (cholesterol = 3) and (bmi = 31.396) and (age = 29.564) -> 0, if (ap_hi = 134.0) and (cholesterol = 3) and (bmi = 31.396) and (age = 64.924) -> 1, if (ap_hi = 134.0) and (cholesterol = 3) and (bmi = 31.54) -> 1, if (ap_hi = 134.0) and (cholesterol = 3) and (bmi = 298.667) -> 1], [if (ap_hi = 139.5) and (age = 43.563) and (bmi = 3.472) -> 0, if (ap_hi = 139.5) and (age = 43.563) and (bmi = 298.667) -> 1, if (ap_hi = 139.5) and (age = 50.565) -> 1, if (ap_hi = 139.5) and (age = 64.924) -> 1], [if (ap_hi = 7) and (age = 29.564) and (cholesterol = 3) and (bmi = 3.472) -> 0, if (ap_hi = 122.25) and (age = 29.564) and (cholesterol = 3) and (bmi = 3.472) -> 0, if (ap_hi = 7) and (age = 29.564) and (cholesterol = 3) and (bmi = 298.667) -> 0, if (ap_hi = 7) and (age = 47.2) and (cholesterol = 3) and (bmi = 298.667) -> 1, if (ap_hi = 7) and (age = 54.329) and (cholesterol = 3) and (bmi = 32.032) -> 0, if (ap_hi = 7) and (age = 54.329) and (cholesterol = 3) and (bmi = 298.667) -> 1], [if (ap_hi = 240) and (bmi = 298.667) and (age = 29.564) -> 1, if (ap_hi = 240) and (bmi = 298.667) and (age = 57.084) -> 1, if (ap_hi = 240) and (bmi = 28.639) and (age = 64.924) -> 1, if (ap_hi = 240) and (bmi = 298.667) and (age = 64.924) -> 0]]
In [108]:
df.describe().transpose()
Out[108]:
count | mean | std | min | 25% | 50% | 75% | max | |
---|---|---|---|---|---|---|---|---|
age | 68985.0 | 53.290421 | 6.757633 | 29.564122 | 48.340817 | 53.939875 | 58.380791 | 64.924433 |
ap_hi | 68985.0 | 126.325027 | 17.698621 | 7.000000 | 120.000000 | 120.000000 | 140.000000 | 240.000000 |
cholesterol | 68985.0 | 1.364384 | 0.678691 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 3.000000 |
cardio | 68985.0 | 0.494905 | 0.499978 | 0.000000 | 0.000000 | 0.000000 | 1.000000 | 1.000000 |
bmi | 68985.0 | 27.524761 | 6.081130 | 3.471784 | 23.875115 | 26.346494 | 30.119376 | 298.666667 |
In [109]:
import numpy as np
from skfuzzy import control as ctrl
import skfuzzy as fuzz
age = ctrl.Antecedent(np.arange(29, 65, 0.5), "age")
ap_hi = ctrl.Antecedent(np.arange(7, 240, 0.5), "ap_hi")
cholesterol = ctrl.Antecedent([1, 2, 3], "cholesterol")
bmi = ctrl.Antecedent(np.arange(3, 299, 0.05), "bmi")
cardio = ctrl.Consequent([0, 1], "cardio")
age.automf(3, variable_type="quant")
age.view()
ap_hi.automf(3, variable_type="quant")
ap_hi.view()
cholesterol.automf(3, variable_type="quant")
cholesterol.view()
bmi.automf(3, variable_type="quant")
bmi.view()
cardio.automf(2, variable_type="quant", names=["No", "Yes"])
cardio.view()
/Users/user/Projects/python/fuzzy-rules-generator/.venv/lib/python3.12/site-packages/skfuzzy/control/fuzzyvariable.py:125: UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown fig.show()
In [110]:
from src.rules import get_fuzzy_rules
fuzzy_variables = {
"age": age,
"ap_hi": ap_hi,
"cholesterol": cholesterol,
"bmi": bmi,
"consequent": cardio,
}
fuzzy_rules = get_fuzzy_rules(clustered_rules, fuzzy_variables)
fuzzy_cntrl = ctrl.ControlSystem(fuzzy_rules)
sim = ctrl.ControlSystemSimulation(fuzzy_cntrl, lenient=False)
display(len(fuzzy_rules))
fuzzy_rules
41
Out[110]:
[IF (ap_hi[low] AND age[low]) AND cholesterol[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[average] AND age[low]) AND cholesterol[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[low] AND age[low]) AND cholesterol[average]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[low] AND age[low]) AND cholesterol[average]) AND bmi[high] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[low] AND age[average]) AND cholesterol[low]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[low] AND age[average]) AND cholesterol[low]) AND bmi[high] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[average] AND age[average]) AND cholesterol[low]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[average] AND age[average]) AND cholesterol[low]) AND bmi[high] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[average] AND age[high]) AND cholesterol[low]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[low] AND age[high]) AND cholesterol[low]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[low] AND age[high]) AND cholesterol[low]) AND bmi[high] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[average] AND age[high]) AND cholesterol[low]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[average] AND cholesterol[low]) AND age[low]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[average] AND cholesterol[low]) AND age[low]) AND bmi[high] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[average] AND cholesterol[low]) AND age[average]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[average] AND cholesterol[low]) AND age[high]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[average] AND cholesterol[low]) AND age[high]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[average] AND cholesterol[low]) AND age[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[low] AND age[high]) AND cholesterol[high]) AND bmi[low] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[low] AND age[high]) AND cholesterol[high]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[high] AND bmi[low]) AND age[low] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[high] AND bmi[low]) AND age[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[average] AND age[average]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[average] AND age[low]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[average] AND age[low]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[average] AND cholesterol[high]) AND bmi[low]) AND age[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[average] AND cholesterol[high]) AND bmi[low]) AND age[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[average] AND cholesterol[high]) AND bmi[low] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[average] AND cholesterol[high]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[average] AND age[average]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[average] AND age[average]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF ap_hi[average] AND age[average] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF ap_hi[average] AND age[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[low] AND age[low]) AND cholesterol[high]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[average] AND age[low]) AND cholesterol[high]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[low] AND age[low]) AND cholesterol[high]) AND bmi[high] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[low] AND age[average]) AND cholesterol[high]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax, IF ((ap_hi[low] AND age[average]) AND cholesterol[high]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[high] AND bmi[high]) AND age[low] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[high] AND bmi[high]) AND age[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax, IF (ap_hi[high] AND bmi[low]) AND age[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax]
In [111]:
sim.input["age"] = 50.358668
sim.input["ap_hi"] = 110
sim.input["cholesterol"] = 1
sim.input["bmi"] = 21.967120
sim.compute()
sim.print_state()
display(sim.output["cardio"], 1 if sim.output["cardio"] > 0.5 else 0)
cardio.view(sim=sim)
============= Antecedents ============= Antecedent: ap_hi = 110 - low : 0.11397849462365592 - average : 0.886021505376344 - high : 0.0 Antecedent: age = 50.358668 - low : 0.0 - average : 0.7966947605633802 - high : 0.2033052394366198 Antecedent: cholesterol = 1 - low : 1.0 - average : 0.0 - high : 0.0 Antecedent: bmi = 21.96712 - low : 0.8718221321169112 - average : 0.12817786788308883 - high : 0.0 ======= Rules ======= RULE #0: IF (ap_hi[low] AND age[low]) AND cholesterol[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[low] : 0.11397849462365592 - age[low] : 0.0 - cholesterol[low] : 1.0 (ap_hi[low] AND age[low]) AND cholesterol[low] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #1: IF (ap_hi[average] AND age[low]) AND cholesterol[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - age[low] : 0.0 - cholesterol[low] : 1.0 (ap_hi[average] AND age[low]) AND cholesterol[low] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #2: IF ((ap_hi[low] AND age[low]) AND cholesterol[average]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[low] : 0.11397849462365592 - age[low] : 0.0 - cholesterol[average] : 0.0 - bmi[low] : 0.8718221321169112 ((ap_hi[low] AND age[low]) AND cholesterol[average]) AND bmi[low] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #3: IF ((ap_hi[low] AND age[low]) AND cholesterol[average]) AND bmi[high] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[low] : 0.11397849462365592 - age[low] : 0.0 - cholesterol[average] : 0.0 - bmi[high] : 0.0 ((ap_hi[low] AND age[low]) AND cholesterol[average]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #4: IF ((ap_hi[low] AND age[average]) AND cholesterol[low]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[low] : 0.11397849462365592 - age[average] : 0.7966947605633802 - cholesterol[low] : 1.0 - bmi[low] : 0.8718221321169112 ((ap_hi[low] AND age[average]) AND cholesterol[low]) AND bmi[low] = 0.11397849462365592 Activation (THEN-clause): cardio[No] : 0.11397849462365592 RULE #5: IF ((ap_hi[low] AND age[average]) AND cholesterol[low]) AND bmi[high] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[low] : 0.11397849462365592 - age[average] : 0.7966947605633802 - cholesterol[low] : 1.0 - bmi[high] : 0.0 ((ap_hi[low] AND age[average]) AND cholesterol[low]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #6: IF ((ap_hi[average] AND age[average]) AND cholesterol[low]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - age[average] : 0.7966947605633802 - cholesterol[low] : 1.0 - bmi[low] : 0.8718221321169112 ((ap_hi[average] AND age[average]) AND cholesterol[low]) AND bmi[low] = 0.7966947605633802 Activation (THEN-clause): cardio[No] : 0.7966947605633802 RULE #7: IF ((ap_hi[average] AND age[average]) AND cholesterol[low]) AND bmi[high] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - age[average] : 0.7966947605633802 - cholesterol[low] : 1.0 - bmi[high] : 0.0 ((ap_hi[average] AND age[average]) AND cholesterol[low]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #8: IF ((ap_hi[average] AND age[high]) AND cholesterol[low]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - age[high] : 0.2033052394366198 - cholesterol[low] : 1.0 - bmi[low] : 0.8718221321169112 ((ap_hi[average] AND age[high]) AND cholesterol[low]) AND bmi[low] = 0.2033052394366198 Activation (THEN-clause): cardio[No] : 0.2033052394366198 RULE #9: IF ((ap_hi[low] AND age[high]) AND cholesterol[low]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[low] : 0.11397849462365592 - age[high] : 0.2033052394366198 - cholesterol[low] : 1.0 - bmi[low] : 0.8718221321169112 ((ap_hi[low] AND age[high]) AND cholesterol[low]) AND bmi[low] = 0.11397849462365592 Activation (THEN-clause): cardio[No] : 0.11397849462365592 RULE #10: IF ((ap_hi[low] AND age[high]) AND cholesterol[low]) AND bmi[high] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[low] : 0.11397849462365592 - age[high] : 0.2033052394366198 - cholesterol[low] : 1.0 - bmi[high] : 0.0 ((ap_hi[low] AND age[high]) AND cholesterol[low]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #11: IF ((ap_hi[average] AND age[high]) AND cholesterol[low]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - age[high] : 0.2033052394366198 - cholesterol[low] : 1.0 - bmi[high] : 0.0 ((ap_hi[average] AND age[high]) AND cholesterol[low]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #12: IF ((ap_hi[average] AND cholesterol[low]) AND age[low]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - cholesterol[low] : 1.0 - age[low] : 0.0 - bmi[low] : 0.8718221321169112 ((ap_hi[average] AND cholesterol[low]) AND age[low]) AND bmi[low] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #13: IF ((ap_hi[average] AND cholesterol[low]) AND age[low]) AND bmi[high] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - cholesterol[low] : 1.0 - age[low] : 0.0 - bmi[high] : 0.0 ((ap_hi[average] AND cholesterol[low]) AND age[low]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #14: IF ((ap_hi[average] AND cholesterol[low]) AND age[average]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - cholesterol[low] : 1.0 - age[average] : 0.7966947605633802 - bmi[high] : 0.0 ((ap_hi[average] AND cholesterol[low]) AND age[average]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #15: IF ((ap_hi[average] AND cholesterol[low]) AND age[high]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - cholesterol[low] : 1.0 - age[high] : 0.2033052394366198 - bmi[low] : 0.8718221321169112 ((ap_hi[average] AND cholesterol[low]) AND age[high]) AND bmi[low] = 0.2033052394366198 Activation (THEN-clause): cardio[No] : 0.2033052394366198 RULE #16: IF ((ap_hi[average] AND cholesterol[low]) AND age[high]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - cholesterol[low] : 1.0 - age[high] : 0.2033052394366198 - bmi[high] : 0.0 ((ap_hi[average] AND cholesterol[low]) AND age[high]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #17: IF (ap_hi[average] AND cholesterol[low]) AND age[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - cholesterol[low] : 1.0 - age[high] : 0.2033052394366198 (ap_hi[average] AND cholesterol[low]) AND age[high] = 0.2033052394366198 Activation (THEN-clause): cardio[Yes] : 0.2033052394366198 RULE #18: IF ((ap_hi[low] AND age[high]) AND cholesterol[high]) AND bmi[low] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[low] : 0.11397849462365592 - age[high] : 0.2033052394366198 - cholesterol[high] : 0.0 - bmi[low] : 0.8718221321169112 ((ap_hi[low] AND age[high]) AND cholesterol[high]) AND bmi[low] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #19: IF ((ap_hi[low] AND age[high]) AND cholesterol[high]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[low] : 0.11397849462365592 - age[high] : 0.2033052394366198 - cholesterol[high] : 0.0 - bmi[high] : 0.0 ((ap_hi[low] AND age[high]) AND cholesterol[high]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #20: IF (ap_hi[high] AND bmi[low]) AND age[low] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[high] : 0.0 - bmi[low] : 0.8718221321169112 - age[low] : 0.0 (ap_hi[high] AND bmi[low]) AND age[low] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #21: IF (ap_hi[high] AND bmi[low]) AND age[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[high] : 0.0 - bmi[low] : 0.8718221321169112 - age[high] : 0.2033052394366198 (ap_hi[high] AND bmi[low]) AND age[high] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #22: IF (ap_hi[average] AND age[average]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - age[average] : 0.7966947605633802 - bmi[low] : 0.8718221321169112 (ap_hi[average] AND age[average]) AND bmi[low] = 0.7966947605633802 Activation (THEN-clause): cardio[No] : 0.7966947605633802 RULE #23: IF (ap_hi[average] AND age[low]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - age[low] : 0.0 - bmi[low] : 0.8718221321169112 (ap_hi[average] AND age[low]) AND bmi[low] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #24: IF (ap_hi[average] AND age[low]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - age[low] : 0.0 - bmi[high] : 0.0 (ap_hi[average] AND age[low]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #25: IF ((ap_hi[average] AND cholesterol[high]) AND bmi[low]) AND age[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - cholesterol[high] : 0.0 - bmi[low] : 0.8718221321169112 - age[low] : 0.0 ((ap_hi[average] AND cholesterol[high]) AND bmi[low]) AND age[low] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #26: IF ((ap_hi[average] AND cholesterol[high]) AND bmi[low]) AND age[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - cholesterol[high] : 0.0 - bmi[low] : 0.8718221321169112 - age[high] : 0.2033052394366198 ((ap_hi[average] AND cholesterol[high]) AND bmi[low]) AND age[high] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #27: IF (ap_hi[average] AND cholesterol[high]) AND bmi[low] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - cholesterol[high] : 0.0 - bmi[low] : 0.8718221321169112 (ap_hi[average] AND cholesterol[high]) AND bmi[low] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #28: IF (ap_hi[average] AND cholesterol[high]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - cholesterol[high] : 0.0 - bmi[high] : 0.0 (ap_hi[average] AND cholesterol[high]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #29: IF (ap_hi[average] AND age[average]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - age[average] : 0.7966947605633802 - bmi[low] : 0.8718221321169112 (ap_hi[average] AND age[average]) AND bmi[low] = 0.7966947605633802 Activation (THEN-clause): cardio[No] : 0.7966947605633802 RULE #30: IF (ap_hi[average] AND age[average]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - age[average] : 0.7966947605633802 - bmi[high] : 0.0 (ap_hi[average] AND age[average]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #31: IF ap_hi[average] AND age[average] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - age[average] : 0.7966947605633802 ap_hi[average] AND age[average] = 0.7966947605633802 Activation (THEN-clause): cardio[Yes] : 0.7966947605633802 RULE #32: IF ap_hi[average] AND age[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - age[high] : 0.2033052394366198 ap_hi[average] AND age[high] = 0.2033052394366198 Activation (THEN-clause): cardio[Yes] : 0.2033052394366198 RULE #33: IF ((ap_hi[low] AND age[low]) AND cholesterol[high]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[low] : 0.11397849462365592 - age[low] : 0.0 - cholesterol[high] : 0.0 - bmi[low] : 0.8718221321169112 ((ap_hi[low] AND age[low]) AND cholesterol[high]) AND bmi[low] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #34: IF ((ap_hi[average] AND age[low]) AND cholesterol[high]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[average] : 0.886021505376344 - age[low] : 0.0 - cholesterol[high] : 0.0 - bmi[low] : 0.8718221321169112 ((ap_hi[average] AND age[low]) AND cholesterol[high]) AND bmi[low] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #35: IF ((ap_hi[low] AND age[low]) AND cholesterol[high]) AND bmi[high] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[low] : 0.11397849462365592 - age[low] : 0.0 - cholesterol[high] : 0.0 - bmi[high] : 0.0 ((ap_hi[low] AND age[low]) AND cholesterol[high]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #36: IF ((ap_hi[low] AND age[average]) AND cholesterol[high]) AND bmi[low] THEN cardio[No] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[low] : 0.11397849462365592 - age[average] : 0.7966947605633802 - cholesterol[high] : 0.0 - bmi[low] : 0.8718221321169112 ((ap_hi[low] AND age[average]) AND cholesterol[high]) AND bmi[low] = 0.0 Activation (THEN-clause): cardio[No] : 0.0 RULE #37: IF ((ap_hi[low] AND age[average]) AND cholesterol[high]) AND bmi[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[low] : 0.11397849462365592 - age[average] : 0.7966947605633802 - cholesterol[high] : 0.0 - bmi[high] : 0.0 ((ap_hi[low] AND age[average]) AND cholesterol[high]) AND bmi[high] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #38: IF (ap_hi[high] AND bmi[high]) AND age[low] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[high] : 0.0 - bmi[high] : 0.0 - age[low] : 0.0 (ap_hi[high] AND bmi[high]) AND age[low] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #39: IF (ap_hi[high] AND bmi[high]) AND age[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[high] : 0.0 - bmi[high] : 0.0 - age[high] : 0.2033052394366198 (ap_hi[high] AND bmi[high]) AND age[high] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 RULE #40: IF (ap_hi[high] AND bmi[low]) AND age[high] THEN cardio[Yes] AND aggregation function : fmin OR aggregation function : fmax Aggregation (IF-clause): - ap_hi[high] : 0.0 - bmi[low] : 0.8718221321169112 - age[high] : 0.2033052394366198 (ap_hi[high] AND bmi[low]) AND age[high] = 0.0 Activation (THEN-clause): cardio[Yes] : 0.0 ============================== Intermediaries and Conquests ============================== Consequent: cardio = 0.5000000000000001 No: Accumulate using accumulation_max : 0.7966947605633802 Yes: Accumulate using accumulation_max : 0.7966947605633802
np.float64(0.5000000000000001)
1
In [112]:
from sklearn.model_selection import train_test_split
random_state = 9
def fuzzy_pred(row):
sim.input["age"] = row["age"]
sim.input["ap_hi"] = row["ap_hi"]
sim.input["cholesterol"] = row["cholesterol"]
sim.input["bmi"] = row["bmi"]
sim.compute()
return 1 if sim.output["cardio"] > 0.5 else 0
y = df["cardio"]
X = df.drop(["cardio"], axis=1).copy()
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=random_state
)
display(X_train, y_train, X_test, y_test)
age | ap_hi | cholesterol | bmi | |
---|---|---|---|---|
id | ||||
94960 | 62.014018 | 120 | 1 | 26.892323 |
30807 | 57.745592 | 120 | 1 | 28.393726 |
26485 | 59.670354 | 120 | 3 | 23.875115 |
3868 | 49.715256 | 110 | 1 | 20.820940 |
45890 | 59.785347 | 160 | 1 | 23.529412 |
... | ... | ... | ... | ... |
61975 | 62.558865 | 120 | 1 | 28.196921 |
32741 | 57.882488 | 120 | 1 | 29.043709 |
94833 | 51.371701 | 120 | 1 | 29.242109 |
95660 | 45.767167 | 120 | 1 | 24.977043 |
81002 | 55.544300 | 150 | 1 | 27.053803 |
55188 rows × 4 columns
id 94960 0 30807 0 26485 0 3868 1 45890 1 .. 61975 1 32741 0 94833 0 95660 0 81002 1 Name: cardio, Length: 55188, dtype: int64
age | ap_hi | cholesterol | bmi | |
---|---|---|---|---|
id | ||||
42270 | 60.078305 | 140 | 1 | 45.918367 |
10780 | 55.360859 | 120 | 2 | 24.998904 |
42436 | 48.198445 | 100 | 3 | 21.926126 |
88647 | 41.517906 | 130 | 2 | 27.764650 |
62336 | 51.692038 | 110 | 1 | 22.230987 |
... | ... | ... | ... | ... |
30330 | 47.697404 | 100 | 1 | 22.724403 |
62907 | 58.597087 | 120 | 1 | 23.828125 |
98612 | 51.404556 | 110 | 1 | 22.589551 |
5767 | 62.033184 | 120 | 1 | 23.875115 |
14769 | 41.506954 | 120 | 2 | 22.948116 |
13797 rows × 4 columns
id 42270 1 10780 0 42436 1 88647 1 62336 0 .. 30330 1 62907 0 98612 0 5767 0 14769 1 Name: cardio, Length: 13797, dtype: int64
In [113]:
result_test = X_test.copy()
result_test["Real"] = y_test
result_test = result_test.head(1000)
result_test["Inferred"] = result_test.apply(fuzzy_pred, axis=1)
result_test
Out[113]:
age | ap_hi | cholesterol | bmi | Real | Inferred | |
---|---|---|---|---|---|---|
id | ||||||
42270 | 60.078305 | 140 | 1 | 45.918367 | 1 | 1 |
10780 | 55.360859 | 120 | 2 | 24.998904 | 0 | 0 |
42436 | 48.198445 | 100 | 3 | 21.926126 | 1 | 0 |
88647 | 41.517906 | 130 | 2 | 27.764650 | 1 | 0 |
62336 | 51.692038 | 110 | 1 | 22.230987 | 0 | 0 |
... | ... | ... | ... | ... | ... | ... |
23904 | 53.942613 | 120 | 1 | 35.491690 | 1 | 0 |
63516 | 40.305005 | 120 | 1 | 21.829952 | 0 | 0 |
84904 | 42.561056 | 140 | 1 | 32.882414 | 1 | 0 |
20959 | 45.545395 | 160 | 1 | 43.827160 | 1 | 0 |
77652 | 54.115102 | 140 | 1 | 37.105751 | 0 | 0 |
1000 rows × 6 columns
In [114]:
from sklearn import metrics
display(
"Precision_test",
metrics.precision_score(result_test["Real"], result_test["Inferred"]),
)
display(
"Recall_test", metrics.recall_score(result_test["Real"], result_test["Inferred"])
)
display(
"Accuracy_test",
metrics.accuracy_score(result_test["Real"], result_test["Inferred"]),
)
display(
"F1_test",
metrics.f1_score(result_test["Real"], result_test["Inferred"]),
)
display(
"Confusion_matrix",
metrics.confusion_matrix(result_test["Real"], result_test["Inferred"]),
)
'Precision_test'
np.float64(0.5469483568075117)
'Recall_test'
np.float64(0.4707070707070707)
'Accuracy_test'
0.545
'F1_test'
np.float64(0.505971769815418)
'Confusion_matrix'
array([[312, 193], [262, 233]])