2012-11-29 16:39:20 +00:00
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
# Licensed under the BSD 3-clause license (see LICENSE.txt)
2012-11-29 16:27:46 +00:00
"""
2013-02-01 16:21:26 +00:00
Gaussian Processes classification
2012-11-29 16:27:46 +00:00
"""
import pylab as pb
import numpy as np
import GPy
default_seed = 10000
2013-02-01 16:21:26 +00:00
2013-03-11 11:41:46 +00:00
def crescent_data ( seed = default_seed ) : #FIXME
2012-11-29 16:27:46 +00:00
""" Run a Gaussian process classification on the crescent data. The demonstration calls the basic GP classification model and uses EP to approximate the likelihood.
: param model_type : type of model to fit [ ' Full ' , ' FITC ' , ' DTC ' ] .
: param seed : seed value for data generation .
: type seed : int
: param inducing : number of inducing variables ( only used for ' FITC ' or ' DTC ' ) .
: type inducing : int
"""
2013-02-07 11:36:45 +00:00
2012-11-29 16:27:46 +00:00
data = GPy . util . datasets . crescent_data ( seed = seed )
2013-02-07 11:36:45 +00:00
# Kernel object
kernel = GPy . kern . rbf ( data [ ' X ' ] . shape [ 1 ] )
# Likelihood object
distribution = GPy . likelihoods . likelihood_functions . probit ( )
likelihood = GPy . likelihoods . EP ( data [ ' Y ' ] , distribution )
2012-11-29 16:27:46 +00:00
2013-03-11 11:41:46 +00:00
m = GPy . models . GP ( data [ ' X ' ] , likelihood , kernel )
m . ensure_default_constraints ( )
2012-11-29 16:27:46 +00:00
2013-02-01 16:21:26 +00:00
m . update_likelihood_approximation ( )
2012-11-29 16:27:46 +00:00
print ( m )
# optimize
2013-02-07 11:36:45 +00:00
m . optimize ( )
2012-11-29 16:27:46 +00:00
print ( m )
# plot
m . plot ( )
return m
def oil ( ) :
2013-02-01 16:21:26 +00:00
"""
Run a Gaussian process classification on the oil data . The demonstration calls the basic GP classification model and uses EP to approximate the likelihood .
"""
2012-11-29 16:27:46 +00:00
data = GPy . util . datasets . oil ( )
2013-02-01 16:21:26 +00:00
# Kernel object
kernel = GPy . kern . rbf ( 12 )
2012-11-29 16:27:46 +00:00
2013-02-01 16:21:26 +00:00
# Likelihood object
distribution = GPy . likelihoods . likelihood_functions . probit ( )
likelihood = GPy . likelihoods . EP ( data [ ' Y ' ] [ : , 0 : 1 ] , distribution )
2012-11-29 16:27:46 +00:00
2013-02-01 16:21:26 +00:00
# Create GP model
2013-02-07 11:36:45 +00:00
m = GPy . models . GP ( data [ ' X ' ] , likelihood = likelihood , kernel = kernel )
2013-02-01 16:21:26 +00:00
# Contrain all parameters to be positive
2012-11-29 16:27:46 +00:00
m . constrain_positive ( ' ' )
m . tie_param ( ' lengthscale ' )
2013-02-01 16:21:26 +00:00
m . update_likelihood_approximation ( )
2012-11-29 16:27:46 +00:00
2013-02-01 16:21:26 +00:00
# Optimize
2012-11-29 16:27:46 +00:00
m . optimize ( )
print ( m )
return m
2013-02-01 16:21:26 +00:00
def toy_linear_1d_classification ( seed = default_seed ) :
"""
Simple 1 D classification example
2012-11-29 16:27:46 +00:00
: param seed : seed value for data generation ( default is 4 ) .
: type seed : int
"""
2013-02-01 16:21:26 +00:00
2012-11-29 16:27:46 +00:00
data = GPy . util . datasets . toy_linear_1d_classification ( seed = seed )
2013-02-07 11:36:45 +00:00
Y = data [ ' Y ' ] [ : , 0 : 1 ]
Y [ Y == - 1 ] = 0
2012-11-29 16:27:46 +00:00
2013-02-01 16:21:26 +00:00
# Kernel object
kernel = GPy . kern . rbf ( 1 )
2012-11-29 16:27:46 +00:00
2013-02-01 16:21:26 +00:00
# Likelihood object
distribution = GPy . likelihoods . likelihood_functions . probit ( )
2013-02-07 11:36:45 +00:00
likelihood = GPy . likelihoods . EP ( Y , distribution )
2012-11-29 16:27:46 +00:00
2013-02-01 16:21:26 +00:00
# Model definition
2013-02-01 17:58:21 +00:00
m = GPy . models . GP ( data [ ' X ' ] , likelihood = likelihood , kernel = kernel )
2013-03-11 11:41:46 +00:00
m . ensure_default_constraints ( )
2012-11-29 16:27:46 +00:00
2013-02-01 16:21:26 +00:00
# Optimize
2013-03-11 11:41:46 +00:00
m . update_likelihood_approximation ( )
# Parameters optimization:
m . optimize ( )
#m.EPEM() #FIXME
2013-02-01 16:21:26 +00:00
# Plot
pb . subplot ( 211 )
2013-02-07 11:36:45 +00:00
m . plot_f ( )
2013-02-01 16:21:26 +00:00
pb . subplot ( 212 )
2013-02-01 17:58:21 +00:00
m . plot ( )
2012-11-29 16:27:46 +00:00
print ( m )
2013-02-01 16:21:26 +00:00
2012-11-29 16:27:46 +00:00
return m
2013-03-11 14:05:56 +00:00
def sparse_toy_linear_1d_classification ( seed = default_seed ) :
"""
Simple 1 D classification example
: param seed : seed value for data generation ( default is 4 ) .
: type seed : int
"""
data = GPy . util . datasets . toy_linear_1d_classification ( seed = seed )
Y = data [ ' Y ' ] [ : , 0 : 1 ]
Y [ Y == - 1 ] = 0
# Kernel object
kernel = GPy . kern . rbf ( 1 )
# Likelihood object
distribution = GPy . likelihoods . likelihood_functions . probit ( )
likelihood = GPy . likelihoods . EP ( Y , distribution )
Z = np . random . uniform ( data [ ' X ' ] . min ( ) , data [ ' X ' ] . max ( ) , ( 10 , 1 ) )
# Model definition
2013-03-11 14:26:26 +00:00
m = GPy . models . sparse_GP ( data [ ' X ' ] , likelihood = likelihood , kernel = kernel , Z = Z , normalize_X = True )
m . set ( ' len ' , .5 )
2013-03-11 14:05:56 +00:00
m . ensure_default_constraints ( )
# Optimize
m . update_likelihood_approximation ( )
# Parameters optimization:
m . optimize ( )
#m.EPEM() #FIXME
# Plot
pb . subplot ( 211 )
m . plot_f ( )
pb . subplot ( 212 )
m . plot ( )
print ( m )
return m
def sparse_crescent_data ( inducing = 10 , seed = default_seed ) :
""" Run a Gaussian process classification on the crescent data. The demonstration calls the basic GP classification model and uses EP to approximate the likelihood.
: param model_type : type of model to fit [ ' Full ' , ' FITC ' , ' DTC ' ] .
: param seed : seed value for data generation .
: type seed : int
: param inducing : number of inducing variables ( only used for ' FITC ' or ' DTC ' ) .
: type inducing : int
"""
data = GPy . util . datasets . crescent_data ( seed = seed )
# Kernel object
kernel = GPy . kern . rbf ( data [ ' X ' ] . shape [ 1 ] ) + GPy . kern . white ( data [ ' X ' ] . shape [ 1 ] )
# Likelihood object
distribution = GPy . likelihoods . likelihood_functions . probit ( )
likelihood = GPy . likelihoods . EP ( data [ ' Y ' ] , distribution )
sample = np . random . randint ( 0 , data [ ' X ' ] . shape [ 0 ] , inducing )
Z = data [ ' X ' ] [ sample , : ]
#Z = (np.random.random_sample(2*inducing)*(data['X'].max()-data['X'].min())+data['X'].min()).reshape(inducing,-1)
# create sparse GP EP model
m = GPy . models . sparse_GP ( data [ ' X ' ] , likelihood = likelihood , kernel = kernel , Z = Z )
m . ensure_default_constraints ( )
m . set ( ' len ' , 10. )
m . update_likelihood_approximation ( )
# optimize
m . optimize ( )
print ( m )
# plot
m . plot ( )
return m