Soft Sensor Code
Soft Sensor Code
Soft Sensor Code
#Load data
dataset = loadtxt("ANN V4.csv", delimiter=',')
X = dataset[:,0:5]
y = dataset[:,8]
rsqrddata = []
msedata = []
maedata = []
mapedata = []
model = Sequential()
model.add(Dense(32, input_dim=5, activation='relu'))
model.add(Activation("linear"))
model.add(Dense(20, activation='sigmoid'))
model.add(Dense(1, activation='relu'))
model.summary()
#Predict
y_predicted = model.predict(X_test)
#Metrics
Rsquared = (r2_score(y_test, y_predicted))
print("Rsquared : %f" %Rsquared)
mse = mean_squared_error(y_test, y_predicted)
print("MSE : %f" % mse)
mae = mean_absolute_error(y_test, y_predicted)
print ("MAE: %f" % mae)
mape = mean_absolute_percentage_error(y_test, y_predicted)
print ("MAPE: %f" % mape)
rmse = sqrt(mse)
print ("RMSE: %f" % rmse)
rsqrddata.append(Rsquared)
msedata.append(mse)
maedata.append(mae)
mapedata.append(mape)
#GRAPHS
fig, ax = plt.subplots()
ax.scatter(y_test, y_predicted ,s=10,color='mediumseagreen',linewidths=1)
ax.plot([y.min(), y.max()], [y.min(), y.max()], 'k-', lw=1.75)
ax.set_xlabel('Actual (%)',fontsize='x-large')
ax.set_ylabel('Predicted (%)',fontsize='x-large')
y_test, y_predicted = y_test.reshape(-1,1), y_predicted.reshape(-1,1)
ax.plot(y_test, LinearRegression().fit(y_test, y_predicted).predict(y_test),
color="red", lw=1.75)
ax.set_title('CH4 Conversion')
handles = [mpl_patches.Rectangle((0, 0), 1, 1, fc="white", ec="white",
lw=0, alpha=0)] * 4
# create the corresponding number of labels (= the text you want to display)
lines = []
handles[0] = Line2D([0], [0], color='red',lw=3)
labels = []
labels.append("R² = {0:.3g}".format(Rsquared))
labels.append("RMSE = {0:.2g}".format(rmse))
labels.append("MAE = {0:.2g}".format(mae))
ax.legend(handles, labels, loc='best', fontsize='xx-large',
fancybox=True, framealpha=0.7)
plt.show()
#RANDOM FOREST
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
from math import sqrt
import matplotlib.patches as mpl_patches
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error
plt.rcParams["font.family"] = "Cambria"
from matplotlib.lines import Line2D
plt.rcParams["font.family"] = "Cambria"
data.describe()
data = pd.get_dummies(data)
data.iloc[:,1:].head(1)
labels = np.array(data['H2'])
data= data.drop('H2', axis = 1)
data_list = list(data.columns)
data = np.array(data)
#Metrics
Rsquared = (r2_score(test_labels, predictions))
print("Rsquared : %f" %Rsquared)
_mse = mean_squared_error(test_labels, predictions)
print("MSE : %f" % _mse)
mae = mean_absolute_error(test_labels, predictions)
print ("MAE: %f" % mae)
rmse = sqrt(_mse)
print ("RMSE: %f" % rmse)
importances = list(rf.feature_importances_)
feature_importances_ = [(feature, round(importance, 4)) for feature, importance in
zip(data_list, importances)]
feature_importances_ = sorted(feature_importances_, key = lambda x: x[1], reverse =
True)
[print('Variable: {:20} Importance: {}'.format(*pair)) for pair in
feature_importances_];
#Graph
fig, ax = plt.subplots()
ax.scatter(test_labels, predictions ,s=10,color='MEDIUMSEAGREEN',linewidths=1)
ax.plot([labels.min(), labels.max()], [labels.min(), labels.max()], 'k-', lw=1.75)
ax.set_xlabel('Actual (%)',fontsize='x-large')
ax.set_ylabel('Predicted (%)',fontsize='x-large')
test_labels, predictions = test_labels.reshape(-1,1), predictions.reshape(-1,1)
ax.plot(test_labels, LinearRegression().fit(test_labels,
predictions).predict(test_labels), color="red", lw=1.75)
plt.show()