mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-06-08 15:05:15 +02:00
[kernel] plot_ard added (some other fixes as well)
This commit is contained in:
parent
e3617942d4
commit
d2d8a62d2d
14 changed files with 371 additions and 337 deletions
|
|
@ -1,21 +1,21 @@
|
|||
#===============================================================================
|
||||
# Copyright (c) 2012-2015, GPy authors (see AUTHORS.txt).
|
||||
# All rights reserved.
|
||||
#
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
#
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright notice, this
|
||||
# list of conditions and the following disclaimer.
|
||||
#
|
||||
#
|
||||
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions and the following disclaimer in the documentation
|
||||
# and/or other materials provided with the distribution.
|
||||
#
|
||||
#
|
||||
# * Neither the name of GPy nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
|
|
@ -28,7 +28,8 @@
|
|||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#===============================================================================
|
||||
import numpy as np
|
||||
from . import pl
|
||||
from . import plotting_library as pl
|
||||
#from .. import gpy_plot
|
||||
from .plot_util import get_x_y_var, get_free_dims, get_which_data_ycols,\
|
||||
get_which_data_rows, update_not_existing_kwargs, helper_predict_with_model
|
||||
|
||||
|
|
@ -51,12 +52,12 @@ def plot_data(self, which_data_rows='all',
|
|||
:param {'2d','3d'} projection: whether to plot in 2d or 3d. This only applies when plotting two dimensional inputs!
|
||||
:param str label: the label for the plot
|
||||
:param kwargs plot_kwargs: kwargs for the data plot for the plotting library you are using
|
||||
|
||||
|
||||
:returns list: of plots created.
|
||||
"""
|
||||
canvas, plot_kwargs = pl.new_canvas(projection=projection, **plot_kwargs)
|
||||
canvas, plot_kwargs = pl().new_canvas(projection=projection, **plot_kwargs)
|
||||
plots = _plot_data(self, canvas, which_data_rows, which_data_ycols, visible_dims, projection, label, **plot_kwargs)
|
||||
return pl.add_to_canvas(canvas, plots)
|
||||
return pl().add_to_canvas(canvas, plots)
|
||||
|
||||
def _plot_data(self, canvas, which_data_rows='all',
|
||||
which_data_ycols='all', visible_dims=None,
|
||||
|
|
@ -66,26 +67,26 @@ def _plot_data(self, canvas, which_data_rows='all',
|
|||
|
||||
X, _, Y = get_x_y_var(self)
|
||||
free_dims = get_free_dims(self, visible_dims, None)
|
||||
|
||||
|
||||
plots = {}
|
||||
plots['dataplot'] = []
|
||||
|
||||
|
||||
#one dimensional plotting
|
||||
if len(free_dims) == 1:
|
||||
for d in ycols:
|
||||
update_not_existing_kwargs(plot_kwargs, pl.defaults.data_1d) # @UndefinedVariable
|
||||
plots['dataplot'].append(pl.scatter(canvas, X[rows, free_dims], Y[rows, d], label=label, **plot_kwargs))
|
||||
update_not_existing_kwargs(plot_kwargs, pl().defaults.data_1d) # @UndefinedVariable
|
||||
plots['dataplot'].append(pl().scatter(canvas, X[rows, free_dims], Y[rows, d], label=label, **plot_kwargs))
|
||||
#2D plotting
|
||||
elif len(free_dims) == 2:
|
||||
if projection=='2d':
|
||||
for d in ycols:
|
||||
update_not_existing_kwargs(plot_kwargs, pl.defaults.data_2d) # @UndefinedVariable
|
||||
plots['dataplot'].append(pl.scatter(canvas, X[rows, free_dims[0]], X[rows, free_dims[1]],
|
||||
update_not_existing_kwargs(plot_kwargs, pl().defaults.data_2d) # @UndefinedVariable
|
||||
plots['dataplot'].append(pl().scatter(canvas, X[rows, free_dims[0]], X[rows, free_dims[1]],
|
||||
color=Y[rows, d], label=label, **plot_kwargs))
|
||||
else:
|
||||
for d in ycols:
|
||||
update_not_existing_kwargs(plot_kwargs, pl.defaults.data_2d) # @UndefinedVariable
|
||||
plots['dataplot'].append(pl.scatter(canvas, X[rows, free_dims[0]], X[rows, free_dims[1]],
|
||||
update_not_existing_kwargs(plot_kwargs, pl().defaults.data_2d) # @UndefinedVariable
|
||||
plots['dataplot'].append(pl().scatter(canvas, X[rows, free_dims[0]], X[rows, free_dims[1]],
|
||||
Z=Y[rows, d], color=Y[rows, d], label=label, **plot_kwargs))
|
||||
elif len(free_dims) == 0:
|
||||
pass #Nothing to plot!
|
||||
|
|
@ -98,7 +99,7 @@ def plot_data_error(self, which_data_rows='all',
|
|||
projection='2d', label=None, **error_kwargs):
|
||||
"""
|
||||
Plot the training data input error.
|
||||
|
||||
|
||||
For higher dimensions than two, use fixed_inputs to plot the data points with some of the inputs fixed.
|
||||
|
||||
Can plot only part of the data
|
||||
|
|
@ -114,12 +115,12 @@ def plot_data_error(self, which_data_rows='all',
|
|||
:param dict error_kwargs: kwargs for the error plot for the plotting library you are using
|
||||
:param str label: the label for the plot
|
||||
:param kwargs plot_kwargs: kwargs for the data plot for the plotting library you are using
|
||||
|
||||
|
||||
:returns list: of plots created.
|
||||
"""
|
||||
canvas, error_kwargs = pl.new_canvas(projection=projection, **error_kwargs)
|
||||
canvas, error_kwargs = pl().new_canvas(projection=projection, **error_kwargs)
|
||||
plots = _plot_data_error(self, canvas, which_data_rows, which_data_ycols, visible_dims, projection, label, **error_kwargs)
|
||||
return pl.add_to_canvas(canvas, plots)
|
||||
return pl().add_to_canvas(canvas, plots)
|
||||
|
||||
def _plot_data_error(self, canvas, which_data_rows='all',
|
||||
which_data_ycols='all', visible_dims=None,
|
||||
|
|
@ -129,27 +130,27 @@ def _plot_data_error(self, canvas, which_data_rows='all',
|
|||
|
||||
X, X_variance, Y = get_x_y_var(self)
|
||||
free_dims = get_free_dims(self, visible_dims, None)
|
||||
|
||||
|
||||
plots = {}
|
||||
|
||||
if X_variance is not None:
|
||||
plots['xerrorplot'] = []
|
||||
|
||||
if X_variance is not None:
|
||||
plots['xerrorplot'] = []
|
||||
#one dimensional plotting
|
||||
if len(free_dims) == 1:
|
||||
for d in ycols:
|
||||
update_not_existing_kwargs(error_kwargs, pl.defaults.xerrorbar)
|
||||
plots['xerrorplot'].append(pl.xerrorbar(canvas, X[rows, free_dims].flatten(), Y[rows, d].flatten(),
|
||||
2 * np.sqrt(X_variance[rows, free_dims].flatten()), label=label,
|
||||
update_not_existing_kwargs(error_kwargs, pl().defaults.xerrorbar)
|
||||
plots['xerrorplot'].append(pl().xerrorbar(canvas, X[rows, free_dims].flatten(), Y[rows, d].flatten(),
|
||||
2 * np.sqrt(X_variance[rows, free_dims].flatten()), label=label,
|
||||
**error_kwargs))
|
||||
#2D plotting
|
||||
elif len(free_dims) == 2:
|
||||
update_not_existing_kwargs(error_kwargs, pl.defaults.xerrorbar) # @UndefinedVariable
|
||||
update_not_existing_kwargs(error_kwargs, pl().defaults.xerrorbar) # @UndefinedVariable
|
||||
for d in ycols:
|
||||
plots['xerrorplot'].append(pl.xerrorbar(canvas, X[rows, free_dims[0]].flatten(), Y[rows, d].flatten(),
|
||||
2 * np.sqrt(X_variance[rows, free_dims[0]].flatten()), label=label,
|
||||
plots['xerrorplot'].append(pl().xerrorbar(canvas, X[rows, free_dims[0]].flatten(), Y[rows, d].flatten(),
|
||||
2 * np.sqrt(X_variance[rows, free_dims[0]].flatten()), label=label,
|
||||
**error_kwargs))
|
||||
plots['yerrorplot'].append(pl.xerrorbar(canvas, X[rows, free_dims[1]].flatten(), Y[rows, d].flatten(),
|
||||
2 * np.sqrt(X_variance[rows, free_dims[1]].flatten()), label=label,
|
||||
plots['yerrorplot'].append(pl().xerrorbar(canvas, X[rows, free_dims[1]].flatten(), Y[rows, d].flatten(),
|
||||
2 * np.sqrt(X_variance[rows, free_dims[1]].flatten()), label=label,
|
||||
**error_kwargs))
|
||||
elif len(free_dims) == 0:
|
||||
pass #Nothing to plot!
|
||||
|
|
@ -161,13 +162,13 @@ def _plot_data_error(self, canvas, which_data_rows='all',
|
|||
def plot_inducing(self, visible_dims=None, projection='2d', label=None, **plot_kwargs):
|
||||
"""
|
||||
Plot the inducing inputs of a sparse gp model
|
||||
|
||||
|
||||
:param array-like visible_dims: an array specifying the input dimensions to plot (maximum two)
|
||||
:param kwargs plot_kwargs: keyword arguments for the plotting library
|
||||
"""
|
||||
canvas, kwargs = pl.new_canvas(projection=projection, **plot_kwargs)
|
||||
canvas, kwargs = pl().new_canvas(projection=projection, **plot_kwargs)
|
||||
plots = _plot_inducing(self, canvas, visible_dims, projection, label, **kwargs)
|
||||
return pl.add_to_canvas(canvas, plots)
|
||||
return pl().add_to_canvas(canvas, plots)
|
||||
|
||||
def _plot_inducing(self, canvas, visible_dims, projection, label, **plot_kwargs):
|
||||
if visible_dims is None:
|
||||
|
|
@ -180,15 +181,15 @@ def _plot_inducing(self, canvas, visible_dims, projection, label, **plot_kwargs)
|
|||
|
||||
#one dimensional plotting
|
||||
if len(free_dims) == 1:
|
||||
update_not_existing_kwargs(plot_kwargs, pl.defaults.inducing_1d) # @UndefinedVariable
|
||||
plots['inducing'] = pl.plot_axis_lines(canvas, Z[:, free_dims], **plot_kwargs)
|
||||
update_not_existing_kwargs(plot_kwargs, pl().defaults.inducing_1d) # @UndefinedVariable
|
||||
plots['inducing'] = pl().plot_axis_lines(canvas, Z[:, free_dims], **plot_kwargs)
|
||||
#2D plotting
|
||||
elif len(free_dims) == 2 and projection == '3d':
|
||||
update_not_existing_kwargs(plot_kwargs, pl.defaults.inducing_3d) # @UndefinedVariable
|
||||
plots['inducing'] = pl.plot_axis_lines(canvas, Z[:, free_dims], **plot_kwargs)
|
||||
update_not_existing_kwargs(plot_kwargs, pl().defaults.inducing_3d) # @UndefinedVariable
|
||||
plots['inducing'] = pl().plot_axis_lines(canvas, Z[:, free_dims], **plot_kwargs)
|
||||
elif len(free_dims) == 2:
|
||||
update_not_existing_kwargs(plot_kwargs, pl.defaults.inducing_2d) # @UndefinedVariable
|
||||
plots['inducing'] = pl.scatter(canvas, Z[:, free_dims[0]], Z[:, free_dims[1]],
|
||||
update_not_existing_kwargs(plot_kwargs, pl().defaults.inducing_2d) # @UndefinedVariable
|
||||
plots['inducing'] = pl().scatter(canvas, Z[:, free_dims[0]], Z[:, free_dims[1]],
|
||||
**plot_kwargs)
|
||||
elif len(free_dims) == 0:
|
||||
pass #Nothing to plot!
|
||||
|
|
@ -197,18 +198,18 @@ def _plot_inducing(self, canvas, visible_dims, projection, label, **plot_kwargs)
|
|||
return plots
|
||||
|
||||
def plot_errorbars_trainset(self, which_data_rows='all',
|
||||
which_data_ycols='all', fixed_inputs=None,
|
||||
which_data_ycols='all', fixed_inputs=None,
|
||||
plot_raw=False, apply_link=False, label=None, projection='2d',
|
||||
predict_kw=None, **plot_kwargs):
|
||||
"""
|
||||
Plot the errorbars of the GP likelihood on the training data.
|
||||
These are the errorbars after the appropriate
|
||||
These are the errorbars after the appropriate
|
||||
approximations according to the likelihood are done.
|
||||
|
||||
|
||||
This also works for heteroscedastic likelihoods.
|
||||
|
||||
|
||||
Give the Y_metadata in the predict_kw if you need it.
|
||||
|
||||
|
||||
:param which_data_rows: which of the training data to plot (default all)
|
||||
:type which_data_rows: 'all' or a slice object to slice self.X, self.Y
|
||||
:param which_data_ycols: when the data has several columns (independant outputs), only plot these
|
||||
|
|
@ -217,13 +218,13 @@ def plot_errorbars_trainset(self, which_data_rows='all',
|
|||
:param dict predict_kwargs: kwargs for the prediction used to predict the right quantiles.
|
||||
:param kwargs plot_kwargs: kwargs for the data plot for the plotting library you are using
|
||||
"""
|
||||
canvas, kwargs = pl.new_canvas(projection=projection, **plot_kwargs)
|
||||
plots = _plot_errorbars_trainset(self, canvas, which_data_rows, which_data_ycols,
|
||||
canvas, kwargs = pl().new_canvas(projection=projection, **plot_kwargs)
|
||||
plots = _plot_errorbars_trainset(self, canvas, which_data_rows, which_data_ycols,
|
||||
fixed_inputs, plot_raw, apply_link, label, projection, predict_kw, **kwargs)
|
||||
return pl.add_to_canvas(canvas, plots)
|
||||
return pl().add_to_canvas(canvas, plots)
|
||||
|
||||
def _plot_errorbars_trainset(self, canvas,
|
||||
which_data_rows='all', which_data_ycols='all',
|
||||
def _plot_errorbars_trainset(self, canvas,
|
||||
which_data_rows='all', which_data_ycols='all',
|
||||
fixed_inputs=None,
|
||||
plot_raw=False, apply_link=False,
|
||||
label=None, projection='2d', predict_kw=None, **plot_kwargs):
|
||||
|
|
@ -232,43 +233,43 @@ def _plot_errorbars_trainset(self, canvas,
|
|||
rows = get_which_data_rows(self, which_data_rows)
|
||||
|
||||
X, _, Y = get_x_y_var(self)
|
||||
|
||||
|
||||
if fixed_inputs is None:
|
||||
fixed_inputs = []
|
||||
free_dims = get_free_dims(self, None, fixed_inputs)
|
||||
free_dims = get_free_dims(self, None, fixed_inputs)
|
||||
|
||||
Xgrid = X.copy()
|
||||
for i, v in fixed_inputs:
|
||||
Xgrid[:, i] = v
|
||||
|
||||
plots = []
|
||||
|
||||
|
||||
if len(free_dims)<=2:
|
||||
update_not_existing_kwargs(plot_kwargs, pl.defaults.yerrorbar)
|
||||
update_not_existing_kwargs(plot_kwargs, pl().defaults.yerrorbar)
|
||||
if predict_kw is None:
|
||||
predict_kw = {}
|
||||
if 'Y_metadata' not in predict_kw:
|
||||
predict_kw['Y_metadata'] = self.Y_metadata or {}
|
||||
mu, percs, _ = helper_predict_with_model(self, Xgrid, plot_raw,
|
||||
apply_link, (2.5, 97.5),
|
||||
mu, percs, _ = helper_predict_with_model(self, Xgrid, plot_raw,
|
||||
apply_link, (2.5, 97.5),
|
||||
ycols, predict_kw)
|
||||
if len(free_dims)==1:
|
||||
for d in ycols:
|
||||
plots.append(pl.yerrorbar(canvas, X[rows,free_dims[0]], mu[rows,d],
|
||||
plots.append(pl().yerrorbar(canvas, X[rows,free_dims[0]], mu[rows,d],
|
||||
np.vstack([mu[rows, d] - percs[0][rows, d], percs[1][rows, d] - mu[rows,d]]),
|
||||
label=label,
|
||||
label=label,
|
||||
**plot_kwargs))
|
||||
elif len(free_dims) == 2:
|
||||
for d in ycols:
|
||||
plots.append(pl.yerrorbar(canvas, X[rows,free_dims[0]], X[rows,free_dims[1]],
|
||||
plots.append(pl().yerrorbar(canvas, X[rows,free_dims[0]], X[rows,free_dims[1]],
|
||||
np.vstack([mu[rows, d] - percs[0][rows, d], percs[1][rows, d] - mu[rows,d]]),
|
||||
color=Y[rows,d],
|
||||
label=label,
|
||||
label=label,
|
||||
**plot_kwargs))
|
||||
plots.append(pl.xerrorbar(canvas, X[rows,free_dims[0]], X[rows,free_dims[1]],
|
||||
plots.append(pl().xerrorbar(canvas, X[rows,free_dims[0]], X[rows,free_dims[1]],
|
||||
np.vstack([mu[rows, d] - percs[0][rows, d], percs[1][rows, d] - mu[rows,d]]),
|
||||
color=Y[rows,d],
|
||||
label=label,
|
||||
label=label,
|
||||
**plot_kwargs))
|
||||
pass #Nothing to plot!
|
||||
else:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue