def learn_from_instance(self, X, y, weight, ht): """Update the node with the provided instance. Parameters ---------- X: numpy.ndarray of length equal to the number of features. Instance attributes for updating the node. y: int Instance class. weight: float Instance weight. ht: HoeffdingTree Hoeffding Tree to update. """ try: self._observed_class_distribution[0] += weight self._observed_class_distribution[1] += y * weight self._observed_class_distribution[2] += y * y * weight except KeyError: self._observed_class_distribution[0] = weight self._observed_class_distribution[1] = y * weight self._observed_class_distribution[2] = y * y * weight for i in range(len(X)): try: obs = self._attribute_observers[i] except KeyError: if i in ht.nominal_attributes: obs = NominalAttributeRegressionObserver() else: obs = NumericAttributeRegressionObserver() self._attribute_observers[i] = obs obs.observe_attribute_class(X[i], y, weight)
def learn_from_instance(self, X, y, weight, rht): """Update the node with the provided instance. Parameters ---------- X: numpy.ndarray of length equal to the number of features. Instance attributes for updating the node. y: int Instance class. weight: float Instance weight. rht: RegressionHoeffdingTree Regression Hoeffding Tree to update. """ if self.perceptron_weight is None: self.perceptron_weight = self.random_state.uniform( -1, 1, len(X) + 1) try: self._observed_class_distribution[0] += weight except KeyError: self._observed_class_distribution[0] = weight if rht.learning_ratio_const: learning_ratio = rht.learning_ratio_perceptron else: learning_ratio = rht.learning_ratio_perceptron / \ (1 + self._observed_class_distribution[0] * rht.learning_ratio_decay) try: self._observed_class_distribution[1] += y * weight self._observed_class_distribution[2] += y * y * weight except KeyError: self._observed_class_distribution[1] = y * weight self._observed_class_distribution[2] = y * y * weight for i in range(int(weight)): self.update_weights(X, y, learning_ratio, rht) for i in range(len(X)): try: obs = self._attribute_observers[i] except KeyError: if i in rht.nominal_attributes: obs = NominalAttributeRegressionObserver() else: obs = NumericAttributeRegressionObserver() self._attribute_observers[i] = obs obs.observe_attribute_class(X[i], y, weight)