Exemplo n.º 1
0
    def fit(self, x_train, w_train):
        # Train all committee classifiers with their corresponding views.
        for (classifier, view) in self.committee:
            classifier.fit(x_train[:, view], w_train)

        # Estimate a prior probabilities p(wi) for each class wi.
        self.p_w = DataLoader.compute_a_priori(w_train)

        return self
Exemplo n.º 2
0
  def fit(self, x_train, w_train):
    """
    Stores training points *x_train* and their correponsindg labels *w_train*,
    and estimates the a prior probabilities p(w_i) for each class w_i.
    """
    # Store examples.
    self.x_train = x_train
    self.w_train = w_train

    # Estimate a prior probabilities p(wi) for each class wi.
    self.p_w = DataLoader.compute_a_priori(w_train)
    self.num_classes = len(self.p_w)

    return self
Exemplo n.º 3
0
    def fit(self, x_train, w_train):
        """
    Estimates a prior probabilities p(w_i), the mean i and the variance i for
    each class in the training dataset. That is, from (x_train, w_train).
    Once trained, you can call classify() to predict the class/label
    for a given feature vector.
    Note: all classes must have at least one sample.
    """
        # Break down dataset into smaller groups sharing the same label.
        x_groups = DataLoader.group_by_label(x_train, w_train)

        # Estimate a prior probabilities p(wi) for each class wi.
        self.p_w = DataLoader.compute_a_priori(w_train)

        # Estimate mean and [diagonal] variances for each class w_i.
        # Pattern Classification (Second Edition), Section 3.2.3.
        self.mu = np.array(
            list(map(
                lambda x_train_i: np.mean(x_train_i, axis=0),
                x_groups,
            )))
        self.sigma = np.array(
            list(
                map(
                    lambda i: np.mean((x_groups[i] - self.mu[i])**2, axis=0),
                    range(len(x_groups)),
                )))

        # For the sake of optimization, we may precompute some constants in the
        # gaussian pdf equations - the amplitudes and the inverse of sigma.
        n = len(self.mu)
        pi_const = np.power(2 * np.pi, n / 2.0)
        det_sigma = np.abs(np.product(self.sigma, axis=1))
        self.inv_sigma = 1.0 / (self.sigma + epsilon)
        self.amplitudes = 1.0 / (pi_const * np.sqrt(det_sigma + epsilon))

        return self