-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGP_TP.py
203 lines (170 loc) · 5.16 KB
/
GP_TP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python
# coding: utf-8
#importing important libraries
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import os
import math
#importing the ML libraries
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, RationalQuadratic, WhiteKernel
from sklearn.gaussian_process.kernels import ConstantKernel as C
#Reading the dataset
df = pd.read_csv('prior_Sample.csv',delimiter=',')
df2 = pd.read_csv('complete.csv',delimiter=',')
#print(df.head())
#sorting out the data
#df = df.sort_values(by=['Pressure'], ascending='True')
#print(df.head(n=40))
#Substitute a data row with some non-zero value if methane uptake for that row zero, this is to avoid log(0) = NaN error
#df['Uptake_for_Cu-BTC-CH4(300K)'].replace(to_replace=0, value=0.00001)
#print(df.head(n=40))
#print(df)
#Unseen array
P_test = np.atleast_2d(np.linspace(1e-6,300,50)).flatten().reshape(-1,1)
T_test = np.atleast_2d(np.linspace(100,300,40)).flatten().reshape(-1,1)
'''
FOR generating Prior dataset for comparision
for i in range(len(X_test)):
os.system("echo "+str(X_test[i])+" >> Prior.csv")
'''
#Reading the data
p = df.iloc[:,0].values
t = df.iloc[:,1].values
y = df.iloc[:,2].values
#Taking the error data as well
e = df.iloc[:,3].values
#from complete-original dataset
p2 = df2.iloc[:,0].values
t2 = df2.iloc[:,1].values
y2 = df2.iloc[:,2].values
#Replacing y if some y value in zero
for i in range(len(y)):
if (y[i] == 0):
y[i] = 0.0001
#For y2
for i in range(len(y2)):
if (y2[i] == 0):
y2[i] = 0.0001
#Transforming 1D arrays to 2D
p = np.atleast_2d(p).flatten().reshape(-1,1)
t = np.atleast_2d(t).flatten().reshape(-1,1)
y = np.atleast_2d(y).flatten()
p_true = p
t_true = t
y_actual = y
#before the transition
#print(x,y)
#converting P to bars
p = p/(1.0e5)
#Taking logbase 10 of the input vector
p = np.log10(p)
t = np.log10(t)
y = np.log10(y)
#print(len(x),len(y))
#Taking the log of X_test
P_test = np.log10(P_test)
T_test = np.log10(T_test)
#Extracting the mean and std. dev for P_test
p_m = np.mean(P_test)
p_std = np.std(P_test,ddof=1)
#Extracting the mean and std. dev for T_test
t_m = np.mean(T_test)
t_std = np.std(T_test,ddof=1)
#Standardising p,t and y in log-space
p_s = (p - p_m)/p_std
t_s = (t - t_m)/t_std
#Standardising X_test in log-space
P_test = (P_test - p_m)/p_std
T_test = (T_test - t_m)/t_std
#Initializing scaled down training and prediction set
#X_test = np.zeros((len(P_test)*len(T_test),2))
x_s = np.zeros((len(p_s),2))
#Filling all the data in training and prediction set
for i in range(len(p_s)):
for k in range(2):
#Inserting pressure for the first column
if k == 0:
x_s[i,k] = p_s[i]
else:
#Inserting temperature for the second column
x_s[i,k] = t_s[i]
'''
for i in range(len(P_test)):
for j in range(len(T_test)):
for k in range(2):
#Inserting pressure for the first column
if k == 0:
X_test[i+j,k] = P_test[i]
else:
#Inserting temperature for the second column
X_test[i+j,k] = T_test[j]
'''
#new definition of X_test
X1, X2 = np.meshgrid(P_test, T_test,indexing='ij')
#Testing the meshgrid function
'''
for i in range(len(X1)):
for j in range(len(X2)):
print(X1[i],X2[j])
print(X1,X2)
'''
#print("The type and size of X1 is:",type(X1),len(X1))
X_test = np.vstack((X1.flatten(), X2.flatten())).T
#print(x_s,y)
#Building the GP regresson
# Instantiate a Gaussian Process model
#kernel = C(1.0, (1e-3, 1e3)) * RationalQuadratic(10, (1e-2, 1e2))
kernel = RationalQuadratic(length_scale=50, alpha=0.5,length_scale_bounds=(1e-13,1e13),alpha_bounds=(1e-13,1e13))+ RationalQuadratic(length_scale=50, alpha=0.5,length_scale_bounds=(1e-13,1e13),alpha_bounds=(1e-13,1e13))
gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=100, normalize_y=True)
#print(type(x_s),type(y.T))
#print(y.T)
#Fitting our normalized data to the GP model
gp.fit(x_s,y.T)
'''
count = 0
for i in range(len(X_test)):
P_trial = (X_test[i,0]*p_std) + p_m
P_trial = 10**(P_trial)
P_trial = 1e5*(P_trial)
T_trial = (X_test[i,1]*t_std) + t_m
T_trial = 10**(T_trial)
print(P_trial,T_trial)
count += 1
print(count)
'''
# Make the prediction on the test data (ask for MSE as well)
y_pred, sigma = gp.predict(X_test, return_std=True)
Params = gp.get_params()
#print(Params)
#print(y_pred,sigma)
rel_error = np.zeros(len(sigma))
#finding the relative error—
for i in range(len(sigma)):
rel_error[i] = abs(sigma[i]/abs(y_pred[i]))
#define the limit for uncertainty
lim = 0.02
Max = np.amax(rel_error)
index = np.argmax(rel_error)
#transforming the index to original pressure point
#X_test = (X_test*x_std) + x_m
#X_test = 10**(X_test)
#print(X_test,10**(y_pred),rel_error)
#checking the whether the maximum uncertainty is less than out desired limit
if (Max >= lim):
Data = str(X_test[index])
Data = Data.replace("[","")
Data = Data.replace("]","")
#Doing the inverse transform now
X_test[index,0] = (X_test[index,0]*p_std) + p_m
X_test[index,0] = 10**(X_test[index,0])
X_test[index,0] = 1e5*(X_test[index,0])
X_test[index,1] = (X_test[index,1]*t_std) + t_m
X_test[index,1] = 10**(X_test[index,1])
print(X_test[index,0],X_test[index,1])
print("NOT_DONE ")
print(rel_error[index])
else:
print(index)
print("DONE")