mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-24 14:15:14 +02:00
ard and latent plotting improved
This commit is contained in:
parent
e3af917b1b
commit
f469ce8f41
2 changed files with 50 additions and 29 deletions
|
|
@ -79,6 +79,8 @@ class kern(Parameterized):
|
||||||
if ax is None:
|
if ax is None:
|
||||||
fig = pb.figure(fignum)
|
fig = pb.figure(fignum)
|
||||||
ax = fig.add_subplot(111)
|
ax = fig.add_subplot(111)
|
||||||
|
else:
|
||||||
|
fig = ax.figure
|
||||||
from GPy.util import Tango
|
from GPy.util import Tango
|
||||||
from matplotlib.textpath import TextPath
|
from matplotlib.textpath import TextPath
|
||||||
Tango.reset()
|
Tango.reset()
|
||||||
|
|
@ -124,6 +126,7 @@ class kern(Parameterized):
|
||||||
# ax.set_xticklabels(xticklabels, rotation=17)
|
# ax.set_xticklabels(xticklabels, rotation=17)
|
||||||
ax.set_xticks([])
|
ax.set_xticks([])
|
||||||
ax.set_xlim(-.5, x0 - .5)
|
ax.set_xlim(-.5, x0 - .5)
|
||||||
|
if legend:
|
||||||
if title is '':
|
if title is '':
|
||||||
mode = 'expand'
|
mode = 'expand'
|
||||||
if len(bars) > 1:
|
if len(bars) > 1:
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,10 @@
|
||||||
import pylab as pb
|
import pylab as pb
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from .. import util
|
from .. import util
|
||||||
|
from GPy.util.latent_space_visualizations.controllers.imshow_controller import ImshowController
|
||||||
|
import itertools
|
||||||
|
|
||||||
def plot_latent(model, labels=None, which_indices=None, resolution=50, ax=None, marker='o', s=40, fignum=None, plot_inducing=False, legend=True):
|
def most_significant_input_dimensions(model, which_indices):
|
||||||
"""
|
|
||||||
:param labels: a np.array of size model.num_data containing labels for the points (can be number, strings, etc)
|
|
||||||
:param resolution: the resolution of the grid on which to evaluate the predictive variance
|
|
||||||
"""
|
|
||||||
if ax is None:
|
|
||||||
fig = pb.figure(num=fignum)
|
|
||||||
ax = fig.add_subplot(111)
|
|
||||||
util.plot.Tango.reset()
|
|
||||||
|
|
||||||
if labels is None:
|
|
||||||
labels = np.ones(model.num_data)
|
|
||||||
if which_indices is None:
|
if which_indices is None:
|
||||||
if model.input_dim == 1:
|
if model.input_dim == 1:
|
||||||
input_1 = 0
|
input_1 = 0
|
||||||
|
|
@ -27,15 +18,40 @@ def plot_latent(model, labels=None, which_indices=None, resolution=50, ax=None,
|
||||||
raise ValueError, "cannot Atomatically determine which dimensions to plot, please pass 'which_indices'"
|
raise ValueError, "cannot Atomatically determine which dimensions to plot, please pass 'which_indices'"
|
||||||
else:
|
else:
|
||||||
input_1, input_2 = which_indices
|
input_1, input_2 = which_indices
|
||||||
|
return input_1, input_2
|
||||||
|
|
||||||
|
def plot_latent(model, labels=None, which_indices=None,
|
||||||
|
resolution=50, ax=None, marker='o', s=40,
|
||||||
|
fignum=None, plot_inducing=False, legend=True,
|
||||||
|
aspect='auto', updates=False):
|
||||||
|
"""
|
||||||
|
:param labels: a np.array of size model.num_data containing labels for the points (can be number, strings, etc)
|
||||||
|
:param resolution: the resolution of the grid on which to evaluate the predictive variance
|
||||||
|
"""
|
||||||
|
if ax is None:
|
||||||
|
fig = pb.figure(num=fignum)
|
||||||
|
ax = fig.add_subplot(111)
|
||||||
|
util.plot.Tango.reset()
|
||||||
|
|
||||||
|
if labels is None:
|
||||||
|
labels = np.ones(model.num_data)
|
||||||
|
|
||||||
|
input_1, input_2 = most_significant_input_dimensions(model, which_indices)
|
||||||
|
|
||||||
# first, plot the output variance as a function of the latent space
|
# first, plot the output variance as a function of the latent space
|
||||||
Xtest, xx, yy, xmin, xmax = util.plot.x_frame2D(model.X[:, [input_1, input_2]], resolution=resolution)
|
Xtest, xx, yy, xmin, xmax = util.plot.x_frame2D(model.X[:, [input_1, input_2]], resolution=resolution)
|
||||||
Xtest_full = np.zeros((Xtest.shape[0], model.X.shape[1]))
|
Xtest_full = np.zeros((Xtest.shape[0], model.X.shape[1]))
|
||||||
Xtest_full[:, :2] = Xtest
|
def plot_function(x):
|
||||||
|
Xtest_full[:, [input_1, input_2]] = x
|
||||||
mu, var, low, up = model.predict(Xtest_full)
|
mu, var, low, up = model.predict(Xtest_full)
|
||||||
var = var[:, :1]
|
var = var[:, :1]
|
||||||
ax.imshow(var.reshape(resolution, resolution).T,
|
return var
|
||||||
extent=[xmin[0], xmax[0], xmin[1], xmax[1]], cmap=pb.cm.binary, interpolation='bilinear', origin='lower')
|
view = ImshowController(ax, plot_function, tuple(xmin) + tuple(xmax),
|
||||||
|
resolution, aspect=aspect, interpolation='bilinear',
|
||||||
|
cmap=pb.cm.binary)
|
||||||
|
|
||||||
|
# ax.imshow(var.reshape(resolution, resolution).T,
|
||||||
|
# extent=[xmin[0], xmax[0], xmin[1], xmax[1]], cmap=pb.cm.binary, interpolation='bilinear', origin='lower')
|
||||||
|
|
||||||
# make sure labels are in order of input:
|
# make sure labels are in order of input:
|
||||||
ulabels = []
|
ulabels = []
|
||||||
|
|
@ -43,6 +59,8 @@ def plot_latent(model, labels=None, which_indices=None, resolution=50, ax=None,
|
||||||
if not lab in ulabels:
|
if not lab in ulabels:
|
||||||
ulabels.append(lab)
|
ulabels.append(lab)
|
||||||
|
|
||||||
|
marker = itertools.cycle(list(marker))
|
||||||
|
|
||||||
for i, ul in enumerate(ulabels):
|
for i, ul in enumerate(ulabels):
|
||||||
if type(ul) is np.string_:
|
if type(ul) is np.string_:
|
||||||
this_label = ul
|
this_label = ul
|
||||||
|
|
@ -50,10 +68,7 @@ def plot_latent(model, labels=None, which_indices=None, resolution=50, ax=None,
|
||||||
this_label = 'class %i' % ul
|
this_label = 'class %i' % ul
|
||||||
else:
|
else:
|
||||||
this_label = 'class %i' % i
|
this_label = 'class %i' % i
|
||||||
if len(marker) == len(ulabels):
|
m = marker.next()
|
||||||
m = marker[i]
|
|
||||||
else:
|
|
||||||
m = marker
|
|
||||||
|
|
||||||
index = np.nonzero(labels == ul)[0]
|
index = np.nonzero(labels == ul)[0]
|
||||||
if model.input_dim == 1:
|
if model.input_dim == 1:
|
||||||
|
|
@ -78,4 +93,7 @@ def plot_latent(model, labels=None, which_indices=None, resolution=50, ax=None,
|
||||||
if plot_inducing:
|
if plot_inducing:
|
||||||
ax.plot(model.Z[:, input_1], model.Z[:, input_2], '^w')
|
ax.plot(model.Z[:, input_1], model.Z[:, input_2], '^w')
|
||||||
|
|
||||||
|
if updates:
|
||||||
|
ax.figure.canvas.show()
|
||||||
|
raw_input('Enter to continue')
|
||||||
return ax
|
return ax
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue