<jupyter_start><jupyter_text><jupyter_code># Importando os módulos
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
import seaborn as sns
#encontrando o dataframe
df = pd.read_excel('Folds5x2_pp.xlsx', sheet_name='Sheet1')
#mostrando o dataset
df.head()<jupyter_output><empty_output><jupyter_text>- Temperature (T) in the range 1.81°C and 37.11°C,- Ambient Pressure (AP) in the range 992.89-1033.30 milibar,- Relative Humidity (RH) in the range 25.56% to 100.16%- Exhaust Vacuum (V) in teh range 25.36-81.56 cm Hg- Net hourly electrical energy output (PE) 420.26-495.76 MW<jupyter_code>df.shape<jupyter_output><empty_output><jupyter_text>**Análise Exploratória**<jupyter_code>#encontrando as estatísticas descritivas
df.describe()
#analisando a distribuição dos dados
df.PE.hist()
df.PE.hist(bins = 50);
df[['AT', 'V', 'AP', 'RH']].hist();
df[['AT', 'V', 'RH']].boxplot();<jupyter_output><empty_output><jupyter_text>**Engenharia de Características**<jupyter_code>#verificando a multicolinearidade
corr = df.corr()
plt.figure(figsize=(10,5))
sns.heatmap(corr, annot=True)
plt.show()
f,axes=plt.subplots(2,2,figsize=(15,7))

sns.scatterplot(y='PE',x='AT',data=df,ax=axes[0,0])
sns.scatterplot(y='PE',x='AP',data=df,ax=axes[0,1],color='green')
sns.scatterplot(y='PE',x='V',data=df,ax=axes[1,0],color='red')
sns.scatterplot(y='PE',x='RH',data=df,ax=axes[1,1])

plt.tight_layout()

#utilizando outros métodos para identificar multicolinearidade
#VIF (Variable Inflation Factors)
from statsmodels.stats.outliers_influence import variance_inflation_factor

def calc_vif(X):

    # calculando o VIF
    vif = pd.DataFrame()
    vif["variáveis"] = X.columns
    vif["VIF"] = [variance_inflation_factor(X.values, i) for i in range(X.shape[1])]

    return(vif)<jupyter_output>/usr/local/lib/python3.7/dist-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
  import pandas.util.testing as tm<jupyter_text>![image.png](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHUAAAA+CAIAAACa+cNEAAADg0lEQVR4Ae2b0ZHjIAyG1QI1uAV6oARqcAvpIB24AypwBTSQBuiAHnRzqxkdiyH2kii3ieWHHcUBGT7/FkLOAuohSQAknatvVL6yIlC+yleWgKx31a/ylSUg6131q3yfSiDGaK2NMT7Va9fZifSbUnLOwdehfLuKGPsi5+ycizGu6woAyncM436vGKPy3cc03EL5DqM71FH5HsI03Ej5DqM71FH5HsI03Ej5DqPb75hzvlwuAHC5XPZbP6NFY39xvV4BYJ7nnv9pmgAgpYSIy7IAwPV65ca3241y+Oqv957b/BeDNxc8MOec9EgafAmQMaZ5bfp2mib6lm5GyZceQGOM+36UbZqeP/Jkgy8ikkLXdd3OmZ6vZVnu832BNLZj+4Vn2nwJYjNElMEBEXv6Vb50s9t8U0oAsA0RFBystawU5csomkabLyJaawEghFB2I12XJx/nO8/z90D979PLVvlyjs+1u3wpMagWfWMMAOSceRA9vtv1rRnNOdbzml4aHxBkunxzzjRVpkmVvYp4j2+JiWxeEvnejBlbz68/c3zkXb6I6L0vQ8Q8zwBQybDHV056r6e5veJz+IYQAIAFa76OyvXr+VYD+OUf7+k358wBl4LDNmN7nC8lfFuNAIDcQ/Cyu3KPLyJSTFiWhYzta5XH+Z40f6A7TLK11hpjeE9c3vzH+ZbePs/e0W+ZPzWzUeV7XxP7fIkgANxut62vXv3sA0InIoYQuOpmrW0S2DIpz+zzLVufzXbOUdkvpTRN04Bo3ptvSsl7P1z5pFJqmbpYa6sEnyXlnONUlU/uGu/Kl99EVNX93QlXDSj6UV6Uc+6lSTFGY8yJ4oP3PoRAm/hh/XJ9laGToiuHIYSx4Iv4/v8f8KB+qVjHfGnLWqb5BJeLMNzyoPGu8YGn9yBf7p5zDiEYY8o96rqu0zQR3JxzcwfAI2kan8m3XLIqu3z2q/WNfgBYYqLyAHs4Xf7wN8B9f3td0tm1aXGjF+Hee2MM2bsdjzf4TP0enL9zjh950vKzitQ8gFPzrX7mIVGxe2++tNw75wbWd+pbFlVoK1wmDyzDYeNd+XJVhBcfgB/MpfyREccEqqX0Ki1jiH8wprELnLyX8pUVgPJVvrIEZL2rfpWvLAFZ76pf5StLQNa76lf5yhKQ9a76Vb6yBGS9q36VrywBWe+qX+UrS0DWu+pXlu8fyVhDSaTZn8UAAAAASUVORK5CYII=)<jupyter_code>X = df.iloc[:,:-1]
