fuzzy-rules-generator/temp_density_tree.ipynb

302 KiB

In [44]:
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 [45]:
from src.rules import get_rules


rules = get_rules(model, features)
display(len(rules))
rules
27
Out[45]:
[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 [46]:
from src.rules import normalise_rules


rules = normalise_rules(rules)
display(len(rules))
rules
27
Out[46]:
[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 [47]:
from src.rules import delete_same_rules


rules = delete_same_rules(rules)
display(len(rules))
rules
15
Out[47]:
[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 [48]:
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[48]:
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 [49]:
from src.rules import simplify_rules

rules = simplify_rules(train, rules)
display(len(rules))
rules
15
Out[49]:
[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 [64]:
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(3, variable_type="quant")
temp.view()
In [65]:
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[65]:
[IF density[low] AND al[high] THEN temp[low]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF (density[low] AND ti[low]) AND al[low] THEN temp[average]
 	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[high]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF density[low] THEN temp[average]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF density[lower] THEN temp[high]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF density[average] THEN temp[high]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax,
 IF density[average] AND al[low] THEN temp[high]
 	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[low]
 	AND aggregation function : fmin
 	OR aggregation function  : fmax]
In [66]:
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[low]
	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[low] : 0.0

RULE #1:
  IF (density[low] AND ti[low]) AND al[low] THEN temp[average]
	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[average] : 0.5693410286540264

RULE #2:
  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 #3:
  IF density[low] AND ti[high] THEN temp[high]
	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[high] : 0.0

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

  Aggregation (IF-clause):
  - density[low]                                           : 0.5693410286540264
                                              density[low] = 0.5693410286540264
  Activation (THEN-clause):
                                             temp[average] : 0.5693410286540264

RULE #5:
  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 #6:
  IF density[average] THEN temp[high]
	AND aggregation function : fmin
	OR aggregation function  : fmax

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

RULE #7:
  IF density[average] AND al[low] THEN temp[high]
	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[high] : 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[low]
	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[low] : 0.0


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

np.float64(47.13412979960593)
In [67]:
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[67]:
T Al2O3 TiO2 Density Real Inferred RMSE
0 20 0.00 0.0 1.06250 20 46.094987 26.095
1 25 0.00 0.0 1.05979 25 46.553081 21.553
2 35 0.00 0.0 1.05404 35 47.812656 12.813
3 40 0.00 0.0 1.05103 40 48.650748 8.651
4 45 0.00 0.0 1.04794 45 49.664863 4.665
5 50 0.00 0.0 1.04477 50 50.899825 0.900
6 60 0.00 0.0 1.03826 60 54.286981 5.713
7 65 0.00 0.0 1.03484 65 56.744067 8.256
8 70 0.00 0.0 1.03182 70 59.521802 10.478
9 20 0.05 0.0 1.08755 20 45.513146 25.513
10 45 0.05 0.0 1.07105 45 45.157215 0.157
11 50 0.05 0.0 1.06760 50 45.445257 4.555
12 55 0.05 0.0 1.06409 55 45.863334 9.137
13 65 0.05 0.0 1.05691 65 47.132241 17.868
14 70 0.05 0.0 1.05291 70 48.111432 21.889
In [68]:
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[68]:
T Al2O3 TiO2 Density Real Inferred RMSE
0 30 0.00 0.00 1.05696 30 47.121319 17.121
1 55 0.00 0.00 1.04158 55 52.390781 2.609
2 25 0.05 0.00 1.08438 25 45.222921 20.223
3 30 0.05 0.00 1.08112 30 45.031722 15.032
4 35 0.05 0.00 1.07781 35 44.952086 9.952
5 40 0.05 0.00 1.07446 40 44.992751 4.993
6 60 0.05 0.00 1.06053 60 46.419899 13.580
7 35 0.30 0.00 1.17459 35 32.104404 2.896
8 65 0.30 0.00 1.14812 65 47.920831 17.079
9 45 0.00 0.05 1.07424 45 44.999669 0.000
10 50 0.00 0.05 1.07075 50 45.177446 4.823
11 55 0.00 0.05 1.06721 55 45.485450 9.515
12 20 0.00 0.30 1.22417 20 28.316733 8.317
13 30 0.00 0.30 1.21310 30 28.432013 1.568
14 40 0.00 0.30 1.20265 40 29.056559 10.943
15 60 0.00 0.30 1.18265 60 31.098758 28.901
16 70 0.00 0.30 1.17261 70 32.363220 37.637
In [69]:
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[69]:
{'RMSE_train': 15.354573113986062,
 'RMSE_test': 15.530460096876435,
 'RMAE_test': 3.4741778705724395,
 'R2_test': -0.13066358713988602}