diff --git a/GPy/kern/_src/kern.py b/GPy/kern/_src/kern.py index 049d2814..453f5c2e 100644 --- a/GPy/kern/_src/kern.py +++ b/GPy/kern/_src/kern.py @@ -217,6 +217,12 @@ class Kern(Parameterized): """ Determine which dimensions should be plotted + Returns the top three most signification input dimensions + + if less then three dimensions, the non existing dimensions are + labeled as None, so for a 1 dimensional input this returns + (0, None, None). + :param which_indices: force the indices to be the given indices. :type which_indices: int or tuple(int,int) """ @@ -224,23 +230,32 @@ class Kern(Parameterized): if self.input_dim == 1: input_1 = 0 input_2 = None + input_3 = None if self.input_dim == 2: input_1, input_2 = 0, 1 + input_3 = None + if self.input_dim == 3: + input_1, input_2, input_3 = 0, 1, 2 else: try: - which_indices = np.argsort(self.input_sensitivity())[::-1][:2] + which_indices = np.argsort(self.input_sensitivity())[::-1][:3] except: raise ValueError("cannot automatically determine which dimensions to plot, please pass 'which_indices'") try: - input_1, input_2 = which_indices + input_1, input_2, input_3 = which_indices except TypeError: - # which_indices was an int - input_1, input_2 = which_indices, None - except ValueError: - # which_indices was a list or array like with only one int - input_1, input_2 = which_indices[0], None + # which indices is tuple or int + try: + input_3 = None + input_1, input_2 = which_indices + except TypeError: + # which_indices is an int + input_1, input_2 = which_indices, None + except ValueError: + # which_indices was a list or array like with only one int + input_1, input_2 = which_indices[0], None - return input_1, input_2 + return input_1, input_2, input_3 def __add__(self, other): diff --git a/GPy/plotting/__init__.py b/GPy/plotting/__init__.py index cd2ae31c..7de05473 100644 --- a/GPy/plotting/__init__.py +++ b/GPy/plotting/__init__.py @@ -59,13 +59,4 @@ if config.get('plotting', 'library') is not 'none': Kern.plot_covariance = gpy_plot.kernel_plots.plot_covariance Kern.plot_covariance = gpy_plot.kernel_plots.plot_ARD - # Variational plot! - - - #from . import matplot_dep - # Still to convert to new style: - #GP.plot = matplot_dep.models_plots.plot_fit - #GP.plot_f = matplot_dep.models_plots.plot_fit_f - - #GP.plot_magnification = matplot_dep.dim_reduction_plots.plot_magnification - + # Variational plot! diff --git a/GPy/plotting/gpy_plot/data_plots.py b/GPy/plotting/gpy_plot/data_plots.py index 82d4c963..0cfb4006 100644 --- a/GPy/plotting/gpy_plot/data_plots.py +++ b/GPy/plotting/gpy_plot/data_plots.py @@ -170,6 +170,9 @@ def plot_inducing(self, visible_dims=None, projection='2d', label=None, **plot_k return pl.show_canvas(canvas, plots) def _plot_inducing(self, canvas, visible_dims, projection, label, **plot_kwargs): + if visible_dims is None: + sig_dims = self.get_most_significant_input_dimensions() + visible_dims = [i for i in sig_dims if i is not None] free_dims = get_free_dims(self, visible_dims, None) Z = self.Z[:, free_dims] diff --git a/GPy/plotting/gpy_plot/latent_plots.py b/GPy/plotting/gpy_plot/latent_plots.py index 8f4f8949..fd2bba20 100644 --- a/GPy/plotting/gpy_plot/latent_plots.py +++ b/GPy/plotting/gpy_plot/latent_plots.py @@ -116,16 +116,16 @@ def _plot_prediction_fit(self, canvas, plot_limits=None, raise NotImplementedError("Cannot plot in more then one dimension.") return plots -def _plot_latent_scatter(self, canvas, X, input_1, input_2, labels, marker, num_samples, **kwargs): +def _plot_latent_scatter(self, canvas, X, visible_dims, labels, marker, num_samples, projection='2d', **kwargs): from .. import Tango Tango.reset() if labels is None: labels = np.ones(self.num_data) X, labels = subsample_X(X, labels, num_samples) scatters = [] - for x, y, this_label, _, m in scatter_label_generator(labels, X, input_1, input_2, marker): + for x, y, z, this_label, _, m in scatter_label_generator(labels, X, visible_dims, marker): update_not_existing_kwargs(kwargs, pl.defaults.latent_scatter) - scatters.append(pl.scatter(canvas, x, y, marker=m, color=Tango.nextMedium(), label=this_label, **kwargs)) + scatters.append(pl.scatter(canvas, x, y, Z=z, marker=m, color=Tango.nextMedium(), label=this_label, **kwargs)) return scatters def plot_latent_scatter(self, labels=None, @@ -134,6 +134,7 @@ def plot_latent_scatter(self, labels=None, plot_limits=None, marker='<>^vsd', num_samples=1000, + projection='2d', **kwargs): """ Plot a scatter plot of the latent space. @@ -146,13 +147,23 @@ def plot_latent_scatter(self, labels=None, :param str marker: markers to use - cycle if more labels then markers are given :param kwargs: the kwargs for the scatter plots """ - input_1, input_2 = self.get_most_significant_input_dimensions(which_indices) - canvas, kwargs = pl.get_new_canvas(xlabel='latent dimension %i' % input_1, ylabel='latent dimension %i' % input_2, **kwargs) + sig_dims = self.get_most_significant_input_dimensions(which_indices) + input_1, input_2, input_3 = [i for i in sig_dims if i is not None] + + canvas, kwargs = pl.get_new_canvas(projection=projection, **kwargs) X, _, _ = get_x_y_var(self) - scatters = _plot_latent_scatter(self, canvas, X, input_1, input_2, labels, marker, num_samples, **kwargs) - return pl.show_canvas(canvas, dict(scatter=scatters), legend=legend and (labels is not None)) - - + scatters = _plot_latent_scatter(self, canvas, X, sig_dims, labels, marker, num_samples, projection=projection, **kwargs) + if projection == '3d': + return pl.show_canvas(canvas, dict(scatter=scatters), legend=legend and (labels is not None), + xlabel='latent dimension %i' % input_1, + ylabel='latent dimension %i' % input_2, + zlabel='latent dimension %i' % input_3) + else: + return pl.show_canvas(canvas, dict(scatter=scatters), legend=legend and (labels is not None), + xlabel='latent dimension %i' % input_1, + ylabel='latent dimension %i' % input_2, + #zlabel='latent dimension %i' % input_3 + ) def _plot_magnification(self, canvas, input_1, input_2, Xgrid, diff --git a/GPy/plotting/gpy_plot/plot_util.py b/GPy/plotting/gpy_plot/plot_util.py index fd1255b8..f99ee5e6 100644 --- a/GPy/plotting/gpy_plot/plot_util.py +++ b/GPy/plotting/gpy_plot/plot_util.py @@ -129,7 +129,7 @@ def helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resoluti Xgrid[:,i] = v return X, Xvar, Y, fixed_dims, free_dims, Xgrid, x, y, xmin, xmax, resolution -def scatter_label_generator(labels, X, input_1, input_2=None, marker=None): +def scatter_label_generator(labels, X, visible_dims, marker=None): ulabels = [] for lab in labels: if not lab in ulabels: @@ -140,6 +140,8 @@ def scatter_label_generator(labels, X, input_1, input_2=None, marker=None): else: m = None + input_1, input_2, input_3 = visible_dims + for ul in ulabels: if type(ul) is np.string_: this_label = ul @@ -160,10 +162,16 @@ def scatter_label_generator(labels, X, input_1, input_2=None, marker=None): if input_2 is None: x = X[index, input_1] y = np.zeros(index.size) - else: + z = None + elif input_3 is None: x = X[index, input_1] y = X[index, input_2] - yield x, y, this_label, index, m + z = None + else: + x = X[index, input_1] + y = X[index, input_2] + z = X[index, input_3] + yield x, y, z, this_label, index, m def subsample_X(X, labels, num_samples=1000): """ @@ -175,7 +183,7 @@ def subsample_X(X, labels, num_samples=1000): print("Warning: subsampling X, as it has more samples then 1000. X.shape={!s}".format(X.shape)) if labels is not None: subsample = [] - for _, _, _, index, _ in scatter_label_generator(labels, X, 0): + for _, _, _, _, index, _ in scatter_label_generator(labels, X, (0, None, None)): subsample.append(np.random.choice(index, size=max(2, int(index.size*(float(num_samples)/X.shape[0]))), replace=False)) subsample = np.hstack(subsample) else: diff --git a/GPy/plotting/matplot_dep/plot_definitions.py b/GPy/plotting/matplot_dep/plot_definitions.py index afade4ec..129bdf66 100644 --- a/GPy/plotting/matplot_dep/plot_definitions.py +++ b/GPy/plotting/matplot_dep/plot_definitions.py @@ -82,7 +82,9 @@ class MatplotlibPlots(AbstractPlottingLibrary): return ax.scatter(X, Y, c=color, zs=Z, label=label, marker=marker, **kwargs) return ax.scatter(X, Y, c=color, label=label, marker=marker, **kwargs) - def plot(self, ax, X, Y, color=None, label=None, **kwargs): + def plot(self, ax, X, Y, Z=None, color=None, label=None, **kwargs): + if Z is not None: + return ax.plot(X, Y, color=color, zs=Z, label=label, **kwargs) return ax.plot(X, Y, color=color, label=label, **kwargs) def plot_axis_lines(self, ax, X, color=Tango.colorsHex['mediumBlue'], label=None, **kwargs):