StackGP.printGPModel#
StackGP.printGPModel(model,inputData=symbols([“x”+str(i) for i in range(100)]))
printGPModel is a StackGP function that evaluates a GP model with the supplied data.
The function expects 1 arguments: model
There is 1 optional argument: inputData
The arguments are described below:
model: A StackGP model.
inputData: a list of SymPy symbols to use as the variable names.
First we need to load in the necessary packages
import StackGP as sgp
import numpy as np
Overview#
Display a model#
Here we generate a random model with up to 4 variables, the default operator set, the default constant set, and a maxSize of 10.
randomModel=sgp.generateRandomModel(4, sgp.defaultOps(), sgp.defaultConst(), 10)
We can display the random model below
sgp.printGPModel(randomModel)
Options#
This section showcases how each of the different option settings can be used with the printGPModel function.
inputData#
inputData can be used to change the symbols used when displaying the model. The default is to use x1, x2, …, x100
First lets generate some random data to fit a model to.
#Define a challenging function to generate data
def demoFunc(x,y):
return np.sin(x) + y
#Generate data
inputData=np.array([np.random.randint(1,10,10),np.random.randint(1,10,10)])
response=demoFunc(inputData[0],inputData[1])
Now lets try fitting with the default settings. We will turn on tracking so we can see the progress.
#Generate models
models=sgp.evolve(inputData,response,liveTracking=True, generations=100, popSize=100, ops=sgp.allOps())
Now we can display the best model evolved.
#View best model
sgp.printGPModel(models[0])
Now maybe we don’t want x and y as the variable names and instead want something like var1 and var2. We can do this using the inputData argument like below.
from sympy import symbols
sgp.printGPModel(models[0], inputData=symbols("var1,var2"))
Examples#
This section showcases how each of the different arguments can be used with the printGPModel function.
Checking the form of a random model using specific variable names#
randomModel = sgp.generateRandomModel(4, sgp.defaultOps(), sgp.defaultConst(), 10)
sgp.printGPModel(randomModel, inputData=symbols("temperature , pressure, humidity, windSpeed"))
Checking the forms of models from an evolved Pareto front#
First we generate some random data and evolve a model population.
inputData, responseData, model = sgp.generateRandomBenchmark()
models = sgp.evolve(inputData,responseData,liveTracking=True, generations=100, popSize=100, ops=sgp.allOps())
Now we can grab the Pareto front of the models
pFront = sgp.selectModels(models, selectionSize=2)
for i, model in zip(range(len(pFront)), pFront):
print(i, sgp.printGPModel(model, inputData=symbols("position, velocity, acceleration, time, mass")))
0 -3.5527136788005e-16 + 1.0/velocity
1 7.35939390871629 - 8.29633610621588*velocity