예제 #1
0
def evaluate(X_tr, y_tr, X_te, y_te, interactions=None):
    grid = sg.RegularGridConfiguration()
    grid.dim_ = 64
    grid.level_ = 2
    grid.type_ = sg.GridType_ModLinear

    adapt = sg.AdaptivityConfiguration()
    adapt.numRefinements_ = 0
    adapt.noPoints_ = 0

    solv = sg.SLESolverConfiguration()
    solv.maxIterations_ = 50
    solv.eps_ = 10e-6
    solv.threshold_ = 10e-6
    solv.type_ = sg.SLESolverType_CG

    final_solv = solv
    final_solv.maxIterations = 200

    regular = sg.RegularizationConfiguration()
    regular.type_ = sg.RegularizationType_Identity
    regular.exponentBase_ = 1.0
    regular.lambda_ = 0.1    

    X_tr = sg.DataMatrix(X_tr)
    y_tr = sg.DataVector(y_tr)
    X_te = sg.DataMatrix(X_te)
    y_te = sg.DataVector(y_te)
    
    if interactions is None:
        estimator = sg.ClassificationLearner(grid, adapt, solv, final_solv,regular)
    else:
        estimator = sg.ClassificationLearner(grid, adapt, solv, final_solv,regular, interactions)
    estimator.train(X_tr,y_tr)
    return estimator.getAccuracy(X_te,y_te)
def evaluate_one(estimator, X, y, train, test):
    train_X = sg.DataMatrix(X[train])
    train_y = sg.DataVector(y[train])
    test_X = sg.DataMatrix(X[test])
    test_y = sg.DataVector(y[test])
    estimator.train(train_X, train_y)
    error = estimator.getMSE(test_X, test_y)
    return error
예제 #3
0
def eval_rosenblattdd(sg_pdf, xs):
    op = pysgpp.createOperationRosenblattTransformation(sg_pdf.grid)
    X, Y = np.meshgrid(xs, xs)
    input_points = pysgpp.DataMatrix(len(xs), sg_pdf.d)
    output_points = pysgpp.DataMatrix(len(xs), sg_pdf.d)
    for i in range(len(xs)):
      for j in range(sg_pdf.d):
        input_points.set(i, j , 0.5)
    op.doTransformation(sg_pdf.alpha, input_points, output_points)
    print(output_points)
예제 #4
0
    def __init__(self, gridType, basisType, degree, growthFactor,
                 levelManagerType, distType, samples, params):
        self.operation = buildSparseGrid(gridType, basisType, degree,
                                         growthFactor)

        # set evaluation points
        self.numDims = samples.shape[1]
        self.samples_mat = pysgpp.DataMatrix(samples)
        self.samples_mat.transpose()
        self.operation.setParameters(self.samples_mat)

        self.levelManagerType = levelManagerType
        self.levelManager = buildLevelManager(levelManagerType)
        if levelManagerType == "variance":
            self.orthogonal_basis = params.getOrthogonalPolynomialBasisFunctions(
            ).get(0)
            self.tensor_operation = buildSparseGrid(
                gridType,
                basisType,
                degree,
                growthFactor,
                orthogonal_basis=self.orthogonal_basis)
            self.tensor_operation.getLevelManager().addRegularLevels(1)
            self.tensor_operation.setLevelManager(self.levelManager)
        else:
            self.operation.setLevelManager(self.levelManager)
예제 #5
0
def test_laplace2(grid, lmax):
    # grid.getGenerator().regular(lmax)
    gridStorage = grid.getStorage()
    size = gridStorage.getSize()
    m = pysgpp.DataMatrix(size, size)
    op = pysgpp.createOperationLaplaceExplicit(m, grid)
    print(m)
예제 #6
0
def test_LTwoDot(grid, l):
    res = 10000
    b = grid.getBasis();
    grid.getGenerator().regular(l)
    gridStorage = grid.getStorage()
    size = gridStorage.getSize()
    # print(size)
    m = pysgpp.DataMatrix(size, size)
    opMatrix = pysgpp.createOperationLTwoDotExplicit(m, grid)
    # print m
    # m_comp = pysgpp.DataMatrix(size, size)
    for i in range(gridStorage.getSize()):
        for j in range(i, gridStorage.getSize()):
            gpi = gridStorage.getPoint(i)
            gpj = gridStorage.getPoint(j)
            sol = 1
            # print "--------"
            # print "i:{} j:{}".format(i, j)
            for k in range(d):
                lik = gpi.getLevel(k)
                iik = gpi.getIndex(k)
                ljk = gpj.getLevel(k)
                ijk = gpj.getIndex(k)
                # print "i l,i: {},{}   j l,i: {},{}".format(lik, iik, ljk, ijk)
                xs = np.linspace(0, 1, res)
                tmp = sum([b.eval(lik, iik, x)*b.eval(ljk, ijk, x) for x in xs]) / res
                sol *= tmp
                # print("lik:{} iik:{} ljk:{} ijk:{} k:{} tmp: {}".format(lik, iik, ljk, ijk, k,tmp))
            # print(sol)
            error = abs(m.get(i,j) - sol)
            # print error
            if(error >= 10**-4):
                print("i:{} j:{} error: {}".format(i, j, error))
                print("iik:{} lik:{} ijk:{} ljk:{} error: {}".format(iik, lik, ijk, ljk, error))
                print("is:{} should:{}".format(m.get(i,j), sol))
예제 #7
0
def generate_friedman1(seed):
    (X, y) = data.make_friedman1(n_samples=2000, random_state=seed, noise=1.0)

    # transform values to DataMatrix/DataVector types
    X = sg.DataMatrix(X)
    y = sg.DataVector(y)

    return (X, y)
예제 #8
0
def plotFunction(opEval, surpluses, X):
    if not doPlot: return

    # generate a meshgrid for plotting
    xx0 = np.linspace(0, 1, 65)
    xx1 = np.linspace(0, 1, 65)
    XX0, XX1 = np.meshgrid(xx0, xx1)
    XX = pysgpp.DataMatrix(np.column_stack([XX0.flatten(), XX1.flatten()]))

    # evaluate interpolant at meshgrid
    YY = pysgpp.DataVector(0)
    opEval.multiEval(surpluses, XX, YY)

    # convert resulting sgpp::base::DataVector to NumPy array
    YY = np.reshape(np.array([YY[k] for k in range(YY.getSize())]), XX0.shape)

    # actual plotting
    fig = plt.figure(figsize=(6, 6))
    ax = fig.gca(projection="3d")
    ax.plot_surface(XX0, XX1, YY)
    ax.plot(X[:, 0], X[:, 1], "k.", zs=f(X[:, 0], X[:, 1]), ms=10)
예제 #9
0
def test_firstMoment(grid, lmax):
    grid.getGenerator().regular(lmax)
    resolution = 100000
    gridStorage = grid.getStorage()
    b = grid.getBasis()
    op = pysgpp.createOperationFirstMoment(grid)
    alpha = pysgpp.DataVector(grid.getSize(), 1.0)
    bounds = pysgpp.DataMatrix(1, 2, 0.0)
    bounds.set(0, 1, 1.0)
    res = 0.0
    for i in range(grid.getSize()):
        lev = gridStorage.getPoint(i).getLevel(0)
        ind = gridStorage.getPoint(i).getIndex(0)
        temp_res = 0.0
        for c in range(resolution):
            x = float(c) / resolution
            temp_res += x * b.eval(lev, ind, x)
        res += alpha.get(i) * temp_res / resolution
    print("--FirstMoment--")
    print(res)
    print(op.doQuadrature(alpha, bounds))
    print(res - op.doQuadrature(alpha, bounds))
예제 #10
0
def test_LTwoDotImplicit(grid, l):
    grid.getGenerator().regular(l)
    gridStorage = grid.getStorage()
    size = gridStorage.getSize()
    # print(size)
    m = pysgpp.DataMatrix(size, size)
    opExplicit = pysgpp.createOperationLTwoDotExplicit(m, grid)
    op = pysgpp.createOperationLTwoDotProduct(grid)
    alpha = pysgpp.DataVector(size)
    resultExplicit = pysgpp.DataVector(size)
    result = pysgpp.DataVector(size)
    for i in range(size):
        alpha[i] = 1
    opExplicit.mult(alpha, resultExplicit)
    op.mult(alpha, result)
    for i in range(size):
        if result[i] != resultExplicit[i]:
            print("Error result entry {} differs".format(i))

        if abs(result[i] - resultExplicit[i]) > 1e-16:
            # print result[i] - resultExplicit[i]
            print("result:{}".format(result[i]))
            print("resultExplicit:{}".format(resultExplicit[i]))
예제 #11
0
def example3():
    ## Use Leja points unlike example 2 and use CombigridMultiOperation for evaluation at multiple
    ## points.
    operation = pysgpp.CombigridMultiOperation.createLinearLejaPolynomialInterpolation(
        d, func)

    ## We slightly deviate from the C++ example here and pass the interpolation points via a DataMatrix.
    ## We will use 2 interpolation points.
    ## IMPORTANT: For python, the parameters matrix needs to be transposed
    firstParam = [0.2, 0.6, 0.7]
    secondParam = [0.3, 0.9, 1.0]

    params = np.array([firstParam, secondParam])
    parameters = pysgpp.DataMatrix(params.transpose())
    print(parameters)

    ## Let's use the simple interface for this example and stop the time:
    stopwatch = pysgpp.Stopwatch()
    result = operation.evaluate(3, parameters)
    stopwatch.log()
    print("First result: " + str(result[0]) + ", function value: " +
          str(func(pysgpp.DataVector(firstParam))))
    print("Second result: " + str(result[1]) + ", function value: " +
          str(func(pysgpp.DataVector(secondParam))))
예제 #12
0
def generate_friedman1(seed):
    (X, y) = data.make_friedman1(n_samples=10000, random_state=seed, noise=1.0)
    y = sg.DataVector(y)
    X = sg.DataMatrix(X)
    return X, y
예제 #13
0
def main():
    # Generate data
    print("generate dataset... ", end=' ')
    data_tr,_ = generate_friedman1(123456)
    print("Done")
    print("generated a friedman1 dataset (10D) with 2000 samples")
    
    # Config grid
    print("create grid config... ", end=' ')
    grid = sg.RegularGridConfiguration()
    grid.dim_ = 10
    grid.level_ = 3
    grid.type_ = sg.GridType_Linear
    print("Done")

    # Config adaptivity
    print("create adaptive refinement config... ", end=' ')
    adapt = sg.AdaptivityConfiguration()
    adapt.numRefinements_ = 0
    adapt.noPoints_ = 10
    print("Done")
    
    # Config solver
    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")

    # Config regularization
    print("create regularization config... ", end=' ')
    regular = sg.RegularizationConfiguration()
    regular.regType_ = sg.RegularizationType_Laplace  
    print("Done")

    # Config cross validation for learner
    print("create learner config... ", end=' ')
    #crossValid = sg.CrossvalidationConfiguration()
    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")

    #
    # Create the learner with the given configuration
    #
    print("create the learner... ")
    learner = sg.LearnerSGDE(grid, adapt, solv, regular, crossValid)
    learner.initialize(data_tr)
    
    # Train the learner
    print("start training... ")
    learner.train()
    print("done training")
    
    #
    # Estimate the probability density function (pdf) via a Gaussian kernel 
    # density estimation (KDE) and print the corresponding values
    #
    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")
    
    # Print the covariances
    C = sg.DataMatrix(grid.dim_, grid.dim_)
    print("----------------------- Cov_SGDE -----------------------")
    learner.cov(C)
    print(C)
    print("----------------------- Cov_KDE -----------------------")
    kde.cov(C)
    print(C)
    
    #
    # Apply the inverse Rosenblatt transformatio to a matrix of random points. To 
    # do this, first generate the random points uniformly, then initialize an 
    # inverse Rosenblatt transformation operation and apply it to the points.
    # Finally print the calculated values
    #
    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)
    
    #
    # To check whether the results are correct perform a Rosenform transformation on
    # the data that has been created by the inverse Rosenblatt transformation above
    # and print the calculated values
    #
    points.setAll(0.0)
    opRos = sg.createOperationRosenblattTransformation(learner.getGrid())
    opRos.doTransformation(learner.getSurpluses(), pointsCdf, points)
    
    print("-----------------------------------------------")
    print(pointsCdf)
    print("-----------------------------------------------")
    print(points)
예제 #14
0
## \c DataVector. Alternatively, we could also apply sgpp::combigrid::OperationUPFullGrid with
## opPole to obtain the surpluses for this single full grid.
# select the second full grid of the combination grid (arbitrary choice)
fullGridIndex = 1
fullGrid = combiGrid.getFullGrids()[fullGridIndex]
l = fullGrid.getLevel()
print("Level of selected full grid with index {}: {}".format(
    fullGridIndex, np.array(l)))

# create operation for evaluating and evaluate
opEval = pysgpp.OperationEvalFullGrid(fullGrid)
y = opEval.eval(surpluses[fullGridIndex], xDv)
print("Value of full grid interpolant at {}: {:.6g}".format(np.array(x), y))

# compute grid points of full grid
X = pysgpp.DataMatrix(0, 0)
pysgpp.IndexVectorRange.getPoints(fullGrid, X)

# convert resulting sgpp::base::DataMatrix to NumPy array
X = np.array([[X.get(k, j) for j in range(X.getNcols())]
              for k in range(X.getNrows())])

# plot
plotFunction(opEval, surpluses[fullGridIndex], X)

if doPlot: plt.show()
else:
    print("Skipping plots due to failed import of Matplotlib.")

    ## The example program outputs the following results:
    ## \verbinclude combigrid.output.txt