Beispiel #1
0
    def predict(self, x):
        """
        applying multiple estimators for prediction
        Args:
            x (numpy.ndarray): NxD array
        Returns:
            numpy.ndarray: predicted labels, Nx1 array
        """
        predictions = []
        confidences = []
        for i in self.estimators:
            estimators_from_i = self.estimators[i]
            for j in estimators_from_i:
                estimator = estimators_from_i[j]
                confidence = np.ravel(estimator.decision_function(x))

                indices = (confidence > 0).astype(np.int)
                prediction = self.classes_[indices]

                predictions.append(prediction.reshape(-1, 1))
                confidences.append(confidence.reshape(-1, 1))

        predictions = np.hstack(predictions)
        confidences = np.hstack(confidences)
        y = _ovr_decision_function(predictions,
                                   confidences, len(self.classes_))
        return self.classes_[y.argmax(axis=1)]
Beispiel #2
0
def test_ovr_decision_function():
    # test properties for ovr decision function

    predictions = np.array([[0, 1, 1],
                            [0, 1, 0],
                            [0, 1, 1],
                            [0, 1, 1]])

    confidences = np.array([[-1e16, 0, -1e16],
                            [1., 2., -3.],
                            [-5., 2., 5.],
                            [-0.5, 0.2, 0.5]])

    n_classes = 3

    dec_values = _ovr_decision_function(predictions, confidences, n_classes)

    # check that the decision values are within 0.5 range of the votes
    votes = np.array([[1, 0, 2],
                      [1, 1, 1],
                      [1, 0, 2],
                      [1, 0, 2]])

    assert_allclose(votes, dec_values, atol=0.5)

    # check that the prediction are what we expect
    # highest vote or highest confidence if there is a tie.
    # for the second sample we have a tie (should be won by 1)
    expected_prediction = np.array([2, 1, 2, 2])
    assert_array_equal(np.argmax(dec_values, axis=1), expected_prediction)

    # third and fourth sample have the same vote but third sample
    # has higher confidence, this should reflect on the decision values
    assert (dec_values[2, 2] > dec_values[3, 2])

    # assert subset invariance.
    dec_values_one = [_ovr_decision_function(np.array([predictions[i]]),
                                             np.array([confidences[i]]),
                                             n_classes)[0] for i in range(4)]

    assert_allclose(dec_values, dec_values_one, atol=1e-6)
def test_ovr_decision_function():
    # test properties for ovr decision function

    predictions = np.array([[0, 1, 1],
                            [0, 1, 0],
                            [0, 1, 1],
                            [0, 1, 1]])

    confidences = np.array([[-1e16, 0, -1e16],
                            [1., 2., -3.],
                            [-5., 2., 5.],
                            [-0.5, 0.2, 0.5]])

    n_classes = 3

    dec_values = _ovr_decision_function(predictions, confidences, n_classes)

    # check that the decision values are within 0.5 range of the votes
    votes = np.array([[1, 0, 2],
                      [1, 1, 1],
                      [1, 0, 2],
                      [1, 0, 2]])

    assert_allclose(votes, dec_values, atol=0.5)

    # check that the prediction are what we expect
    # highest vote or highest confidence if there is a tie.
    # for the second sample we have a tie (should be won by 1)
    expected_prediction = np.array([2, 1, 2, 2])
    assert_array_equal(np.argmax(dec_values, axis=1), expected_prediction)

    # third and fourth sample have the same vote but third sample
    # has higher confidence, this should reflect on the decision values
    assert (dec_values[2, 2] > dec_values[3, 2])

    # assert subset invariance.
    dec_values_one = [_ovr_decision_function(np.array([predictions[i]]),
                                             np.array([confidences[i]]),
                                             n_classes)[0] for i in range(4)]

    assert_allclose(dec_values, dec_values_one, atol=1e-6)