def test_check_random_state():
    rand = None

    rand = check_random_state(rand)
    assert isinstance(rand, np.random.mtrand.RandomState)

    rand = check_random_state(rand)
    assert isinstance(rand, np.random.mtrand.RandomState)

    rand = check_random_state(int(1))
    assert isinstance(rand, np.random.mtrand.RandomState)

    with pytest.raises(ValueError):
        check_random_state(2.0)
    def generate_centroids(self):
        """ generate_centroids
        
        The centroids are generated just as it's done in the parent class, 
        the difference is the extra step taken to setup the drift, if there's 
        any.
        
        To __configure the drift, random offset speeds are chosen for 
        self.num_drift_centroids centroids. Finally, the speed are 
        normalized.
        
        """
        super().generate_centroids()
        model_random_state = check_random_state(
            self._original_model_random_state)
        num_drift_centroids = self.num_drift_centroids
        self.centroid_speed = []
        if num_drift_centroids > self.n_centroids:
            num_drift_centroids = self.n_centroids

        for i in range(num_drift_centroids):
            rand_speed = []
            norm_speed = 0.0

            for j in range(self.n_num_features):
                rand_speed.append(model_random_state.rand())
                norm_speed += rand_speed[j] * rand_speed[j]

            norm_speed = np.sqrt(norm_speed)

            for j in range(self.n_num_features):
                rand_speed[j] /= norm_speed

            self.centroid_speed.append(rand_speed)
    def __init__(self,
                 nb_ensemble=10,
                 max_features='auto',
                 disable_weighted_vote=False,
                 lambda_value=6,
                 performance_metric='acc',
                 drift_detection_method: BaseDriftDetector = ADWIN(0.001),
                 warning_detection_method: BaseDriftDetector = ADWIN(0.01),
                 max_byte_size=33554432,
                 memory_estimate_period=2000000,
                 grace_period=50,
                 split_criterion='info_gain',
                 split_confidence=0.01,
                 tie_threshold=0.05,
                 binary_split=False,
                 stop_mem_management=False,
                 remove_poor_atts=False,
                 no_preprune=False,
                 leaf_prediction='nba',
                 nb_threshold=0,
                 nominal_attributes=None,
                 random_state=None):
        """AdaptiveRandomForest class constructor."""
        super().__init__()
        self.nb_ensemble = nb_ensemble
        self.max_features = max_features
        self.disable_weighted_vote = disable_weighted_vote
        self.lambda_value = lambda_value
        if isinstance(drift_detection_method, BaseDriftDetector):
            self.drift_detection_method = drift_detection_method
        else:
            self.drift_detection_method = None
        if isinstance(warning_detection_method, BaseDriftDetector):
            self.warning_detection_method = warning_detection_method
        else:
            self.warning_detection_method = None
        self.instances_seen = 0
        self._train_weight_seen_by_model = 0.0
        self.ensemble = None
        self.random_state = check_random_state(random_state)
        if performance_metric in ['acc', 'kappa']:
            self.performance_metric = performance_metric
        else:
            raise ValueError(
                'Invalid performance metric: {}'.format(performance_metric))

        # ARH Hoeffding Tree configuration
        self.max_byte_size = max_byte_size
        self.memory_estimate_period = memory_estimate_period
        self.grace_period = grace_period
        self.split_criterion = split_criterion
        self.split_confidence = split_confidence
        self.tie_threshold = tie_threshold
        self.binary_split = binary_split
        self.stop_mem_management = stop_mem_management
        self.remove_poor_atts = remove_poor_atts
        self.no_preprune = no_preprune
        self.leaf_prediction = leaf_prediction
        self.nb_threshold = nb_threshold
        self.nominal_attributes = nominal_attributes
    def prepare_for_use(self):
        """
        Should be called before generating the samples.

        """
        self.random_state = check_random_state(self._original_random_state)
        self.sample_idx = 0
    def generate_random_tree(self):
        """ generate_random_tree
        
        Generates the random tree, starting from the root node and following 
        the constraints passed as parameters to the initializer. 
        
        The tree is recursively generated, node by node, until it reaches the
        maximum tree depth.
        
        """
        # Starting random generators and parameter arrays
        tree_random_state = check_random_state(
            self._original_tree_random_state)
        nominal_att_candidates = array('i')
        min_numeric_value = array('d')
        max_numeric_value = array('d')

        for i in range(self.n_num_features):
            min_numeric_value.append(0.0)
            max_numeric_value.append(1.0)

        for i in range(self.n_num_features + self.n_cat_features):
            nominal_att_candidates.append(i)

        self.tree_root = self.generate_random_tree_node(
            0, nominal_att_candidates, min_numeric_value, max_numeric_value,
            tree_random_state)
 def __init__(self, split_test, class_observations):
     SplitNode.__init__(self, split_test, class_observations)
     self._estimation_error_weight = ADWIN()
     self._alternate_tree = None
     self.error_change = False
     self._random_seed = 1
     self._classifier_random = check_random_state(self._random_seed)
    def prepare_for_use(self):
        """
        Sould be called before generating the samples.

        """
        self.random_state = check_random_state(self._original_random_state)
        self.next_class_should_be_zero = False
        self.sample_idx = 0
Beispiel #8
0
 def prepare_for_use(self):
     self.random_state = check_random_state(self._original_random_state)
     self.sample_idx = 0
     self._input_stream.prepare_for_use()
     self._drift_stream.prepare_for_use()
     if self.alpha_option != 0.0:
         self.width_option = int(1 /
                                 np.tan(self.alpha_option * np.pi / 180))
Beispiel #9
0
 def __configure(self):
     self.random_state = check_random_state(self._original_random_state)
     self.n_cat_features = self._TOTAL_ATTRIBUTES_INCLUDING_NOISE if self.has_noise else self._NUM_BASE_ATTRIBUTES
     self.n_features = self.n_cat_features
     self.feature_names = [
         "att_num_" + str(i) for i in range(self.n_cat_features)
     ]
     self.target_values = [i for i in range(self.n_targets)]
    def prepare_for_use(self):
        """
        Should be called before generating the samples.

        """
        self.random_state = check_random_state(self._original_random_state)
        self._next_class_should_be_zero = False
        self.sample_idx = 0
        for i in range(self.n_features):
            self._weights[i] = self.random_state.rand()
            self._sigma[i] = 1 if (i < self.n_drift_features) else 0
    def _partial_fit(self, X, y, weight):
        self.instances_seen += 1
        
        if self.ensemble is None:
            self.init_ensemble(X)

        for i in range(self.nb_ensemble):
            y_predicted = self.ensemble[i].predict(np.asarray([X]))
            self.ensemble[i].evaluator.add_result(y_predicted, y, weight)
            rnd = check_random_state(self.random_state)
            k = rnd.poisson(self.lambda_value)
            if k > 0:
                self.ensemble[i].partial_fit(np.asarray([X]), np.asarray([y]), np.asarray([k]), self.instances_seen)
 def __init__(self, n_samples=40000, n_features=100, n_informative=10, n_targets=1, random_state=None):
     super().__init__()
     self.X = None
     self.y = None
     self.n_samples = n_samples
     self.n_features = n_features
     self.n_targets = n_targets
     self.n_informative = n_informative
     self.n_num_features = n_features
     self.n_features = n_features
     self.random_state = check_random_state(random_state)
     self.name = "Regression Generator"
     self.__configure()
    def __init__(self,
                 n_samples=40000,
                 n_features=20,
                 n_targets=5,
                 n_labels=2,
                 random_state=None):
        super().__init__()
        self.X = None
        self.y = None
        self.n_samples = n_samples
        self.n_features = n_features
        self.n_targets = n_targets
        self.n_labels = n_labels
        self.n_classes = 2
        self.n_num_features = n_features
        self.random_state = check_random_state(random_state)
        self.name = "Multilabel Generator"

        self.__configure()
Beispiel #14
0
 def __init__(self,
              n_samples=40000,
              n_features=20,
              n_targets=5,
              n_labels=2,
              random_state=None):
     super().__init__()
     self.X = None
     self.y = None
     self.num_samples = 0
     self.num_features = 0
     self.num_target_tasks = 0
     self.num_labels = 0
     self.num_numerical_attributes = 0
     self.num_nominal_attributes = 0
     self.num_values_per_nominal_att = 0
     self.instance_index = 0
     self.current_instance_y = None
     self.current_instance_x = None
     self.random_state = check_random_state(random_state)
     self.__configure(n_samples, n_features, n_targets, n_labels)
 def generate_centroids(self):
     """ generate_centroids
     
     Sequentially creates all the centroids, choosing at random a center, 
     a label, a standard deviation and a weight. 
     
     """
     model_random_state = check_random_state(
         self._original_model_random_state)
     self.centroids = []
     self.centroid_weights = []
     for i in range(self.n_centroids):
         self.centroids.append(Centroid())
         rand_centre = []
         for j in range(self.n_num_features):
             rand_centre.append(model_random_state.rand())
         self.centroids[i].centre = rand_centre
         self.centroids[i].class_label = model_random_state.randint(
             self.n_classes)
         self.centroids[i].std_dev = model_random_state.rand()
         self.centroid_weights.append(model_random_state.rand())
Beispiel #16
0
 def prepare_for_use(self):
     self.random_state = check_random_state(self._original_random_state)
     self.next_class_should_be_zero = False
     self.sample_idx = 0
 def prepare_for_use(self):
     self.sample_random_state = check_random_state(
         self._original_sample_random_state)
     self.sample_idx = 0
     self.generate_random_tree()
Beispiel #18
0
 def prepare_for_use(self):
     self.random_state = check_random_state(self._original_random_state)
     self.sample_idx = 0
 def prepare_for_use(self):
     self.generate_centroids()
     self.sample_random_state = check_random_state(
         self._original_sample_random_state)
 def _sample_features(self, n_features):
     rnd = check_random_state(self.random_state)
     return rnd.choice(n_features, size=self.max_features, replace=False)
 def __init__(self, initial_class_observations):
     LearningNodeNBAdaptive.__init__(self, initial_class_observations)
     self.estimationErrorWeight = ADWIN()
     self.ErrorChange = False
     self._randomSeed = 1
     self._classifier_random = check_random_state(self._randomSeed)