Beispiel #1
0
    def add_result(self, y_true, y_pred, weight=1.0):
        """ Updates its statistics with the results of a prediction.

        Parameters
        ----------
        y_true: int
            Index of the true label

        y_pred: int
            Indices of top-N predicted labels

        weight: float
            Sample's weight

        """
        check_weights(weight)
        rank = np.where(y_pred == y_true)[0]
        if rank.size == 1:  # Relevant item exists
            self.rranks.append(1 / (rank[0] + 1))  # Works for next-item prediction
            self.confusion_matrix.update(y_true, y_true, weight=weight)  # True positive
        else:
            self.confusion_matrix.update(y_true, y_pred[0], weight=weight)  # False positive

        self.sample_count += 1

        if self.last_true_label == y_true:
            self.correct_no_change += weight

        self.last_true_label = y_true
        self.last_prediction = y_pred
    def add_result(self, y_true, y_pred, weight=1.0):
        """ Updates its statistics with the results of a prediction.

        Parameters
        ----------
        y_true: int
            The true label.

        y_pred: int
            The classifier's prediction

        weight: float
            Sample's weight

        """
        check_weights(weight)

        true_y = self._get_target_index(y_true, True)
        pred = self._get_target_index(y_pred, True)
        self.confusion_matrix.update(true_y, pred)
        self.sample_count += weight

        if self.get_majority_class() == y_true:
            self.majority_classifier = self.majority_classifier + weight
        if self.last_true_label == y_true:
            self.correct_no_change = self.correct_no_change + weight

        self.last_true_label = y_true
        self.last_prediction = y_pred
Beispiel #3
0
    def add_result(self, y_true, y_pred, weight=1.0):
        """ Updates its statistics with the results of a prediction.
        If needed it will remove samples from the observation window.

        Parameters
        ----------
        y_true: int
            The true label.

        y_pred: int
            The classifier's prediction

        weight: float
            Sample's weight
        """
        check_weights(weight)
        true_y = self._get_target_index(y_true, True)
        pred = self._get_target_index(y_pred, True)
        old_true = self.true_labels.add_element(np.array([y_true]))
        old_predict = self.predictions.add_element(np.array([y_pred]))

        # Verify if it's needed to decrease the count of any label
        # pair in the confusion matrix
        if (old_true is not None) and (old_predict is not None):
            self.temp += 1
            self.confusion_matrix.remove(
                self._get_target_index(old_true[0]),
                self._get_target_index(old_predict[0]))
            self.correct_no_change += self.correct_no_change_correction.peek()
            self.majority_classifier += self.majority_classifier_correction.peek(
            )

        # Verify if it's needed to decrease the majority_classifier count
        if (self.get_majority_class()
                == y_true) and (self.get_majority_class() is not None):
            self.majority_classifier += weight
            self.majority_classifier_correction.add_element([-1])
        else:
            self.majority_classifier_correction.add_element([0])

        # Verify if it's needed to decrease the correct_no_change
        if (self.last_true_label == y_true) and (self.last_true_label
                                                 is not None):
            self.correct_no_change += weight
            self.correct_no_change_correction.add_element([-1])
        else:
            self.correct_no_change_correction.add_element([0])

        self.confusion_matrix.update(true_y, pred, weight=weight)

        self.last_true_label = y_true
        self.last_prediction = y_pred
    def partial_fit(self, X, y, classes=None, sample_weight=None):
        """ Partially (incrementally) fit the model.
        Parameters
        ----------
        X : numpy.ndarray of shape (n_samples, n_features)
            The features to train the model.
        y: numpy.ndarray of shape (n_samples)
            An array-like with the class labels of all samples in X.
        classes: numpy.ndarray, list, optional (default=None)
            Array with all possible/known class labels. This is an optional parameter, except
            for the first partial_fit call where it is compulsory.
        sample_weight: numpy.ndarray of shape (n_samples), optional (default=None)
            Samples weight. If not provided, uniform weights are assumed.
        """
        if self.classes is None and classes is not None:
            self.classes = classes

        if sample_weight is None:
            weight = 1.0
        else:
            weight = sample_weight

        if y is not None:
            row_cnt, _ = get_dimensions(X)
            weight = check_weights(weight, expand_length=row_cnt)
            for iterator in range(row_cnt):
                if weight[iterator] != 0.0:
                    self._partial_fit(X[iterator], y[iterator], self.classes,
                                      weight[iterator])
        return self
 def partial_fit(self, X, y, classes=None, weight=1.0):
     if y is not None:
         row_cnt, _ = get_dimensions(X)
         weight = check_weights(weight, expand_length=row_cnt)
         for i in range(row_cnt):
             if weight[i] != 0.0:
                 self._train_weight_seen_by_model += weight[i]
                 self._partial_fit(X[i], y[i], weight[i])
