How to combine plot of two GPy models?

1k Views Asked by At

I have calculated two GP regression models and would like to have them plotted in the same figure.

Model 1

kernel = GPy.kern.RBF(input_dim=1, variance=.1, lengthscale=1.)
m1 = GPy.models.GPRegression(xa, ya,kernel)
m1.optimize_restarts(num_restarts = 10)
m1.optimize(messages=True)

from IPython.display import display
display(m1)

fig1 = m1.plot(plot_density=True) 
m1.plot(plot_density=True)
GPy.plotting.show(fig2, filename='2')

Model 2

m2 = GPy.models.GPRegression(xe, ye,kernel)
m2.optimize_restarts(num_restarts = 10)
m2.optimize(messages=True)

from IPython.display import display
display(m2)

fig2 = m2.plot(plot_density=True,)

GPy.plotting.show(fig2, filename='2')

I want both plots in one figure, in either matplotlib or plotly i.e. GPy.plotting.show(fig, filename='filename').

Thanks

1

There are 1 best solutions below

0
On

Using matplotlib, you can define a subplot, and specify the subplot to be used using the same axes (specifically, param ax).

import matplotlib.plt as plt
fig, ax = plt.subplots()
m1.plot(plot_density=True, ax=ax)
m2.plot(plot_density=True, ax=ax)

I tested this out with a test data set:

# Random Test Data
import pods
data = pods.datasets.olympic_marathon_men()

# First X,Y Regression Model
kernel = GPy.kern.RBF(input_dim=1, variance=.1, lengthscale=1.)
m1 = GPy.models.GPRegression(data['X'], data['Y'], kernel)
m1.optimize_restarts(num_restarts = 10)
m1.optimize(messages=True)

# Second model; changed the X, Y slightly.
m2 = GPy.models.GPRegression(data['X'] + 5, data['Y'] + 3,kernel)
m2.optimize_restarts(num_restarts = 10)
m2.optimize(messages=True)

Related: What is the best way of combining two independent plots with matplotlib?

Plot of two densities w/data