fuzzy-rules-generator/temp_density_tree.ipynb

386 KiB

In [70]:
import pickle
import pandas as pd
from sklearn import tree

model = pickle.load(open("data/temp_density_tree.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)
|--- Density <= 1.04
|   |--- Density <= 1.03
|   |   |--- value: [70.00]
|   |--- Density >  1.03
|   |   |--- Density <= 1.04
|   |   |   |--- value: [65.00]
|   |   |--- Density >  1.04
|   |   |   |--- value: [60.00]
|--- Density >  1.04
|   |--- Density <= 1.07
|   |   |--- TiO2 <= 0.03
|   |   |   |--- Al2O3 <= 0.03
|   |   |   |   |--- Density <= 1.05
|   |   |   |   |   |--- Density <= 1.05
|   |   |   |   |   |   |--- value: [50.00]
|   |   |   |   |   |--- Density >  1.05
|   |   |   |   |   |   |--- value: [42.50]
|   |   |   |   |--- Density >  1.05
|   |   |   |   |   |--- Density <= 1.06
|   |   |   |   |   |   |--- value: [35.00]
|   |   |   |   |   |--- Density >  1.06
|   |   |   |   |   |   |--- value: [22.50]
|   |   |   |--- Al2O3 >  0.03
|   |   |   |   |--- Density <= 1.06
|   |   |   |   |   |--- Density <= 1.05
|   |   |   |   |   |   |--- value: [70.00]
|   |   |   |   |   |--- Density >  1.05
|   |   |   |   |   |   |--- value: [65.00]
|   |   |   |   |--- Density >  1.06
|   |   |   |   |   |--- Density <= 1.07
|   |   |   |   |   |   |--- value: [55.00]
|   |   |   |   |   |--- Density >  1.07
|   |   |   |   |   |   |--- value: [50.00]
|   |   |--- TiO2 >  0.03
|   |   |   |--- Density <= 1.06
|   |   |   |   |--- value: [70.00]
|   |   |   |--- Density >  1.06
|   |   |   |   |--- Density <= 1.06
|   |   |   |   |   |--- value: [65.00]
|   |   |   |   |--- Density >  1.06
|   |   |   |   |   |--- value: [60.00]
|   |--- Density >  1.07
|   |   |--- Density <= 1.12
|   |   |   |--- Density <= 1.08
|   |   |   |   |--- Density <= 1.07
|   |   |   |   |   |--- value: [45.00]
|   |   |   |   |--- Density >  1.07
|   |   |   |   |   |--- Density <= 1.08
|   |   |   |   |   |   |--- value: [40.00]
|   |   |   |   |   |--- Density >  1.08
|   |   |   |   |   |   |--- value: [35.00]
|   |   |   |--- Density >  1.08
|   |   |   |   |--- Density <= 1.09
|   |   |   |   |   |--- value: [30.00]
|   |   |   |   |--- Density >  1.09
|   |   |   |   |   |--- Al2O3 <= 0.03
|   |   |   |   |   |   |--- value: [22.50]
|   |   |   |   |   |--- Al2O3 >  0.03
|   |   |   |   |   |   |--- value: [20.00]
|   |   |--- Density >  1.12
|   |   |   |--- Density <= 1.18
|   |   |   |   |--- Density <= 1.15
|   |   |   |   |   |--- value: [70.00]
|   |   |   |   |--- Density >  1.15
|   |   |   |   |   |--- Al2O3 <= 0.15
|   |   |   |   |   |   |--- value: [65.00]
|   |   |   |   |   |--- Al2O3 >  0.15
|   |   |   |   |   |   |--- value: [50.00]
|   |   |   |--- Density >  1.18
|   |   |   |   |--- Al2O3 <= 0.15
|   |   |   |   |   |--- Density <= 1.20
|   |   |   |   |   |   |--- value: [50.00]
|   |   |   |   |   |--- Density >  1.20
|   |   |   |   |   |   |--- value: [30.00]
|   |   |   |   |--- Al2O3 >  0.15
|   |   |   |   |   |--- Density <= 1.18
|   |   |   |   |   |   |--- value: [30.00]
|   |   |   |   |   |--- Density >  1.18
|   |   |   |   |   |   |--- value: [22.50]

In [71]:
from src.rules import get_rules


rules = get_rules(model, features)
display(len(rules))
rules
27
Out[71]:
[if (Density > 1.042) and (Density > 1.069) and (Density <= 1.118) and (Density > 1.083) and (Density > 1.086) and (Al2O3 > 0.025) -> 20.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 <= 0.025) and (Density > 1.053) and (Density > 1.057) -> 22.5,
 if (Density <= 1.042) and (Density > 1.033) and (Density > 1.037) -> 60.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 <= 0.025) and (Density <= 1.053) and (Density <= 1.046) -> 50.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 <= 0.025) and (Density <= 1.053) and (Density > 1.046) -> 42.5,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 <= 0.025) and (Density > 1.053) and (Density <= 1.057) -> 35.0,
 if (Density <= 1.042) and (Density > 1.033) and (Density <= 1.037) -> 65.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 > 0.025) and (Density <= 1.061) and (Density <= 1.055) -> 70.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 > 0.025) and (Density <= 1.061) and (Density > 1.055) -> 65.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 > 0.025) and (Density > 1.061) and (Density <= 1.066) -> 55.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 > 0.025) and (Density > 1.061) and (Density > 1.066) -> 50.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 > 0.025) and (Density <= 1.058) -> 70.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 > 0.025) and (Density > 1.058) and (Density <= 1.062) -> 65.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 > 0.025) and (Density > 1.058) and (Density > 1.062) -> 60.0,
 if (Density > 1.042) and (Density > 1.069) and (Density <= 1.118) and (Density <= 1.083) and (Density <= 1.074) -> 45.0,
 if (Density > 1.042) and (Density > 1.069) and (Density <= 1.118) and (Density <= 1.083) and (Density > 1.074) and (Density <= 1.079) -> 40.0,
 if (Density > 1.042) and (Density > 1.069) and (Density <= 1.118) and (Density <= 1.083) and (Density > 1.074) and (Density > 1.079) -> 35.0,
 if (Density > 1.042) and (Density > 1.069) and (Density <= 1.118) and (Density > 1.083) and (Density <= 1.086) -> 30.0,
 if (Density > 1.042) and (Density > 1.069) and (Density <= 1.118) and (Density > 1.083) and (Density > 1.086) and (Al2O3 <= 0.025) -> 22.5,
 if (Density <= 1.042) and (Density <= 1.033) -> 70.0,
 if (Density > 1.042) and (Density > 1.069) and (Density > 1.118) and (Density <= 1.179) and (Density <= 1.148) -> 70.0,
 if (Density > 1.042) and (Density > 1.069) and (Density > 1.118) and (Density <= 1.179) and (Density > 1.148) and (Al2O3 <= 0.15) -> 65.0,
 if (Density > 1.042) and (Density > 1.069) and (Density > 1.118) and (Density <= 1.179) and (Density > 1.148) and (Al2O3 > 0.15) -> 50.0,
 if (Density > 1.042) and (Density > 1.069) and (Density > 1.118) and (Density > 1.179) and (Al2O3 <= 0.15) and (Density <= 1.203) -> 50.0,
 if (Density > 1.042) and (Density > 1.069) and (Density > 1.118) and (Density > 1.179) and (Al2O3 <= 0.15) and (Density > 1.203) -> 30.0,
 if (Density > 1.042) and (Density > 1.069) and (Density > 1.118) and (Density > 1.179) and (Al2O3 > 0.15) and (Density <= 1.182) -> 30.0,
 if (Density > 1.042) and (Density > 1.069) and (Density > 1.118) and (Density > 1.179) and (Al2O3 > 0.15) and (Density > 1.182) -> 22.5]
In [72]:
from src.rules import normalise_rules


rules = normalise_rules(rules)
display(len(rules))
rules
27
Out[72]:
[if (Density > 1.042) and (Density <= 1.118) and (Al2O3 > 0.025) -> 20.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 <= 0.025) -> 22.5,
 if (Density <= 1.042) and (Density > 1.033) -> 60.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 <= 0.025) -> 50.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 <= 0.025) -> 42.5,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 <= 0.025) -> 35.0,
 if (Density <= 1.042) and (Density > 1.033) -> 65.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 > 0.025) -> 70.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 > 0.025) -> 65.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 > 0.025) -> 55.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 > 0.025) -> 50.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 > 0.025) -> 70.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 > 0.025) -> 65.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 > 0.025) -> 60.0,
 if (Density > 1.042) and (Density <= 1.118) -> 45.0,
 if (Density > 1.042) and (Density <= 1.118) -> 40.0,
 if (Density > 1.042) and (Density <= 1.118) -> 35.0,
 if (Density > 1.042) and (Density <= 1.118) -> 30.0,
 if (Density > 1.042) and (Density <= 1.118) and (Al2O3 <= 0.025) -> 22.5,
 if (Density <= 1.042) -> 70.0,
 if (Density > 1.042) and (Density <= 1.179) -> 70.0,
 if (Density > 1.042) and (Density <= 1.179) and (Al2O3 <= 0.15) -> 65.0,
 if (Density > 1.042) and (Density <= 1.179) and (Al2O3 > 0.15) -> 50.0,
 if (Density > 1.042) and (Density <= 1.203) and (Al2O3 <= 0.15) -> 50.0,
 if (Density > 1.042) and (Al2O3 <= 0.15) -> 30.0,
 if (Density > 1.042) and (Density <= 1.182) and (Al2O3 > 0.15) -> 30.0,
 if (Density > 1.042) and (Al2O3 > 0.15) -> 22.5]
In [73]:
from src.rules import delete_same_rules


rules = delete_same_rules(rules)
display(len(rules))
rules
15
Out[73]:
[if (Density > 1.042) and (Density <= 1.118) and (Al2O3 > 0.025) -> 20.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 <= 0.025) -> 37.5,
 if (Density <= 1.042) and (Density > 1.033) -> 62.5,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 <= 0.025) and (Al2O3 > 0.025) -> 60.0,
 if (Density > 1.042) and (Density <= 1.069) and (TiO2 > 0.025) -> 65.0,
 if (Density > 1.042) and (Density <= 1.118) -> 37.5,
 if (Density > 1.042) and (Density <= 1.118) and (Al2O3 <= 0.025) -> 22.5,
 if (Density <= 1.042) -> 70.0,
 if (Density > 1.042) and (Density <= 1.179) -> 70.0,
 if (Density > 1.042) and (Density <= 1.179) and (Al2O3 <= 0.15) -> 65.0,
 if (Density > 1.042) and (Density <= 1.179) and (Al2O3 > 0.15) -> 50.0,
 if (Density > 1.042) and (Density <= 1.203) and (Al2O3 <= 0.15) -> 50.0,
 if (Density > 1.042) and (Al2O3 <= 0.15) -> 30.0,
 if (Density > 1.042) and (Density <= 1.182) and (Al2O3 > 0.15) -> 30.0,
 if (Density > 1.042) and (Al2O3 > 0.15) -> 22.5]
In [74]:
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()
T Al2O3 TiO2 Density
0 20 0.0 0.0 1.06250
1 25 0.0 0.0 1.05979
2 35 0.0 0.0 1.05404
T Al2O3 TiO2 Density
0 30 0.00 0.0 1.05696
1 55 0.00 0.0 1.04158
2 25 0.05 0.0 1.08438
Out[74]:
count mean std min 25% 50% 75% max
T 38.0 45.526316 16.513282 20.00000 31.25000 45.000000 60.000000 70.0000
Al2O3 38.0 0.078947 0.126080 0.00000 0.00000 0.000000 0.050000 0.3000
TiO2 38.0 0.057895 0.108133 0.00000 0.00000 0.000000 0.050000 0.3000
Density 38.0 1.108180 0.060772 1.03182 1.05763 1.082755 1.169058 1.2186
In [75]:
from src.rules import simplify_rules

rules = simplify_rules(train, rules)
display(len(rules))
rules
15
Out[75]:
[if (Density = 1.08) and (Al2O3 = 0.3) -> 20.0,
 if (Density = 1.055) and (TiO2 = 0.0) and (Al2O3 = 0.0) -> 37.5,
 if (Density = 1.037) -> 62.5,
 if (Density = 1.055) and (TiO2 = 0.0) and (Al2O3 = 0.3) -> 60.0,
 if (Density = 1.055) and (TiO2 = 0.3) -> 65.0,
 if (Density = 1.08) -> 37.5,
 if (Density = 1.08) and (Al2O3 = 0.0) -> 22.5,
 if (Density = 1.032) -> 70.0,
 if (Density = 1.11) -> 70.0,
 if (Density = 1.11) and (Al2O3 = 0.0) -> 65.0,
 if (Density = 1.11) and (Al2O3 = 0.3) -> 50.0,
 if (Density = 1.122) and (Al2O3 = 0.0) -> 50.0,
 if (Density = 1.219) and (Al2O3 = 0.0) -> 30.0,
 if (Density = 1.112) and (Al2O3 = 0.3) -> 30.0,
 if (Density = 1.219) and (Al2O3 = 0.3) -> 22.5]
In [124]:
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, 0.1), "temp")

al.automf(3, variable_type="quant")
al.view()
ti.automf(3, variable_type="quant")
ti.view()
density.automf(5, variable_type="quant")
density.view()
temp.automf(5, variable_type="quant")
temp.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 [125]:
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
11
Out[125]:
[IF density[low] AND al[high] THEN temp[lower]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF (density[low] AND ti[low]) AND al[low] THEN temp[low]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF density[lower] THEN temp[high]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF (density[low] AND ti[low]) AND al[high] THEN temp[high]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF density[low] AND ti[high] THEN temp[higher]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF density[low] AND al[low] THEN temp[lower]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF density[average] THEN temp[higher]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF density[average] AND al[low] THEN temp[higher]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF density[average] AND al[high] THEN temp[average]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF density[higher] AND al[low] THEN temp[low]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF density[higher] AND al[high] THEN temp[lower]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax]
In [126]:
sim.input["al"] = 0.0
sim.input["ti"] = 0.0
sim.input["density"] = 1.0569013636039
sim.compute()
sim.print_state()
display(sim.output["temp"])
temp.view(sim=sim)
=============
 Antecedents 
=============
Antecedent: density                 = 1.0569013636039
  - lower                           : 0.43065897134597164
  - low                             : 0.5693410286540264
  - average                         : 0.0
  - high                            : 0.0
  - higher                          : 0.0
Antecedent: al                      = 0.0
  - low                             : 1.0
  - average                         : 0.0
  - high                            : 0.0
Antecedent: ti                      = 0.0
  - low                             : 1.0
  - average                         : 0.0
  - high                            : 0.0

=======
 Rules 
=======
RULE #0:
  IF density[low] AND al[high] THEN temp[lower]
	AND aggregation function : fmin
	OR aggregation function  : fmax

  Aggregation (IF-clause):
  - density[low]                                           : 0.5693410286540264
  - al[high]                                               : 0.0
                                 density[low] AND al[high] = 0.0
  Activation (THEN-clause):
                                               temp[lower] : 0.0

RULE #1:
  IF (density[low] AND ti[low]) AND al[low] THEN temp[low]
	AND aggregation function : fmin
	OR aggregation function  : fmax

  Aggregation (IF-clause):
  - density[low]                                           : 0.5693410286540264
  - ti[low]                                                : 1.0
  - al[low]                                                : 1.0
                    (density[low] AND ti[low]) AND al[low] = 0.5693410286540264
  Activation (THEN-clause):
                                                 temp[low] : 0.5693410286540264

RULE #2:
  IF density[lower] THEN temp[high]
	AND aggregation function : fmin
	OR aggregation function  : fmax

  Aggregation (IF-clause):
  - density[lower]                                         : 0.43065897134597164
                                            density[lower] = 0.43065897134597164
  Activation (THEN-clause):
                                                temp[high] : 0.43065897134597164

RULE #3:
  IF (density[low] AND ti[low]) AND al[high] THEN temp[high]
	AND aggregation function : fmin
	OR aggregation function  : fmax

  Aggregation (IF-clause):
  - density[low]                                           : 0.5693410286540264
  - ti[low]                                                : 1.0
  - al[high]                                               : 0.0
                   (density[low] AND ti[low]) AND al[high] = 0.0
  Activation (THEN-clause):
                                                temp[high] : 0.0

RULE #4:
  IF density[low] AND ti[high] THEN temp[higher]
	AND aggregation function : fmin
	OR aggregation function  : fmax

  Aggregation (IF-clause):
  - density[low]                                           : 0.5693410286540264
  - ti[high]                                               : 0.0
                                 density[low] AND ti[high] = 0.0
  Activation (THEN-clause):
                                              temp[higher] : 0.0

RULE #5:
  IF density[low] AND al[low] THEN temp[lower]
	AND aggregation function : fmin
	OR aggregation function  : fmax

  Aggregation (IF-clause):
  - density[low]                                           : 0.5693410286540264
  - al[low]                                                : 1.0
                                  density[low] AND al[low] = 0.5693410286540264
  Activation (THEN-clause):
                                               temp[lower] : 0.5693410286540264

RULE #6:
  IF density[average] THEN temp[higher]
	AND aggregation function : fmin
	OR aggregation function  : fmax

  Aggregation (IF-clause):
  - density[average]                                       : 0.0
                                          density[average] = 0.0
  Activation (THEN-clause):
                                              temp[higher] : 0.0

RULE #7:
  IF density[average] AND al[low] THEN temp[higher]
	AND aggregation function : fmin
	OR aggregation function  : fmax

  Aggregation (IF-clause):
  - density[average]                                       : 0.0
  - al[low]                                                : 1.0
                              density[average] AND al[low] = 0.0
  Activation (THEN-clause):
                                              temp[higher] : 0.0

RULE #8:
  IF density[average] AND al[high] THEN temp[average]
	AND aggregation function : fmin
	OR aggregation function  : fmax

  Aggregation (IF-clause):
  - density[average]                                       : 0.0
  - al[high]                                               : 0.0
                             density[average] AND al[high] = 0.0
  Activation (THEN-clause):
                                             temp[average] : 0.0

RULE #9:
  IF density[higher] AND al[low] THEN temp[low]
	AND aggregation function : fmin
	OR aggregation function  : fmax

  Aggregation (IF-clause):
  - density[higher]                                        : 0.0
  - al[low]                                                : 1.0
                               density[higher] AND al[low] = 0.0
  Activation (THEN-clause):
                                                 temp[low] : 0.0

RULE #10:
  IF density[higher] AND al[high] THEN temp[lower]
	AND aggregation function : fmin
	OR aggregation function  : fmax

  Aggregation (IF-clause):
  - density[higher]                                        : 0.0
  - al[high]                                               : 0.0
                              density[higher] AND al[high] = 0.0
  Activation (THEN-clause):
                                               temp[lower] : 0.0


==============================
 Intermediaries and Conquests 
==============================
Consequent: temp                     = 41.73310616908626
  lower:
    Accumulate using accumulation_max : 0.5693410286540264
  low:
    Accumulate using accumulation_max : 0.5693410286540264
  average:
    Accumulate using accumulation_max : 0.0
  high:
    Accumulate using accumulation_max : 0.43065897134597164
  higher:
    Accumulate using accumulation_max : 0.0

np.float64(41.73310616908626)
In [127]:
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[127]:
T Al2O3 TiO2 Density Real Inferred RMSE
0 20 0.00 0.0 1.06250 20 39.276564 19.277
1 25 0.00 0.0 1.05979 25 40.474182 15.474
2 35 0.00 0.0 1.05404 35 43.002194 8.002
3 40 0.00 0.0 1.05103 40 44.373956 4.374
4 45 0.00 0.0 1.04794 45 45.803674 0.804
5 50 0.00 0.0 1.04477 50 47.330811 2.669
6 60 0.00 0.0 1.03826 60 50.888953 9.111
7 65 0.00 0.0 1.03484 65 53.183324 11.817
8 70 0.00 0.0 1.03182 70 55.645272 14.355
9 20 0.05 0.0 1.08755 20 37.043626 17.044
10 45 0.05 0.0 1.07105 45 36.776662 8.223
11 50 0.05 0.0 1.06760 50 38.797623 11.202
12 55 0.05 0.0 1.06409 55 40.347656 14.652
13 65 0.05 0.0 1.05691 65 42.508159 22.492
14 70 0.05 0.0 1.05291 70 43.516405 26.484
In [128]:
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[128]:
T Al2O3 TiO2 Density Real Inferred RMSE
0 30 0.00 0.00 1.05696 30 41.707469 11.707
1 55 0.00 0.00 1.04158 55 48.979472 6.021
2 25 0.05 0.00 1.08438 25 35.546123 10.546
3 30 0.05 0.00 1.08112 30 33.702339 3.702
4 35 0.05 0.00 1.07781 35 31.483248 3.517
5 40 0.05 0.00 1.07446 40 34.051564 5.948
6 60 0.05 0.00 1.06053 60 41.554965 18.445
7 35 0.30 0.00 1.17459 35 26.052643 8.947
8 65 0.30 0.00 1.14812 65 51.649578 13.350
9 45 0.00 0.05 1.07424 45 32.924090 12.076
10 50 0.00 0.05 1.07075 50 35.608122 14.392
11 55 0.00 0.05 1.06721 55 37.735219 17.265
12 20 0.00 0.30 1.22417 20 32.475100 12.475
13 30 0.00 0.30 1.21310 30 32.475102 2.475
14 40 0.00 0.30 1.20265 40 32.475114 7.525
15 60 0.00 0.30 1.18265 60 32.475245 27.525
16 70 0.00 0.30 1.17261 70 32.477777 37.522
In [132]:
import matplotlib.pyplot as plt

result_test.hist(bins=30, figsize=(10, 10))
plt.show()
In [130]:
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[130]:
{'RMSE_train': 14.558276689668899,
 'RMSE_test': 15.299814000482547,
 'RMAE_test': 3.543337541765307,
 'R2_test': -0.09732952379343951}