calc_vif(X)
# começa em 1
# VIF < 5 -> baixa multicolinearidade
# VIF > 10 -> alta multicolinearidade<jupyter_output><empty_output><jupyter_text>**Ajustando a multicolinearidade**<jupyter_code>from sklearn.preprocessing import PolynomialFeatures
feature_cols = ['AT', 'V', 'AP', 'RH']
poly_cols = ['Coff','AT', 'V', 'AP', 'RH', 'AT2', 'AT*V', 'AT*AP', 'AT*RH', 'V2', 'V*AP', 'V*RH', 'AP2', 'AP*RH', 'RH2']
X = df[feature_cols]
# Invoking polynomial feature transform method with degree of 2 for two variables
poly_reg = PolynomialFeatures(degree=2, interaction_only=False)
X_poly = poly_reg.fit_transform(X)

# Converting array into a Dataframe
X_poly = pd.DataFrame(X_poly, columns=poly_cols, index=df.index)
#[a, b]
#[1, a, b, a^2, ab, b^2]
X_poly.head()<jupyter_output><empty_output><jupyter_text>**Aplicando os algoritmos sem a transformação**<jupyter_code>#aplicando a normalização para os dados
scaler = MinMaxScaler()
X_new = scaler.fit_transform(df[['AT', 'V', 'AP', 'RH']])
#aplicando a normalização para os dados de saída
target_scaler = MinMaxScaler()

Y_new = target_scaler.fit_transform(df['PE'].values.reshape(-1,1))
#dividindo os dados entre treinamento e teste
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X_new, Y_new, test_size=0.4, random_state=333)<jupyter_output><empty_output><jupyter_text>**Aplicando a Floresta Randomica**<jupyter_code>from sklearn.ensemble import RandomForestRegressor
rnf = RandomForestRegressor(n_estimators=300)
rnf.fit(X_train, y_train)
from sklearn.metrics import mean_squared_error
y_preds = rnf.predict(X_test)

print("RMSE : {:.3f} ".format(mean_squared_error(y_test, y_preds, squared=False)))<jupyter_output>RMSE : 0.047<jupyter_text>**Aplicando uma MLP**<jupyter_code># camada inicial e o tipo 
from tensorflow import keras
from tensorflow.keras import layers

#entradas

# criando o formato da entrada
input_shape = (X_train.shape[1],)
print(f'Shape das entradas: {input_shape}')
# Criando o modelo
model = keras.Sequential()
model.add(layers.Dense(16, input_shape=input_shape, activation='relu'))
model.add(layers.Dense(8, activation='relu'))
model.add(layers.Dense(1, activation='linear'))
#mostrando o modelo
print(model.summary())
#treina o modelo
model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['mean_squared_error'])
model.fit(X_train, y_train, epochs=10, batch_size=1, verbose=1, validation_split=0.2)
y_pred = model.predict(X_test)
np.set_printoptions(precision=2)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
from sklearn.metrics import r2_score
r2_score(y_test,y_pred)
print("RMSE : {:.3f} ".format(mean_squared_error(y_test, y_pred, squared=False)))<jupyter_output>RMSE : 0.057<jupyter_text>**Aplicando os algoritmos com a transformação**<jupyter_code>#aplicando a normalização para os dados
scaler = MinMaxScaler()
X_new = scaler.fit_transform(X_poly)
#aplicando a normalização para os dados de saída
target_scaler = MinMaxScaler()

Y_new = target_scaler.fit_transform(df['PE'].values.reshape(-1,1))
#dividindo os dados entre treinamento e teste
X_train, X_test, y_train, y_test = train_test_split(X_new, Y_new, test_size=0.4, random_state=333)
#aplicando a floresta randômica como regressora
rnf = RandomForestRegressor(n_estimators=300)
rnf.fit(X_train, y_train)

y_preds = rnf.predict(X_test)

print("RMSE : {:.3f} ".format(mean_squared_error(y_test, y_preds, squared=False)))
# criando o formato da entrada para o 
input_shape = (X_train.shape[1],)
print(f'Shape das entradas: {input_shape}')
# Criando o modelo
model = keras.Sequential()
model.add(layers.Dense(16, input_shape=input_shape, activation='relu'))
model.add(layers.Dense(8, activation='relu'))
model.add(layers.Dense(1, activation='linear'))
#mostrando o modelo
print(model.summary())
#treina o modelo
model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['mean_squared_error'])
history=model.fit(X_train, y_train, epochs=30,batch_size=10, verbose=1, validation_split=0.2)
history.history.keys()
#história do treinamento
plt.plot(history.history['mean_squared_error'])
plt.plot(history.history['val_mean_squared_error'])
plt.title('Erro médio quadrático')
plt.ylabel('MSE')
plt.xlabel('epoca')
plt.legend(['treinamento', 'teste'], loc='upper left')
plt.show()
# história do treinamento
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Variação da função perda')
plt.ylabel('loss')
plt.xlabel('epoca')
plt.legend(['treinamento', 'teste'], loc='upper left')
plt.show()
y_pred = model.predict(X_test)
np.set_printoptions(precision=2)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
r2_score(y_test,y_pred)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup