예제 #1
0
    def predict_proba(self, X):
        """Return probability estimates for the test data X.

        Parameters
        ----------
        X : array-like, shape (n_query, n_features), \
                or (n_query, n_indexed) if metric == 'precomputed'
            Test samples.

        Returns
        -------
        proba : array of shape (n_samples, n_classes), or a list of n_outputs
            of such arrays if n_outputs > 1.
            The class probabilities of the input samples. Classes are ordered
            by lexicographic order.
        """
        X = check_array(X, accept_sparse='csr')

        dists, inds = self.kneighbors(X, return_distance=True)
        classes = self.fit_y_[inds]
        dists_array = np.empty((X.shape[0], self.n_classes_))
        # TODO: check if a more efficient implementation can be done
        for c in self.classes_:
            dists_array[:, c] = np.ma.MaskedArray(dists,
                                                  classes != c).mean(axis=1)
        probas = softmax(1. / dists_array)
        return probas
예제 #2
0
def predict_proba_ensemble_weighted(classifier_ensemble, weights, X):
    """Estimates the posterior probabilities for each sample in X.

    Parameters
    ----------
    classifier_ensemble : list of shape = [n_classifiers]
                         containing the ensemble of classifiers used to estimate the probabilities.

    weights : array of shape = [n_samples, n_classifiers]
              Weights associated to each base classifier for each sample

    X : array of shape = [n_samples, n_features]
        The input data.

    Returns
    -------
    list_proba : array of shape = [n_classifiers, n_samples, n_classes]
                 probabilities predicted by each base classifier in the ensemble for all samples in X.
    """
    if weights.ndim == 1:
        weights = np.atleast_2d(weights)

    ensemble_proba = _get_ensemble_probabilities(classifier_ensemble, X)
    n_classifiers = ensemble_proba.shape[0]
    if n_classifiers != weights.shape[1]:
        raise ValueError(
            'The number of weights should be equal to the number of base classifiers in the ensemble.'
            'The number of classifiers is {},'
            ' and the number of weights is {}'.format(n_classifiers,
                                                      weights.shape[1]))

    predicted_proba = np.einsum('ijk,ji->jk', ensemble_proba,
                                weights) / n_classifiers

    return softmax(predicted_proba)
예제 #3
0
def aggregate_proba_ensemble_weighted(ensemble_proba, weights):
    predicted_proba = ensemble_proba * np.expand_dims(weights, axis=2)
    predicted_proba = predicted_proba.mean(axis=1)

    return softmax(predicted_proba)
예제 #4
0
def test_softmax(vector, expected):
    assert np.isclose(softmax(vector), expected, atol=0.001).all()
예제 #5
0
def test_softmax_sum_to_one():
    test = np.random.rand(10)
    assert np.allclose(np.sum(softmax(test)), 1.0, atol=0.001)