Ejemplo n.º 1
0
    def initialise_parameters(self, data, num_comp):
        """Initialize using D2-weighting
        """

        centroids_idx = init_d2_weighting(data, num_comp)

        init_mean = np.array([data[k, :] for k in centroids_idx])
                
        init_covar = np.cov(data, rowvar=0)
        
        init_covar = np.repeat(np.array([init_covar]), num_comp, 0)

        labels = classify_by_distance(data,
                                      init_mean,
                                      init_covar)
        
        labels = labels.flatten()

        init_covar = self._get_covar(data, labels)
        
        init_mixweights = element_weights(labels)
        
        init_params = {}
        init_params['mean'] = init_mean
        init_params['covar'] = init_covar
        init_params['mixweights'] = init_mixweights
            
        return init_params
Ejemplo n.º 2
0
    def initialise_parameters(self, data, num_comp):
        '''
        Initialize randomly
        '''
        D = data.shape[1]
        
        data_lims = np.array([[data[:, d].min(), data[:, d].max()]
                              for d in range(D)])

        init_mean = np.array([uniform(*data_lims[d, :], size=num_comp)
                              for d in range(D)]).T

        covar_init = np.repeat([np.diag([1] * D)], num_comp, 0)

        labels = classify_by_distance(data,
                                      init_mean,
                                      covar_init)
        labels = labels.flatten()
        
        init_covar = self._get_covar(data, labels)
        
        init_mixweights = element_weights(labels)
        
        init_params = {}
        init_params['mean'] = init_mean
        init_params['covar'] = init_covar
        init_params['mixweights'] = init_mixweights
            
        return init_params
Ejemplo n.º 3
0
    def _init_d2_weighting(self, num_comp):
        """Initialize using D2-weighting
        """

        centroids_idx = init_d2_weighting(self.data, num_comp)

        init_mean = np.array([self.data[k, :] for k in centroids_idx])
        init_covar = np.cov(self.data, rowvar=0)
        init_covar = np.repeat(np.array([init_covar]), num_comp, 0)

        labels = classify_by_distance(self.data, init_mean,
                                      init_covar).flatten()

        init_covar = self._get_covar(self.data, labels)
        init_mixweights = element_weights(labels)
        return (init_mean, labels, init_covar, init_mixweights)
Ejemplo n.º 4
0
    def _init_random(self, num_comp):
        """Initialize randomly
        """
        D = self.data.shape[1]
        data_lims = np.array([[self.data[:, d].min(), self.data[:, d].max()]
                              for d in range(D)])

        init_mean = np.array([uniform(*data_lims[d, :], size=num_comp)
                              for d in range(D)]).T

        covar_init = np.repeat([np.diag([1] * D)], num_comp, 0)

        labels = classify_by_distance(self.data, init_mean,
                                      covar_init).flatten()
        init_covar = self._get_covar(self.data, labels)
        init_mixweights = element_weights(labels)
        return (init_mean, labels, init_covar, init_mixweights)
Ejemplo n.º 5
0
 def initialise_parameters(self, data, init_mean, init_covar, init_mixweights):
     '''
     If starting solution supplied initialise from it.
     '''                
     if init_mixweights is None:
         labels = classify_by_distance(data,
                                       init_mean,
                                       init_covar)
         
         init_mixweights = element_weights(labels)
     
     if init_covar is None:
         init_covar = self._get_covar(data, labels)
     
     init_params = {}
     init_params['mean'] = init_mean
     init_params['covar'] = init_covar
     init_params['mixweights'] = init_mixweights
         
     return init_params
Ejemplo n.º 6
0
    def __init__(self, data,
                 num_comp_init=10,
                 max_iter=200,
                 thresh=1e-5,
                 verbose=False,
                 init_mean=None,
                 init_covar=None,
                 init_mixweights=None,
                 init_method='d2-weighting',
                 prior_dirichlet=1e-3,
                 dof_init=2,
                 remove_comp_thresh=1e-2,
                 whiten_data=False,
                 plot_monitor=False,
                 use_approx=True):
        """Fit the model to the data using Variational Bayes

        """

        (num_obs, num_features) = np.shape(data)

        if whiten_data:
            data = whiten(data)

        self.data = data
        self.remove_comp_thresh = remove_comp_thresh

        # Choose method to intialize the parameters
        if init_method == 'd2-weighting':
            init_method = self._init_d2_weighting
        elif init_method == 'kmeans':
            init_method = self._init_kmeans
        elif init_method == 'random':
            init_method = self._init_random

        if init_mean is None:
            # No starting solution was supplied
            # Initialize with `init_method`
            (init_mean, labels, init_covar, init_mixweights) = \
                        init_method(num_comp_init)
        else:
            # Starting solution was supplied
            num_comp_init = init_mean.shape[0]
            if init_mixweights is None:
                labels = classify_by_distance(data, init_mean,
                                              init_covar)
                init_mixweights = element_weights(labels)
            if init_covar is None:
                init_covar = self._get_covar(data, labels)

        # Initialize data structures
        Prior = _Prior(data, num_comp_init, prior_dirichlet)

        ESS = _ESS(data, num_comp_init, init_mean, init_covar,
                   init_mixweights)

        LatentVariables = _LatentVariables(data, ESS, num_comp_init)

        Posterior = _Posterior(Prior, num_comp_init, dof_init,
                               use_approx=use_approx)

        LowerBound = _LowerBound(data, num_obs, num_features,
                                 num_comp_init, Prior)

        # Initial M-step
        Posterior.update_parameters(Prior, ESS, LatentVariables)

        # Main loop
        iteration = 1
        done = False

        if plot_monitor:
            self._plot_monitor_init()

        while not done:

            # Update parameters
            self._update_step(Prior, Posterior, ESS,
                              LatentVariables, LowerBound)

            # Converged?
            if iteration == 1:
                converged = False
            else:
                converged = self._convergence_test(LowerBound, thresh)

            done = converged or (iteration >= max_iter)

            if plot_monitor:
                self._plot_monitor_update(ESS)

            if verbose:
                print('iteration %d, lower bound: %f' %
                      (iteration, LowerBound.lower_bound[-1]))
                print Posterior.nws_mean

            iteration += 1

        self.Posterior = Posterior
        self.Prior = Prior
        self.LatentVariables = LatentVariables
        self.ESS = ESS
        self.LowerBound = LowerBound

        # Call main loop of wxFrame to keep the window from closing
        if plot_monitor:
            self.frame.end_of_iteration()
            self.app.MainLoop()