You are on page 1of 5

Appendix A: Extra Tree Model Program C

#!/usr/bin/env python
# coding: utf-8
# In[200]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import scipy as sp
import sklearn as sk
import pickle
get_ipython().run_line_magic('matplotlib', 'inline')
# In[292]:
import os, sys
os.listdir()
# In[202]:
data_set = pd.read_excel('BHP.xlsx')
data_set.head()
# In[203]:
data_set.info()
# In[204]:
data_set.describe()
# In[205]:
data_set.columns
# In[206]:
sns.heatmap(data_set.corr())
# Hence, i will create a model that excludes these parameters and see the effect on the model's
accuracy.
# In[207]:
sns.distplot(data_set['BHP'],color = 'green')
# MODEL WITH ALL PARAMETERS
# In[208]:
X = data_set[['depth (ft)', 'temperature (oF)', 'pressure gradient (psi/ft)']]
# In[209]:
Y = data_set['BHP']
# In[210]:
X.shape, Y.shape
# In[211]:
from sklearn.model_selection import train_test_split
# In[271]:
X_train, X_test,Y_train, Y_test = train_test_split(X, Y, test_size = 0.15, random_state = 5)
# In[272]:
X_train.shape, Y_train.shape
# In[273]:
print(X_train.shape , Y_train.shape)
print(X_test.shape , Y_test.shape)
# In[274]:
from sklearn.ensemble import ExtraTreesRegressor
# In[275]:
model=ExtraTreesRegressor(random_state=65,n_estimators=6)
#542
# In[276]:
model.fit(X_train, Y_train);
# In[277]:
prediction = model.predict(X_test)
# In[278]:
model.score(X_train, Y_train)
# In[279]:
model.score(X_test, Y_test)
# In[280]:
from pandas import DataFrame
df= DataFrame({'Experimental values':Y_test, 'Predicted values':prediction})
df
# In[281]:
df.to_csv('ExtraTrees result.csv')
# In[282]:
sns.regplot(Y_test,prediction)
plt.title('A regression plot of Experimented data vs Predicted data')
plt.show()
# In[283]:
plt.scatter(Y_test,prediction)
plt.title('A regression plot of Experimented data vs Predicted data')
plt.show()
# In[284]:
from sklearn import metrics
# In[307]:
mse = metrics.mean_squared_error(Y_test,prediction)
mse
# In[286]:
RMSE = np.sqrt(mse)
RMSE
# In[287]:
MAE = metrics.mean_absolute_error(Y_test,prediction)
MAE
# In[288]:
R_Squr = metrics.r2_score(Y_test,prediction)
R_Squr
# In[289]:
BHPEXtramodel1='BHPextramodel1.sav'
# In[290]:
pickle.dump(model, open(BHPEXtramodel1, 'wb'))
# In[291]:
loaded_model=pickle.load(open(BHPEXtramodel1, 'rb'))
# In[293]:
data_setb = pd.read_excel('TestData.xlsx')
# In[294]:
Xa = data_setb[['depth (ft)', 'temperature (oF)', 'pressure gradient (psi/ft)']]
# In[295]:
Ya= data_setb['BHP']
# In[296]:
train_pct_index=int(0.0*len(Xa))
# In[297]:
test_pct_index=int(1.0*len(Xa))
# In[298]:
Xa_train,Xa_test=Xa[:train_pct_index],Xa[train_pct_index:test_pct_index]
# In[299]:
Ya_train,Ya_test=Ya[:train_pct_index],Ya[train_pct_index:test_pct_index]
# In[300]:
print(Xa_train.shape , Ya_train.shape)
print(Xa_test.shape , Ya_test.shape)
# In[301]:
result=loaded_model.predict(Xa_test)
# In[302]:
from pandas import DataFrame
df= DataFrame({'pred':result})
df
# In[303]:
df.to_csv('ExtraTreesBHPresult.csv')
# In[310]:
msea = metrics.mean_squared_error(Ya_test,result)
msea
# In[308]:
RMSE = np.sqrt(msea)
RMSE
# In[311]:
MAEa = metrics.mean_absolute_error(Ya_test,result)
MAEa
# In[312]:
R_Squar = metrics.r2_score(Ya_test,result)
R_Squar
# In[ ]:

Appendix B: Feed Forward Model Program Code

import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
import pandas as pd
from sklearn.model_selection import train_test_split
import torch.nn.functional as F
from math import sqrt
from sklearn.metrics import mean_squared_error, accuracy_score, r2_score,
mean_absolute_error

class Net(torch.nn.Module):
def __init__(self, n_feature, n_output):
super(Net, self).__init__()

self.fc1 = nn.Linear(n_feature, 100)


self.fc2 = nn.Linear(100, 50)
self.fc3 = nn.Linear(50, 50)
self.predict = nn.Linear(50, n_output)

def forward(self, x):


x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = self.predict(x)

return x
def save_checkpoint(state, filename='my_checkpoint.pth.tar'):
print("=> Saving checkpoint")
torch.save(state, filename)

df = pd.read_excel (r'BHP data and analysis.xlsx')


col_x = pd.DataFrame(df, columns=['depth (ft)','temperature (oF)','pressure gradient (psi/ft)'])
col_y = pd.DataFrame(df, columns=['BHP'])

Xnump = col_x.values
Ynump = col_y.values
X = torch.from_numpy(Xnump.astype(np.float32))
Y = torch.from_numpy(Ynump.astype(np.float32))
#Y = Y.view(Y.shape[0], 1)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.1)
n_samples, n_features = X_test.shape

input_size = n_features
output_size = 1

model = Net(n_feature=n_features, n_output=output_size)


learning_rate = 0.0001
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate, weight_decay = 0.001 )
num_epochs = 50000
batch_size = 10
for epoch in range(num_epochs):
losses = []
batx = 0
steps = round((X_train.shape[0])/batch_size)
for batch in range(steps):
if X_train[batx:].shape[0] > batch_size:
y_predicted = model(X_train[batx:batx+batch_size])

loss = criterion(y_predicted, Y_train[batx:batx+batch_size])

rms = sqrt(mean_squared_error(Y_train[batx:batx+batch_size],
y_predicted.detach().numpy()))

accuracy = r2_score(Y_train[batx:batx+batch_size], y_predicted.detach().numpy())

losses.append((loss.item())**2)

optimizer.zero_grad()
loss.backward()
optimizer.step()
batx += batch_size

else:
y_predicted = model(X_train[batx:])
loss = criterion(y_predicted, Y_train[batx:])
rms = sqrt(mean_squared_error(Y_train[batx:], y_predicted.detach().numpy()))
accuracy = r2_score(Y_train[batx:], y_predicted.detach().numpy())
losses.append((loss.item())**2)
optimizer.zero_grad()
loss.backward()
optimizer.step()

if (epoch+1) % 10 == 0:
print(f'epoch: {epoch+1}, loss = {loss.item()}, RMSE = {rms}, accuracy: {accuracy}')
checkpoint = {'state_dict' : model.state_dict(), 'optimizer': optimizer.state_dict}
save_checkpoint(checkpoint)

prediction = model(X_test).detach().numpy()
print(X_test)
print(prediction)
accuracy = r2_score(Y_test, prediction)
mae = mean_absolute_error(Y_test, prediction)
mse = mean_squared_error(Y_test, prediction)
rmse = sqrt(mse)

print(f'accuracy: {accuracy}, MAE: {mae}, MSE: {mse}, RMSE: {rmse}')

plt.plot(X_test, Y_test, 'ro')


plt.plot(X_test, prediction, 'b')

plt.show()

You might also like