SG++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
learnerExample.py

This example can be found under datadriven/examples/learnerExample.py.

1 from pysgpp.extensions.datadriven.learner import LearnerBuilder
2 import numpy as np
3 import matplotlib.pyplot as plt
4 from pysgpp.extensions.datadriven.uq.plot.plot1d import plotSG1d
5 from pysgpp.extensions.datadriven.uq.plot.plot2d import plotSG2d
6 
7 numSamples = 100
8 numDims = 2
9 
10 def f(x):
11  """
12  normal parabola
13  """
14  return np.prod(4. * x * (1 - x), axis=1)
15 
16 
17 print "generate uniformly distributed samples (%i, %i)" % (numSamples, numDims)
18 samples = np.random.rand(numSamples, numDims)
19 values = f(samples)
20 
21 builder = LearnerBuilder()
22 builder.buildRegressor()
23 builder.withTrainingDataFromNumPyArray(samples, values)
24 builder = builder.withGrid()
25 builder.withLevel(5)
26 builder = builder.withSpecification()
27 builder.withLambda(0.0001)
28 # does not seem to work!!
29 # builder.withLaplaceOperator()
30 builder.withIdentityOperator()
31 builder = builder.withStopPolicy()
32 builder = builder.withCGSolver()
33 builder.withAccuracy(0.000100)
34 builder.withImax(500)
35 
36 # # Do the learning
37 learner = builder.andGetResult()
38 
39 gs = learner.grid.getStorage()
40 
41 print "Dimensions: %i" % gs.dim()
42 print "Grid points: %i" % gs.size()
43 
44 print "================== Starting learning =================="
45 
46 learner.learnData()
47 print learner.alpha
48 
49 print "======================================================="
50 
51 if numDims == 1:
52  plt.scatter(samples[:, 0], values)
53  plotSG1d(learner.grid, learner.alpha, color="red")
54 else:
55  plotSG2d(learner.grid, learner.alpha)
56  plt.scatter(samples[:, 0], samples[:, 1])
57 plt.show()
58