def clone_window(self):
        window = InstanceWindow(
            n_features=self.window.n_attributes,
            n_targets=self.window.n_targets,
            categorical_list=self.window.categorical_attributes,
            max_size=self.window.max_size)
        window._buffer = np.array(self.window._buffer)
        window._n_samples = self.window._n_samples

        return window
Ejemplo n.º 2
0
 def __init__(self, n_neighbors=5, max_window_size=1000, leaf_size=30, categorical_list=None):
     super().__init__()
     self.n_neighbors = n_neighbors
     self.max_window_size = max_window_size
     self.c = 0
     self.window = InstanceWindow(max_size=max_window_size, dtype=float)
     self.first_fit = True
     self.classes = []
     self.leaf_size = leaf_size
     if categorical_list is None:
         self.categorical_list = []
    def __init__(self, estimator: RegressorMixin, max_window_size=100):
        super().__init__()

        if not isinstance(estimator, RegressorMixin):
            raise ValueError(
                "estimator must be a Regressor, "
                "Call TimeSeriesRegressor with an instance of RegressorMixin")

        self.max_window_size = max_window_size
        self.estimator = estimator
        self.window = InstanceWindow(max_size=max_window_size, dtype=float)
        self.first_fit = True
Ejemplo n.º 4
0
    def partial_fit(self, X, y, classes=None, weight=None):
        """ partial_fit
        
        Partially fits the model. This is done by updating the window 
        with new samples while also updating the adwin algorithm. Then 
        we verify if a change was detected, and if so, the window is 
        correctly split at the drift moment.
        
        Parameters
        ----------
        X: Numpy.ndarray of shape (n_samples, n_features)
            The data upon which the algorithm will create its model.
            
        y: Array-like
            An array-like containing the classification targets for all 
            samples in X.
            
        classes: Not used.

        weight: Not used.
        
        Returns
        -------
        KNNAdwin
            self
        
        """
        r, c = get_dimensions(X)
        if self.window is None:
            self.window = InstanceWindow(max_size=self.max_window_size)

        for i in range(r):
            if r > 1:
                self.window.add_element(np.asarray([X[i]]), np.asarray([[y[i]]]))
            else:
                self.window.add_element(np.asarray([X[i]]), np.asarray([[y[i]]]))
            if self.window._num_samples >= self.n_neighbors:
                add = 1 if self.predict(np.asarray([X[i]])) == y[i] else 0
                self.adwin.add_element(add)
            else:
                self.adwin.add_element(0)

        if self.window._num_samples >= self.n_neighbors:
            changed = self.adwin.detected_change()

            if changed:
                if self.adwin.width < self.window._num_samples:
                    for i in range(self.window._num_samples, self.adwin.width, -1):
                        self.window.delete_element()
        return self
Ejemplo n.º 5
0
 def __init__(self,
              n_neighbors=5,
              max_window_size=1000,
              leaf_size=30,
              nominal_attributes=None):
     super().__init__()
     self.n_neighbors = n_neighbors
     self.max_window_size = max_window_size
     self.c = 0
     self.window = InstanceWindow(max_size=max_window_size, dtype=float)
     self.first_fit = True
     self.classes = []
     self.leaf_size = leaf_size
     self.nominal_attributes = nominal_attributes
     if self.nominal_attributes is None:
         self._nominal_attributes = []
Ejemplo n.º 6
0
    def fit(self, X, y, classes=None, weight=None):
        """ fit
        
        Fits the model on the samples X and targets y. This is actually the 
        function as the partial fit.
        
        For the K-Nearest Neighbors Classifier, fitting the model is the 
        equivalent of inserting the newer samples in the observed window, 
        and if the size_limit is reached, removing older results. To store 
        the viewed samples we use a InstanceWindow object. For this class' 
        documentation please visit skmultiflow.core.utils.data_structures
        
        Parameters
        ----------
        X: Numpy.ndarray of shape (n_samples, n_features)
            The data upon which the algorithm will create its model.
            
        y: Array-like
            An array-like containing the classification targets for all 
            samples in X.
            
        classes: Not used.

        weight: Not used.
        
        Returns
        -------
        KNN
            self
        
        """
        r, c = get_dimensions(X)
        if classes is not None:
            self.classes = list(set().union(self.classes, classes))
        if self.window is None:
            self.window = InstanceWindow(max_size=self.max_window_size)

        for i in range(r):
            self.window.add_element(np.asarray([X[i]]), np.asarray([[y[i]]]))
        return self
Ejemplo n.º 7
0
    def prepare_post_analysis_req(self,
                                  num_features,
                                  num_targets,
                                  num_classes,
                                  target_values,
                                  record=False):
        # Need to get the dataset information but we do not want to
        # take it as an argument to the classifier itself, nor we do want to
        # ask it at each data instance. Hence we take dataset info from user
        # explicitly to create _chunk_data entries.
        #chunk_size = self._chunk_size
        self._chunk_data = InstanceWindow(n_features=num_features,
                                          n_targets=num_targets,
                                          max_size=self._chunk_size)
        #self._chunk_data = chunk_data
        # num_targets shows how many columns you want to predict in the data.
        # num classes is eqv to possible number of values that that column
        # can have.
        self._num_classes = num_classes
        self._target_values = target_values
        self._record = record

        if (self._record):
            # Create files that keeps record of:
            #   - weights at each chunk
            #   - individual component results for every instance
            #   - ground truths for every instance.
            self._f_comp_preds = open("component_predictions.csv", "w+")
            self._f_truths = open("ground_truths.csv", "w+")
            self._f_weights = open("weights.csv", "w+")

            self._f_comp_preds.write(str(self._chunk_size) + '\n')

            self._f_comp_preds.close()
            self._f_truths.close()
            self._f_weights.close()
        return