Beispiel #6
0
    def add_result(self, y_true, y_pred, weight=1.0):
        """ Updates its statistics with the results of a prediction.
        If needed it will remove samples from the observation window.

        Parameters
        ----------
        y_true: int
            Index of the true label

        y_pred: int
            Indices of top-N predicted labels

        weight: float
            Sample's weight
        """
        check_weights(weight)
        old_true = self.true_labels.add_element(np.array([y_true]))

        rank = np.where(y_pred == y_true)[0]
        if rank.size == 1:  # Relevant item exists
            self.rranks.add_element(np.array([1 / (rank[0] + 1)]))
            self.confusion_matrix.update(y_true, y_true, weight=weight)
            old_predict = self.predictions.add_element(np.array([y_true]))  # Add correct prediction
        else:
            self.rranks.add_element(np.array([0]))
            self.confusion_matrix.update(y_true, y_pred[0], weight=weight)
            old_predict = self.predictions.add_element(np.array([y_pred[0]]))  # Add some incorrect prediction

        # Verify if it's needed to decrease the count of any label
        # pair in the confusion matrix
        if (old_true is not None) and (old_predict is not None):
            self.temp += 1
            self.confusion_matrix.remove(old_true[0], old_predict[0])
            self.correct_no_change += self.correct_no_change_correction.peek()

        # Verify if it's needed to decrease the correct_no_change
        if (self.last_true_label == y_true) and (self.last_true_label is not None):
            self.correct_no_change += weight
            self.correct_no_change_correction.add_element([-1])
        else:
            self.correct_no_change_correction.add_element([0])

        self.last_true_label = y_true
        self.last_prediction = y_pred
def test_check_weights():
    test_value = 5
    weights = check_weights(int(test_value))
    assert weights == int(test_value)

    weights = check_weights(float(test_value))
    assert weights == float(test_value)

    weights = check_weights(np.float(test_value))
    assert weights == np.float(test_value)

    weights = check_weights(np.int(test_value))
    assert weights == np.int(test_value)

    weights = check_weights(np.array([test_value], np.float))
    assert isinstance(weights, np.ndarray)
    assert isinstance(weights[0], np.float)

    weights = check_weights(np.array(np.arange(test_value)))
    assert isinstance(weights, np.ndarray)
    for w in weights:
        assert isinstance(w, np.integer)

    weights = check_weights([float(x) for x in range(test_value)])
    assert isinstance(weights, list)
    for w in weights:
        assert isinstance(w, float)

    weights = check_weights(int(test_value), expand_length=10)
    assert isinstance(weights, np.ndarray)
    for w in weights:
        assert w == int(test_value)

    with pytest.raises(ValueError):
        check_weights([1.0, 2.0, 3.0, 4.0, 'invalid'])

    with pytest.raises(ValueError):
        check_weights('invalid')

    with pytest.raises(ValueError):
        check_weights([float(x) for x in range(test_value)], expand_length=10)

    with pytest.raises(ValueError):
        check_weights(int(test_value), expand_length=-10)