238 KiB
238 KiB
In [58]:
import pandas as pd
density_train = pd.read_csv("data/density_train.csv", sep=";", decimal=",")
density_test = pd.read_csv("data/density_test.csv", sep=";", decimal=",")
display(density_train.head(3))
display(density_test.head(3))
In [59]:
import numpy as np
from skfuzzy import control as ctrl
import skfuzzy as fuzz
temp = ctrl.Antecedent(density_train["T"].sort_values().unique(), "temp")
al = ctrl.Antecedent(np.arange(0, 0.3, 0.005), "al")
ti = ctrl.Antecedent(np.arange(0, 0.3, 0.005), "ti")
density = ctrl.Consequent(np.arange(1.03, 1.22, 0.00001), "density")
temp.automf(3, variable_type="quant")
temp.view()
al.automf(3, variable_type="quant")
al.view()
ti.automf(3, variable_type="quant")
ti.view()
density.automf(5, variable_type="quant")
density.view()
In [60]:
rule11 = ctrl.Rule(
temp["low"] & al["low"] & ti["low"],
density["low"],
)
rule12 = ctrl.Rule(
temp["average"] & al["low"] & ti["low"],
density["lower"],
)
rule13 = ctrl.Rule(
temp["high"] & al["low"] & ti["low"],
density["lower"],
)
rule21 = ctrl.Rule(
temp["low"] & al["average"] & ti["low"],
density["low"],
)
rule22 = ctrl.Rule(
temp["average"] & al["average"] & ti["low"],
density["low"],
)
rule23 = ctrl.Rule(
temp["high"] & al["average"] & ti["low"],
density["lower"],
)
rule31 = ctrl.Rule(
temp["low"] & al["high"] & ti["low"],
density["high"],
)
rule32 = ctrl.Rule(
temp["low"] & al["high"] & ti["low"],
density["high"],
)
rule33 = ctrl.Rule(
temp["high"] & al["high"] & ti["low"],
density["average"],
)
rule41 = ctrl.Rule(
temp["low"] & al["low"] & ti["average"],
density["low"],
)
rule42 = ctrl.Rule(
temp["average"] & al["low"] & ti["average"],
density["low"],
)
rule43 = ctrl.Rule(
temp["high"] & al["low"] & ti["average"],
density["lower"],
)
rule51 = ctrl.Rule(
temp["low"] & al["low"] & ti["high"],
density["higher"],
)
rule52 = ctrl.Rule(
temp["average"] & al["low"] & ti["high"],
density["high"],
)
rule53 = ctrl.Rule(
temp["high"] & al["low"] & ti["high"],
density["high"],
)
In [61]:
fuzzy_rules = [
rule11,
rule12,
rule13,
rule21,
rule22,
rule23,
rule31,
rule32,
rule33,
rule41,
rule42,
rule43,
rule51,
rule52,
rule53,
]
density_cntrl = ctrl.ControlSystem(fuzzy_rules)
sim = ctrl.ControlSystemSimulation(density_cntrl)
fuzzy_rules
Out[61]:
In [62]:
sim.input["temp"] = 20
sim.input["al"] = 0.3
sim.input["ti"] = 0.0
sim.compute()
sim.print_state()
display(sim.output["density"])
In [63]:
def fuzzy_pred(row):
sim.input["temp"] = row["T"]
sim.input["al"] = row["Al2O3"]
sim.input["ti"] = row["TiO2"]
sim.compute()
return sim.output["density"]
In [65]:
result_train = density_train.copy()
result_train["DensityPred"] = result_train.apply(fuzzy_pred, axis=1)
result_train.head(15)
Out[65]:
In [66]:
result_test = density_test.copy()
result_test["DensityPred"] = result_test.apply(fuzzy_pred, axis=1)
result_test
Out[66]:
In [67]:
import math
from sklearn import metrics
rmetrics = {}
rmetrics["RMSE_train"] = math.sqrt(
metrics.mean_squared_error(result_train["Density"], result_train["DensityPred"])
)
rmetrics["RMSE_test"] = math.sqrt(
metrics.mean_squared_error(result_test["Density"], result_test["DensityPred"])
)
rmetrics["RMAE_test"] = math.sqrt(
metrics.mean_absolute_error(result_test["Density"], result_test["DensityPred"])
)
rmetrics["R2_test"] = metrics.r2_score(
result_test["Density"], result_test["DensityPred"]
)
rmetrics
Out[67]: