def append(self, x: np.ndarray, y: base.typing.Target): """Add a (single) sample to the specific class sample window. x 1D-array of feature for a single sample. y The target data for a single sample. - will be associated with its target window """ if not self._is_initialized: self._n_features = get_dimensions(x)[1] self._configure() if self._n_features != get_dimensions(x)[1]: raise ValueError( "Inconsistent number of features in X: {}, previously observed {}." .format(get_dimensions(x)[1], self._n_features)) yIndex = self.classes.index(y) # Gets index of class element self._X[yIndex, self._next_insert[yIndex], :] = x slot_replaced = self._imask[yIndex, self._next_insert[yIndex]] self._imask[yIndex, self._next_insert[yIndex]] = True self._next_insert[yIndex] = ( self._next_insert[yIndex] + 1 if self._next_insert[yIndex] < self.window_size - 1 else 0 ) # Postion oldest insert as newest if not slot_replaced: # This is not used after size == window - size of self._size[yIndex] += 1 return self
def append(self, x: np.ndarray, y: base.typing.Target) -> "KNeighborsBuffer": """Add a (single) sample to the sample window. x 1D-array of feature for a single sample. y The target data for a single sample. Raises ------ ValueError: If at any moment, a sample with a different number of attributes than that of the n_attributes parameter is passed, a ValueError is raised. TypeError: If the buffer type is altered by the user, or is not correctly initialized, a TypeError may be raised. """ if not self._is_initialized: self._n_features = get_dimensions(x)[1] self._n_targets = get_dimensions(y)[1] self._configure() if self._n_features != get_dimensions(x)[1]: raise ValueError( "Inconsistent number of features in X: {}, previously observed {}.".format( get_dimensions(x)[1], self._n_features ) ) self._X[self._next_insert, :] = x self._y[self._next_insert] = y slot_replaced = self._imask[self._next_insert] # Update the instance storing logic self._imask[self._next_insert] = True # Mark slot as filled self._next_insert = ( self._next_insert + 1 if self._next_insert < self.window_size - 1 else 0 ) if ( slot_replaced ): # The oldest sample was replaced (complete cycle in the buffer) self._oldest = self._next_insert else: # Actual buffer increased self._size += 1 return self