In [26]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from gaussian_processes_util import plot_gp_2D
noise_2D = 0.1
rx, ry = np.arange(-5, 5, 0.3), np.arange(-5, 5, 0.3)
gx, gy = np.meshgrid(rx, rx)
X_2D = np.c_[gx.ravel(), gy.ravel()]
Y_2D_truth = np.sin(0.5 * np.linalg.norm(X_2D, axis=1))
X_2D_train = np.random.uniform(-4, 4, (100, 2))
Y_2D_train = np.sin(0.5 * np.linalg.norm(X_2D_train, axis=1)) + \
noise_2D * np.random.randn(len(X_2D_train))
sinewave1 = pd.DataFrame(X_2D_train, columns=['x1', 'x2'])
sinewave2 = pd.DataFrame(Y_2D_train, columns=['y'])
sinewave =pd.concat([sinewave1, sinewave2], axis=1)
plt.figure(figsize=(14,7))
plot_gp_2D(gx, gy, Y_2D_truth, X_2D_train, Y_2D_train,
f'3D Sinewave and Noisy Samples', 1)
In [50]:
import sys
sys.path.insert(0, '\\\\newwinsrc\\sasgen\\dev\\mva-vb023\\GTKWX6ND\\misc\\python') # location of src
import swat as sw
s = sw.CAS('rdcgrd327.unx.sas.com', 29640, nworkers=2)
s.sessionprop.setsessopt(caslib='CASUSER',timeout=31535000)
Out[50]:
In [60]:
if s.tableexists('sinewave').exists:
s.CASTable('sinewave').droptable()
dataset = s.upload_frame(sinewave,
importoptions=dict(vars=[dict(type='double'),
dict(type='double'),
dict(type='double')
]),
casout=dict(name='sinewave', promote=True))
In [55]:
s.loadactionset(actionset="nonParametricBayes")
s.gpreg(
table={"name":"sinewave"},
inputs={"x1","x2"},
target="y",
seed=1234,
nInducingPoints=7,
fixInducingPoints=False,
kernel="RBF",
partbyfrac={"valid":0, "test":0, "seed":1235},
nloOpts={"algorithm":"ADAM",
"optmlOpt":{"maxIters":91},
"sgdOpt":{"learningRate":0.15,
"momentum":0.8,
"adaptiveRate":True,
"adaptiveDecay":0.9,
"miniBatchSize":100
},
"printOpt":{"printFreq":10}
},
output={"casout":{"name":"GpReg_Pred", "replace":True}, "copyvars":"ALL"},
outInducingPoints={"name":"GpReg_inducing", "replace":True},
outVariationalCov={"name":"GpReg_S", "replace":True},
saveState={"name":"gpregStore", "replace":True}
)
Out[55]:
In [56]:
s.gpreg(
table={"name":"sinewave"},
inputs={"x1","x2"},
target="y",
seed=1234,
nInducingPoints=25,
fixInducingPoints=True,
kernel="RBF",
partbyfrac={"valid":0, "test":0, "seed":1235},
nloOpts={"algorithm":"ADAM",
"optmlOpt":{"maxIters":1},
"sgdOpt":{"learningRate":0.15,
"momentum":0.8,
"adaptiveRate":True,
"adaptiveDecay":0.9,
"miniBatchSize":100
},
"printOpt":{"printFreq":1}
},
output={"casout":{"name":"GpReg_Pred1", "replace":True}, "copyvars":"ALL"},
outInducingPoints={"name":"GpReg_inducing1", "replace":True},
outVariationalCov={"name":"GpReg_S1", "replace":True},
saveState={"name":"gpregStore1", "replace":True}
)
Out[56]:
In [57]:
sinewave_test = pd.DataFrame(X_2D, columns=['x1', 'x2'])
if s.tableexists('sinewave_test').exists:
s.CASTable('sinewave_test').droptable()
dataset = s.upload_frame(sinewave_test,
importoptions=dict(vars=[dict(type='double'),
dict(type='double')
]),
casout=dict(name='sinewave_test', promote=True))
In [58]:
if s.tableexists('test_pred').exists:
s.CASTable('test_pred').droptable()
if s.tableexists('test_pred1').exists:
s.CASTable('test_pred1').droptable()
s.loadactionset('aStore')
s.score(
table='sinewave_test',
out='test_pred',
rstore='gpregStore',
)
pred = s.CASTable("test_pred").to_frame()
s.loadactionset('aStore')
s.score(
table='sinewave_test',
out='test_pred1',
rstore='gpregStore1',
)
pred1 = s.CASTable("test_pred1").to_frame()
In [59]:
mu = pred['P_yMEAN'].values
mu1 = pred1['P_yMEAN'].values
plt.figure(figsize=(14,7))
plot_gp_2D(gx, gy, mu1, X_2D_train, Y_2D_train,
f'Prediction after 1st Iteration', 1)
plot_gp_2D(gx, gy, mu, X_2D_train, Y_2D_train,
f'Prediction After 201 Iterations', 2)
In [ ]: