diff --git a/GPy/examples/classification.py b/GPy/examples/classification.py index 7645964d..c25ea124 100644 --- a/GPy/examples/classification.py +++ b/GPy/examples/classification.py @@ -84,7 +84,7 @@ def toy_linear_1d_classification(seed=default_seed): likelihood = GPy.likelihoods.EP(data['Y'][:, 0:1],distribution) # Model definition - m = GPy.models.GP(data['X'],kernel,likelihood=likelihood) + m = GPy.models.GP(data['X'],likelihood=likelihood,kernel=kernel) # Optimize """ @@ -98,9 +98,9 @@ def toy_linear_1d_classification(seed=default_seed): # Plot pb.subplot(211) - m.plot_GP() + m.plot_internal() pb.subplot(212) - m.plot_output() + m.plot() print(m) return m diff --git a/GPy/likelihoods/Gaussian.py b/GPy/likelihoods/Gaussian.py index 94cb0560..ff358b2d 100644 --- a/GPy/likelihoods/Gaussian.py +++ b/GPy/likelihoods/Gaussian.py @@ -42,7 +42,7 @@ class Gaussian(likelihood): """ mean = mu*self._std + self._mean true_var = (var + self._variance)*self._std**2 - _5pc = mean + mean - 2.*np.sqrt(var) + _5pc = mean + - 2.*np.sqrt(var) _95pc = mean + 2.*np.sqrt(var) return mean, _5pc, _95pc diff --git a/GPy/likelihoods/likelihood_functions.py b/GPy/likelihoods/likelihood_functions.py index 4f571e14..de97824a 100644 --- a/GPy/likelihoods/likelihood_functions.py +++ b/GPy/likelihoods/likelihood_functions.py @@ -52,8 +52,8 @@ class probit(likelihood_function): mu = mu.flatten() var = var.flatten() mean = stats.norm.cdf(mu/np.sqrt(1+var)) - p_05 = np.zeros([mu.size]) - p_95 = np.ones([mu.size]) + p_05 = np.zeros(mu.shape)#np.zeros([mu.size]) + p_95 = np.zeros(mu.shape)#np.ones([mu.size]) return mean, p_05, p_95 class Poisson(likelihood_function): diff --git a/GPy/models/GP.py b/GPy/models/GP.py index db00755c..c640e529 100644 --- a/GPy/models/GP.py +++ b/GPy/models/GP.py @@ -7,7 +7,7 @@ import pylab as pb from .. import kern from ..core import model from ..util.linalg import pdinv,mdot -from ..util.plot import gpplot,x_frame, Tango +from ..util.plot import gpplot,x_frame1D,x_frame2D, Tango from ..likelihoods import EP class GP(model): @@ -175,7 +175,7 @@ class GP(model): return mean, _5pc, _95pc - def plot_GP(self,samples=0,plot_limits=None,which_data='all',which_functions='all',resolution=None,full_cov=False): + def plot_internal(self,samples=0,plot_limits=None,which_data='all',which_functions='all',resolution=None,full_cov=False): """ Plot the GP's view of the world, where the data is normalised and the likelihood is Gaussian @@ -200,22 +200,49 @@ class GP(model): if which_data=='all': which_data = slice(None) - Xnew, xmin, xmax = x_frame(self.X, plot_limits=plot_limits) + if self.X.shape[1] == 1: + Xnew, xmin, xmax = x_frame1D(self.X, plot_limits=plot_limits) + m,v = self._raw_predict(Xnew, slices=which_functions) + gpplot(Xnew,m,m-np.sqrt(v),m+np.sqrt(v)) + pb.plot(self.X[which_data],self.likelihood.Y[which_data],'kx',mew=1.5) + pb.xlim(xmin,xmax) + elif X.shape[1]==2: + resolution = resolution or 50 + Xnew, xmin, xmax,xx,yy = x_frame2D(self.X, plot_limits=plot_limits) + m,v = self._raw_predict(Xnew, slices=which_functions) + m = m.reshape(resolution,resolution) + pb.contour(xx,yy,zz,vmin=zz.min(),vmax=zz.max(),cmap=pb.cm.jet) + pb.scatter(Xorig[:,0],Xorig[:,1],40,Yorig,linewidth=0,cmap=pb.cm.jet,vmin=zz.min(),vmax=zz.max()) + pb.xlim(xmin[0],xmax[0]) + pb.ylim(xmin[1],xmax[1]) + else: + raise NotImplementedError, "Cannot define a frame with more than two input dimensions" - m,v = self._raw_predict(Xnew, slices=which_functions) - gpplot(Xnew,m,m-np.sqrt(v),m+np.sqrt(v)) - pb.plot(self.X[which_data],self.likelihood.Y[which_data],'kx',mew=1.5) - pb.xlim(xmin,xmax) - - def plot_output(self,samples=0,plot_limits=None,which_data='all',which_functions='all',resolution=None,full_cov=False): + def plot(self,samples=0,plot_limits=None,which_data='all',which_functions='all',resolution=None,full_cov=False): if which_functions=='all': which_functions = [True]*self.kern.Nparts if which_data=='all': which_data = slice(None) - Xnew, xmin, xmax = x_frame(self.X, plot_limits=plot_limits) - m, lower, upper = self.predict(Xnew, slices=which_functions) - gpplot(Xnew,m, lower, upper) - pb.plot(self.X[which_data],self.likelihood.data[which_data],'kx',mew=1.5) - ymin,ymax = self.likelihood.data.min()*1.2,self.likelihood.data.max()*1.2 - pb.xlim(xmin,xmax) - pb.ylim(ymin,ymax) + + if self.X.shape[1] == 1: + Xnew, xmin, xmax = x_frame1D(self.X, plot_limits=plot_limits) + m, lower, upper = self.predict(Xnew, slices=which_functions) + gpplot(Xnew,m, lower, upper) + pb.plot(self.X[which_data],self.likelihood.data[which_data],'kx',mew=1.5) + ymin,ymax = self.likelihood.data.min()*1.2,self.likelihood.data.max()*1.2 + pb.xlim(xmin,xmax) + pb.ylim(ymin,ymax) + elif X.shape[1]==2: + resolution = resolution or 50 + Xnew, xmin, xmax,xx,yy = x_frame2D(self.X, plot_limits=plot_limits) + m,v = self.predict(Xnew, slices=which_functions) + m = m.reshape(resolution,resolution) + pb.contour(xx,yy,zz,vmin=zz.min(),vmax=zz.max(),cmap=pb.cm.jet) + pb.scatter(Xorig[:,0],Xorig[:,1],40,Yorig,linewidth=0,cmap=pb.cm.jet,vmin=zz.min(),vmax=zz.max()) + pb.xlim(xmin[0],xmax[0]) + pb.ylim(xmin[1],xmax[1]) + else: + raise NotImplementedError, "Cannot define a frame with more than two input dimensions" + + + diff --git a/GPy/util/plot.py b/GPy/util/plot.py index 60e3e488..7b346330 100644 --- a/GPy/util/plot.py +++ b/GPy/util/plot.py @@ -70,10 +70,11 @@ def align_subplots(N,M,xlim=None, ylim=None): else: removeUpperTicks() -def x_frame(X,plot_limits=None,resolution=None): +def x_frame1D(X,plot_limits=None,resolution=None): """ Internal helper function for making plots, returns a set of input values to plot as well as lower and upper limits """ + assert X.shape[1] ==1, "x_frame1D is defined for one-dimensional inputs" if plot_limits is None: xmin,xmax = X.min(0),X.max(0) xmin, xmax = xmin-0.2*(xmax-xmin), xmax+0.2*(xmax-xmin) @@ -82,12 +83,24 @@ def x_frame(X,plot_limits=None,resolution=None): else: raise ValueError, "Bad limits for plotting" - if X.shape[1]==1: - Xnew = np.linspace(xmin,xmax,resolution or 200)[:,None] - elif X.shape[1]==2: - resolution = resolution or 50 - xx,yy = np.mgrid[xmin[0]:xmax[0]:1j*resolution,xmin[1]:xmax[1]:1j*resolution] - Xnew = np.vstack((xx.flatten(),yy.flatten())).T - else: - raise NotImplementedError, "Cannot define a frame with more than two input dimensions" + Xnew = np.linspace(xmin,xmax,resolution or 200)[:,None] return Xnew, xmin, xmax + +def x_frame2D(X,plot_limits=None,resolution=None): + """ + Internal helper function for making plots, returns a set of input values to plot as well as lower and upper limits + """ + assert X.shape[1] ==2, "x_frame2D is defined for two-dimensional inputs" + if plot_limits is None: + xmin,xmax = X.min(0),X.max(0) + xmin, xmax = xmin-0.2*(xmax-xmin), xmax+0.2*(xmax-xmin) + elif len(plot_limits)==2: + xmin, xmax = plot_limits + else: + raise ValueError, "Bad limits for plotting" + + resolution = resolution or 50 + xx,yy = np.mgrid[xmin[0]:xmax[0]:1j*resolution,xmin[1]:xmax[1]:1j*resolution] + Xnew = np.vstack((xx.flatten(),yy.flatten())).T + return Xnew, xx,yy,xmin, xmax +