plot_latent added for mrd

This commit is contained in:
Max Zwiessele 2013-04-11 16:00:14 +01:00
parent 89a50e260a
commit de6b00ebfd

View file

@ -24,7 +24,7 @@ class GPLVM(GP):
:type init: 'PCA'|'random' :type init: 'PCA'|'random'
""" """
def __init__(self, Y, Q, init='PCA', X=None, kernel=None, **kwargs): def __init__(self, Y, Q, init='PCA', X = None, kernel=None, **kwargs):
if X is None: if X is None:
X = self.initialise_latent(init, Q, Y) X = self.initialise_latent(init, Q, Y)
if kernel is None: if kernel is None:
@ -39,28 +39,28 @@ class GPLVM(GP):
return np.random.randn(Y.shape[0], Q) return np.random.randn(Y.shape[0], Q)
def _get_param_names(self): def _get_param_names(self):
return sum([['X_%i_%i' % (n, q) for q in range(self.Q)] for n in range(self.N)], []) + GP._get_param_names(self) return sum([['X_%i_%i'%(n,q) for q in range(self.Q)] for n in range(self.N)],[]) + GP._get_param_names(self)
def _get_params(self): def _get_params(self):
return np.hstack((self.X.flatten(), GP._get_params(self))) return np.hstack((self.X.flatten(), GP._get_params(self)))
def _set_params(self, x): def _set_params(self,x):
self.X = x[:self.X.size].reshape(self.N, self.Q).copy() self.X = x[:self.X.size].reshape(self.N,self.Q).copy()
GP._set_params(self, x[self.X.size:]) GP._set_params(self, x[self.X.size:])
def _log_likelihood_gradients(self): def _log_likelihood_gradients(self):
dL_dX = 2.*self.kern.dK_dX(self.dL_dK, self.X) dL_dX = 2.*self.kern.dK_dX(self.dL_dK,self.X)
return np.hstack((dL_dX.flatten(), GP._log_likelihood_gradients(self))) return np.hstack((dL_dX.flatten(),GP._log_likelihood_gradients(self)))
def plot(self): def plot(self):
assert self.likelihood.Y.shape[1] == 2 assert self.likelihood.Y.shape[1]==2
pb.scatter(self.likelihood.Y[:, 0], self.likelihood.Y[:, 1], 40, self.X[:, 0].copy(), linewidth=0, cmap=pb.cm.jet) pb.scatter(self.likelihood.Y[:,0],self.likelihood.Y[:,1],40,self.X[:,0].copy(),linewidth=0,cmap=pb.cm.jet)
Xnew = np.linspace(self.X.min(), self.X.max(), 200)[:, None] Xnew = np.linspace(self.X.min(),self.X.max(),200)[:,None]
mu, var, upper, lower = self.predict(Xnew) mu, var, upper, lower = self.predict(Xnew)
pb.plot(mu[:, 0], mu[:, 1], 'k', linewidth=1.5) pb.plot(mu[:,0], mu[:,1],'k',linewidth=1.5)
def plot_latent(self, labels=None, which_indices=None, resolution=50): def plot_latent(self,labels=None, which_indices=None, resolution=50):
""" """
:param labels: a np.array of size self.N containing labels for the points (can be number, strings, etc) :param labels: a np.array of size self.N containing labels for the points (can be number, strings, etc)
:param resolution: the resolution of the grid on which to evaluate the predictive variance :param resolution: the resolution of the grid on which to evaluate the predictive variance
@ -71,57 +71,54 @@ class GPLVM(GP):
if labels is None: if labels is None:
labels = np.ones(self.N) labels = np.ones(self.N)
if which_indices is None: if which_indices is None:
if self.Q == 1: if self.Q==1:
input_1 = 0 input_1 = 0
input_2 = None input_2 = None
if self.Q == 2: if self.Q==2:
input_1, input_2 = 0, 1 input_1, input_2 = 0,1
else: else:
# try to find a linear of RBF kern in the kernel try:
k = [p for p in self.kern.parts if p.name in ['rbf', 'linear']] input_1, input_2 = np.argsort(self.input_sensitivity())[:2]
if (not len(k) == 1) or (not k[0].ARD): except:
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'"
k = k[0] else:
if k.name == 'rbf': input_1, input_2 = which_indices
input_1, input_2 = np.argsort(k.lengthscale)[:2]
elif k.name == 'linear':
input_1, input_2 = np.argsort(k.variances)[::-1][:2]
# 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(self.X[:, [input_1, input_2]], resolution=resolution) Xtest, xx,yy,xmin,xmax = util.plot.x_frame2D(self.X[:,[input_1, input_2]],resolution=resolution)
Xtest_full = np.zeros((Xtest.shape[0], self.X.shape[1])) Xtest_full = np.zeros((Xtest.shape[0], self.X.shape[1]))
Xtest_full[:, :2] = Xtest Xtest_full[:, :2] = Xtest
mu, var, low, up = self.predict(Xtest_full) mu, var, low, up = self.predict(Xtest_full)
var = var[:, :1] var = var[:, :1]
pb.imshow(var.reshape(resolution, resolution).T[::-1, :], pb.imshow(var.reshape(resolution,resolution).T[::-1,:],
extent=[xmin[0], xmax[0], xmin[1], xmax[1]], cmap=pb.cm.binary, interpolation='bilinear') extent=[xmin[0], xmax[0], xmin[1], xmax[1]], cmap=pb.cm.binary,interpolation='bilinear')
for i, ul in enumerate(np.unique(labels)): for i,ul in enumerate(np.unique(labels)):
if type(ul) is np.string_: if type(ul) is np.string_:
this_label = ul this_label = ul
elif type(ul) is np.int64: elif type(ul) is np.int64:
this_label = 'class %i' % ul this_label = 'class %i'%ul
else: else:
this_label = 'class %i' % i this_label = 'class %i'%i
index = np.nonzero(labels == ul)[0] index = np.nonzero(labels==ul)[0]
if self.Q == 1: if self.Q==1:
x = self.X[index, input_1] x = self.X[index,input_1]
y = np.zeros(index.size) y = np.zeros(index.size)
else: else:
x = self.X[index, input_1] x = self.X[index,input_1]
y = self.X[index, input_2] y = self.X[index,input_2]
pb.plot(x, y, marker='o', color=util.plot.Tango.nextMedium(), mew=0, label=this_label, linewidth=0) pb.plot(x,y,marker='o',color=util.plot.Tango.nextMedium(),mew=0,label=this_label,linewidth=0)
pb.xlabel('latent dimension %i' % input_1) pb.xlabel('latent dimension %i'%input_1)
pb.ylabel('latent dimension %i' % input_2) pb.ylabel('latent dimension %i'%input_2)
if not np.all(labels == 1.): if not np.all(labels==1.):
pb.legend(loc=0, numpoints=1) pb.legend(loc=0,numpoints=1)
pb.xlim(xmin[0], xmax[0]) pb.xlim(xmin[0],xmax[0])
pb.ylim(xmin[1], xmax[1]) pb.ylim(xmin[1],xmax[1])
pb.grid(b=False) # remove the grid if present, it doesn't look good pb.grid(b=False) # remove the grid if present, it doesn't look good
ax = pb.gca() ax = pb.gca()
ax.set_aspect('auto') # set a nice aspect ratio ax.set_aspect('auto') # set a nice aspect ratio
return ax return ax