387 KiB
387 KiB
In [2]:
import pickle
import pandas as pd
from sklearn import tree
model = pickle.load(open("data/temp_density_tree-gs.model.sav", "rb"))
features = (
pd.read_csv("data/density_train.csv", sep=";", decimal=",")
.drop(["T"], axis=1)
.columns.values.tolist()
)
rules = tree.export_text(model, feature_names=features)
print(rules)
In [3]:
from src.rules import get_rules
rules = get_rules(model, features)
display(len(rules))
rules
Out[3]:
In [4]:
from src.rules import normalise_rules
rules = normalise_rules(rules)
display(len(rules))
rules
Out[4]:
In [5]:
from src.rules import delete_same_rules
rules = delete_same_rules(rules)
display(len(rules))
rules
Out[5]:
In [6]:
train = pd.read_csv("data/density_train.csv", sep=";", decimal=",")
test = pd.read_csv("data/density_test.csv", sep=";", decimal=",")
train["Density"] = train["Density"]
test["Density"] = test["Density"]
display(train.head(3))
display(test.head(3))
train.describe().transpose()
Out[6]:
In [7]:
from src.rules import simplify_rules
rules = simplify_rules(train, rules)
display(len(rules))
rules
Out[7]:
In [19]:
import numpy as np
from skfuzzy import control as ctrl
import skfuzzy as fuzz
al = ctrl.Antecedent(np.arange(0, 0.3, 0.1), "al")
ti = ctrl.Antecedent(np.arange(0, 0.3, 0.1), "ti")
density = ctrl.Antecedent(np.arange(1.03, 1.22, 0.001), "density")
# temp = ctrl.Consequent(train["T"].sort_values().unique(), "temp")
temp = ctrl.Consequent(np.arange(20, 70, 1), "temp")
al.automf(3, variable_type="quant")
al.view()
ti.automf(3, variable_type="quant")
ti.view()
density.automf(3, variable_type="quant")
density.view()
temp.automf(7, variable_type="quant")
temp.view()
In [20]:
from src.rules import get_fuzzy_rules
fuzzy_variables = {"Al2O3": al, "TiO2": ti, "Density": density, "consequent": temp}
fuzzy_rules = get_fuzzy_rules(rules, fuzzy_variables)
fuzzy_rules.remove(fuzzy_rules[5])
fuzzy_cntrl = ctrl.ControlSystem(fuzzy_rules)
sim = ctrl.ControlSystemSimulation(fuzzy_cntrl, lenient=False)
display(len(fuzzy_rules))
fuzzy_rules
Out[20]:
In [21]:
sim.input["al"] = 0.0
sim.input["ti"] = 0.0
sim.input["density"] = 1.056
sim.compute()
sim.print_state()
display(sim.output["temp"])
temp.view(sim=sim)
In [22]:
from sklearn import metrics
import math
def fuzzy_pred(row):
sim.input["al"] = row["Al2O3"]
sim.input["ti"] = row["TiO2"]
sim.input["density"] = row["Density"]
sim.compute()
return sim.output["temp"]
def rmse(row):
return math.sqrt(metrics.mean_squared_error([row["Real"]], [row["Inferred"]]))
result_train = train.copy()
result_train["Real"] = result_train["T"]
result_train["Inferred"] = result_train.apply(fuzzy_pred, axis=1)
result_train["RMSE"] = result_train.apply(rmse, axis=1)
result_train = result_train.round({"RMSE": 3})
result_train.head(15)
Out[22]:
In [23]:
result_test = test.copy()
result_test["Real"] = result_test["T"]
result_test["Inferred"] = result_test.apply(fuzzy_pred, axis=1)
result_test["RMSE"] = result_test.apply(rmse, axis=1)
result_test = result_test.round({"RMSE": 3})
result_test
Out[23]:
In [24]:
import matplotlib.pyplot as plt
result_test.hist(bins=30, figsize=(10, 10))
plt.show()
In [25]:
rmetrics = {}
rmetrics["RMSE_train"] = math.sqrt(
metrics.mean_squared_error(result_train["Real"], result_train["Inferred"])
)
rmetrics["RMSE_test"] = math.sqrt(
metrics.mean_squared_error(result_test["Real"], result_test["Inferred"])
)
rmetrics["RMAE_test"] = math.sqrt(
metrics.mean_absolute_error(result_test["Real"], result_test["Inferred"])
)
rmetrics["R2_test"] = metrics.r2_score(result_test["Real"], result_test["Inferred"])
rmetrics
Out[25]: