import sklearn.datasets as data
import numpy as np
import matplotlib.pyplot as plt
from pysgpp.extensions.datadriven.uq.plot.plot1d import plotSG1d
from pysgpp.extensions.datadriven.uq.plot.plot2d import plotSG2d
from pysgpp.extensions.datadriven.learner import Types
import pysgpp as sg
def generate_friedman1(seed):
(X,y) = data.make_friedman1(n_samples=2000, random_state=seed, noise=1.0)
X = sg.DataMatrix(X)
y = sg.DataVector(y)
return (X,y)
def randu_vec(size):
return np.random.rand(size)
def randu_mat(nrows, ncols):
return np.random.rand(nrows, ncols)
print("generate dataset... ", end=' ')
data_tr,_ = generate_friedman1(123456)
print("Done")
print("generated a friedman1 dataset (10D) with 2000 samples")
print("create grid config... ", end=' ')
grid = sg.RegularGridConfiguration()
grid.dim_ = 10
grid.level_ = 3
grid.type_ = sg.GridType_Linear
print("Done")
print("create adaptive refinement config... ", end=' ')
adapt = sg.AdaptivityConfiguration()
adapt.numRefinements_ = 0
adapt.noPoints_ = 10
print("Done")
print("create solver config... ", end=' ')
solv = sg.SLESolverConfiguration()
solv.maxIterations_ = 1000
solv.eps_ = 1e-14
solv.threshold_ = 1e-14
solv.type_ = sg.SLESolverType_CG
print("Done")
print("create regularization config... ", end=' ')
regular = sg.RegularizationConfiguration()
regular.regType_ = sg.RegularizationType_Laplace
print("Done")
print("create learner config... ", end=' ')
crossValid = sg.CrossvalidationConfiguration()
crossValid.enable_ = False
crossValid.kfold_ = 3
crossValid.lambda_ = 3.16228e-06
crossValid.lambdaStart_ = 1e-1
crossValid.lambdaEnd_ = 1e-10
crossValid.lambdaSteps_ = 3
crossValid.logScale_ = True
crossValid.shuffle_ = True
crossValid.seed_ = 1234567
crossValid.silent_ = False
print("Done")
print("create the learner... ")
learner = sg.LearnerSGDE(grid, adapt, solv, regular, crossValid)
learner.initialize(data_tr)
print("start training... ")
learner.train()
print("done training")
kde = sg.KernelDensityEstimator(data_tr)
x = sg.DataVector(learner.getDim())
x.setAll(0.5)
print("-----------------------------------------------")
print(learner.getSurpluses().getSize(), " -> ", learner.getSurpluses().sum())
print("pdf_SGDE(x) = ", learner.pdf(x), " ~ ", kde.pdf(x), " = pdf_KDE(x)")
print("mean_SGDE = ", learner.mean(), " ~ ", kde.mean(), " = mean_KDE")
print("var_SGDE = ", learner.variance(), " ~ ", kde.variance(), " = var_KDE")
C = sg.DataMatrix(grid.dim_, grid.dim_)
print("----------------------- Cov_SGDE -----------------------")
learner.cov(C)
print(C)
print("----------------------- Cov_KDE -----------------------")
kde.cov(C)
print(C)
print("-----------------------------------------------")
opInvRos = sg.createOperationInverseRosenblattTransformation(learner.getGrid())
points = sg.DataMatrix(randu_mat(12, grid.dim_))
print(points)
pointsCdf = sg.DataMatrix(points.getNrows(), points.getNcols())
opInvRos.doTransformation(learner.getSurpluses(), points, pointsCdf)
points.setAll(0.0)
opRos = sg.createOperationRosenblattTransformation(learner.getGrid())
opRos.doTransformation(learner.getSurpluses(), pointsCdf, points)
print("-----------------------------------------------")
print(pointsCdf)
print("-----------------------------------------------")
print(points)
if __name__ == '__main__':