(self, X, y, do_optimize=True)
First optimized the hyperparameters if do_optimize is True and then computes
        the posterior distribution of the weights. See chapter 3.3 of the book by Bishop
        for more details.

        Parameters
        ----------
        X: np.ndarray (N, D)
            Input data points. The dimensionality of X is (N, D),
            with N as the number of points and D is the number of features.
        y: np.ndarray (N,)
            The corresponding target values.
        do_optimize: boolean
            If set to true the hyperparameters are optimized otherwise
            the default hyperparameters are used.
First optimized the hyperparameters if do_optimize is True and then computes
        the posterior distribution of the weights. See chapter 3.3 of the book by Bishop
        for more details.
First optimized the hyperparameters if do_optimize is True and then computes the posterior distribution of the weights . See chapter 3 . 3 of the book by Bishop for more details .
def train(self, X, y, do_optimize=True):
        """
        First optimized the hyperparameters if do_optimize is True and then computes
        the posterior distribution of the weights. See chapter 3.3 of the book by Bishop
        for more details.

        Parameters
        ----------
        X: np.ndarray (N, D)
            Input data points. The dimensionality of X is (N, D),
            with N as the number of points and D is the number of features.
        y: np.ndarray (N,)
            The corresponding target values.
        do_optimize: boolean
            If set to true the hyperparameters are optimized otherwise
            the default hyperparameters are used.
        """

        self.X = X

        if self.basis_func is not None:
            self.X_transformed = self.basis_func(X)
        else:
            self.X_transformed = self.X

        self.y = y

        if do_optimize:
            if self.do_mcmc:
                sampler = emcee.EnsembleSampler(self.n_hypers, 2,
                                                self.marginal_log_likelihood)

                # Do a burn-in in the first iteration
                if not self.burned:
                    # Initialize the walkers by sampling from the prior
                    self.p0 = self.prior.sample_from_prior(self.n_hypers)

                    # Run MCMC sampling
                    self.p0, _, _ = sampler.run_mcmc(self.p0,
                                                     self.burnin_steps,
                                                     rstate0=self.rng)
    
                    self.burned = True
    
                # Start sampling
                pos, _, _ = sampler.run_mcmc(self.p0,
                                             self.chain_length,
                                             rstate0=self.rng)
    
                # Save the current position, it will be the start point in
                # the next iteration
                self.p0 = pos
    
                # Take the last samples from each walker
                self.hypers = np.exp(sampler.chain[:, -1])
            else:
                # Optimize hyperparameters of the Bayesian linear regression        
                res = optimize.fmin(self.negative_mll, self.rng.rand(2))
                self.hypers = [[np.exp(res[0]), np.exp(res[1])]]
                
        else:
            self.hypers = [[self.alpha, self.beta]]
        
        self.models = []
        for sample in self.hypers:
            alpha = sample[0]
            beta = sample[1]

            logger.debug("Alpha=%f ; Beta=%f" % (alpha, beta))

            S_inv = beta * np.dot(self.X_transformed.T, self.X_transformed)
            S_inv += np.eye(self.X_transformed.shape[1]) * alpha

            S = np.linalg.inv(S_inv)
            m = beta * np.dot(np.dot(S, self.X_transformed.T), self.y)

            self.models.append((m, S))
def train ( self , X , y , do_optimize = True ) : self . X = X if self . basis_func is not None : self . X_transformed = self . basis_func ( X ) else : self . X_transformed = self . X self . y = y if do_optimize : if self . do_mcmc : sampler = emcee . EnsembleSampler ( self . n_hypers , 2 , self . marginal_log_likelihood ) # Do a burn-in in the first iteration if not self . burned : # Initialize the walkers by sampling from the prior self . p0 = self . prior . sample_from_prior ( self . n_hypers ) # Run MCMC sampling self . p0 , _ , _ = sampler . run_mcmc ( self . p0 , self . burnin_steps , rstate0 = self . rng ) self . burned = True # Start sampling pos , _ , _ = sampler . run_mcmc ( self . p0 , self . chain_length , rstate0 = self . rng ) # Save the current position, it will be the start point in # the next iteration self . p0 = pos # Take the last samples from each walker self . hypers = np . exp ( sampler . chain [ : , - 1 ] ) else : # Optimize hyperparameters of the Bayesian linear regression         res = optimize . fmin ( self . negative_mll , self . rng . rand ( 2 ) ) self . hypers = [ [ np . exp ( res [ 0 ] ) , np . exp ( res [ 1 ] ) ] ] else : self . hypers = [ [ self . alpha , self . beta ] ] self . models = [ ] for sample in self . hypers : alpha = sample [ 0 ] beta = sample [ 1 ] logger . debug ( "Alpha=%f ; Beta=%f" % ( alpha , beta ) ) S_inv = beta * np . dot ( self . X_transformed . T , self . X_transformed ) S_inv += np . eye ( self . X_transformed . shape [ 1 ] ) * alpha S = np . linalg . inv ( S_inv ) m = beta * np . dot ( np . dot ( S , self . X_transformed . T ) , self . y ) self . models . append ( ( m , S ) )