356 KiB
356 KiB
In [2]:
import pickle
import pandas as pd
from sklearn import tree
model = pickle.load(open("data/temp_viscosity_tree.model.sav", "rb"))
features = (
pd.read_csv("data/viscosity_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/viscosity_train.csv", sep=";", decimal=",")
test = pd.read_csv("data/viscosity_test.csv", sep=";", decimal=",")
display(train.head(3))
display(test.head(3))
In [7]:
from src.rules import simplify_rules
rules = simplify_rules(train, rules)
display(len(rules))
rules
Out[7]:
In [8]:
import numpy as np
from skfuzzy import control as ctrl
import skfuzzy as fuzz
al = ctrl.Antecedent(np.arange(0, 0.3, 0.005), "al")
ti = ctrl.Antecedent(np.arange(0, 0.3, 0.005), "ti")
viscosity = ctrl.Antecedent(np.arange(1.18, 3.71, 0.00001), "viscosity")
temp = ctrl.Consequent(train["T"].sort_values().unique(), "temp")
al.automf(3, variable_type="quant")
al.view()
ti.automf(3, variable_type="quant")
ti.view()
viscosity.automf(3, variable_type="quant")
viscosity.view()
temp.automf(5, variable_type="quant")
temp.view()
In [9]:
from src.rules import get_fuzzy_rules
fuzzy_variables = {"Al2O3": al, "TiO2": ti, "Viscosity": viscosity, "consequent": temp}
fuzzy_rules = get_fuzzy_rules(rules, fuzzy_variables)
fuzzy_cntrl = ctrl.ControlSystem(fuzzy_rules)
sim = ctrl.ControlSystemSimulation(fuzzy_cntrl, lenient=False)
display(len(fuzzy_rules))
fuzzy_rules
Out[9]:
In [10]:
sim.input["al"] = 0.0
sim.input["ti"] = 0.0
sim.input["viscosity"] = 2.716
sim.compute()
sim.print_state()
display(sim.output["temp"])
temp.view(sim=sim)
In [11]:
from sklearn import metrics
import math
def fuzzy_pred(row):
sim.input["al"] = row["Al2O3"]
sim.input["ti"] = row["TiO2"]
sim.input["viscosity"] = row["Viscosity"]
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_test = test.round({"RMSE": 3})
result_train.head(15)
Out[11]:
In [12]:
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["RMSE"] = result_test["RMSE"].apply(lambda x: "{:,.4f}".format(x))
result_test = result_test.round({"RMSE": 3})
result_test
Out[12]:
In [13]:
import matplotlib.pyplot as plt
result_test.hist(bins=30, figsize=(10, 10))
plt.show()
In [14]:
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[14]: