413 KiB
413 KiB
In [167]:
import pickle
import pandas as pd
from sklearn import tree
data = pd.read_csv("data-turbine/clear-data.csv")
model = pickle.load(open("data-turbine/tree-gs.model.sav", "rb"))
features = (
data
.drop(["CO"], axis=1)
.columns.values.tolist()
)
rules = tree.export_text(model, feature_names=features)
print(rules)
In [ ]:
from src.rules import get_rules
rules = get_rules(model, features)
display(len(rules))
rules
Out[ ]:
In [169]:
from src.rules import normalise_rules
rules = normalise_rules(rules)
display(len(rules))
rules
Out[169]:
In [170]:
from src.rules import delete_same_rules
rules = delete_same_rules(rules)
display(len(rules))
rules
Out[170]:
In [171]:
from sklearn.model_selection import train_test_split
random_state = 9
y = data["CO"]
X = data.drop(["CO"], axis=1).copy()
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=random_state
)
display(X_train, y_train, X_test, y_test)
In [172]:
from src.rules import simplify_rules
rules = simplify_rules(X_train, rules)
rules
Out[172]:
In [173]:
data.describe().transpose()
Out[173]:
In [198]:
import numpy as np
from skfuzzy import control as ctrl
import skfuzzy as fuzz
# AT = ctrl.Antecedent(np.arange(-6.2348, 37.103, 0.0001), "AT")
# AP = ctrl.Antecedent(np.arange(985.85, 1036.6, 0.01), "AP")
# AH = ctrl.Antecedent(np.arange(24.085, 100.2, 0.1), "AH")
# AFDP = ctrl.Antecedent(np.arange(2.0874, 7.6106, 0.0001), "AFDP")
TIT = ctrl.Antecedent(np.arange(1000.8, 1100.9, 0.1), "TIT")
TAT = ctrl.Antecedent(np.arange(511.04, 550.61, 0.1), "TAT")
CO = ctrl.Consequent(np.arange(0.000388, 44.103, 0.1), "CO")
# AT.automf(3, variable_type="quant")
# AP.automf(5, variable_type="quant")
# AH.automf(5, variable_type="quant")
# AH.view()
# AFDP.automf(3, variable_type="quant")
TIT.automf(3, variable_type="quant")
# TIT["low"] = fuzz.zmf(TIT.universe, 1025, 1035)
# TIT["average"] = fuzz.trapmf(TIT.universe, [1025, 1035, 1056, 1075])
# TIT["high"] = fuzz.smf(TIT.universe, 1065, 1075)
TIT.view()
TAT.automf(3, variable_type="quant")
# TAT["low"] = fuzz.zmf(TAT.universe, 520, 525)
# TAT["average"] = fuzz.trapmf(TAT.universe, [520, 525, 535, 540])
# TAT["high"] = fuzz.smf(TAT.universe, 535, 540)
TAT.view()
CO.automf(names=[str(i) for i in range(1, 18)], variable_type="quant")
# CO["low"] = fuzz.zmf(CO.universe, 10, 13)
# CO["average"] = fuzz.trapmf(CO.universe, [10, 13, 30, 33])
# CO["high"] = fuzz.smf(CO.universe, 30, 33)
CO.view()
In [ ]:
import matplotlib.pyplot as plt
data.hist(bins=30, figsize=(10, 10))
plt.show()
In [199]:
from src.rules import get_fuzzy_rules
fuzzy_variables = {
# "AT": AT,
# "AP": AP,
# "AH": AH,
# "AFDP": AFDP,
"TIT": TIT,
"TAT": TAT,
"consequent": CO,
}
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[199]:
In [208]:
# sim.input["AT"] = 4.5878
# sim.input["AP"] = 1018.7
# sim.input["AH"] = 83.675
# sim.input["AFDP"] = 3.5758
# sim.input["TIT"] = 1086.2
# sim.input["TAT"] = 549.83
# sim.input["AT"] = 7.8167
# sim.input["AP"] = 1022.2
# sim.input["AH"] = 88.135
# sim.input["AFDP"] = 4.6605
# sim.input["TIT"] = 1100.0
# sim.input["TAT"] = 526.21
sim.input["TIT"] = 1032.5
sim.input["TAT"] = 524.71
sim.compute()
sim.print_state()
display(sim.output["CO"])
CO.view(sim=sim)
In [201]:
from sklearn import metrics
import math
def fuzzy_pred(row):
# sim.input["AT"] = row["AT"]
# sim.input["AP"] = row["AP"]
# sim.input["AH"] = row["AH"]
# sim.input["AFDP"] = row["AFDP"]
sim.input["TIT"] = row["TIT"]
sim.input["TAT"] = row["TAT"]
sim.compute()
return sim.output["CO"]
def rmse(row):
return math.sqrt(metrics.mean_squared_error([row["Real"]], [row["Inferred"]]))
result_train = X_train.copy()
result_train["Real"] = y_train
result_train = result_train[:10000]
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[201]:
In [202]:
result_test = X_test.copy()
result_test["Real"] = y_test
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[202]:
In [209]:
result_test.sort_values(by="Real", ascending=False).head(30)
Out[209]:
In [203]:
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_train"] = math.sqrt(
metrics.mean_absolute_error(result_train["Real"], result_train["Inferred"])
)
rmetrics["RMAE_test"] = math.sqrt(
metrics.mean_absolute_error(result_test["Real"], result_test["Inferred"])
)
rmetrics["MAE_train"] = float(
metrics.mean_absolute_error(result_train["Real"], result_train["Inferred"])
)
rmetrics["MAE_test"] = float(
metrics.mean_absolute_error(result_test["Real"], result_test["Inferred"])
)
rmetrics["R2_train"] = metrics.r2_score(result_train["Real"], result_train["Inferred"])
rmetrics["R2_test"] = metrics.r2_score(result_test["Real"], result_test["Inferred"])
rmetrics
Out[203]: