示例#1
0
        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, x in enumerate(X.tolist()):
                try:
                    obs = self._attribute_observers[i]
                except KeyError:
                    if ht.nominal_attributes is not None and i in ht.nominal_attributes:
                        obs = NominalAttributeRegressionObserver()
                    else:
                        obs = NumericAttributeRegressionObserverMultiTarget()
                    self._attribute_observers[i] = obs
                obs.observe_attribute_class(x, y, weight)
示例#2
0
        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: numpy.ndarray of length equal to the number of targets.
                Instance targets.
            weight: float
                Instance weight.
            rht: RegressionHoeffdingTree
                Regression Hoeffding Tree to update.
            """
            if self.perceptron_weight is None:
                self.perceptron_weight = {}
                # Creates matrix of perceptron random weights
                _, rows = get_dimensions(y)
                _, cols = get_dimensions(X)

                self.perceptron_weight[0] = \
                    self.random_state.uniform(-1.0, 1.0, (rows, cols + 1))
                # Cascade Stacking
                self.perceptron_weight[1] = \
                    self.random_state.uniform(-1.0, 1.0, (rows, rows + 1))
                self.normalize_perceptron_weights()

            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] += weight * y
                self._observed_class_distribution[2] += weight * y * y
            except KeyError:
                self._observed_class_distribution[1] = weight * y
                self._observed_class_distribution[2] = weight * y * y

            for i in range(int(weight)):
                self.update_weights(X, y, learning_ratio, rht)

            for i, x in enumerate(X):
                try:
                    obs = self._attribute_observers[i]
                except KeyError:
                    # Creates targets observers, if not already defined
                    if rht.nominal_attributes is not None and i in rht.nominal_attributes:
                        obs = NominalAttributeRegressionObserver()
                    else:
                        obs = NumericAttributeRegressionObserverMultiTarget()
                    self._attribute_observers[i] = obs
                obs.observe_attribute_class(x, y, weight)
示例#3
0
        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)