def learn_one(self, x, y): """Update the model with a set of features `x` and a label `y`. Parameters ---------- x A dictionary of features. y The class label. Returns ------- self Notes ----- 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. """ self.classes_.add(y) x_arr = dict2numpy(x) self.data_window.append(x_arr, y) return self
def learn_one(self, x, y): """Update the model with a set of features `x` and a real target value `y`. Parameters ---------- x A dictionary of features. y A numeric target. Returns ------- self Notes ----- For the K-Nearest Neighbors regressor, fitting the model is the equivalent of inserting the newer samples in the observed window, and if the `window_size` is reached, removing older results. """ x_arr = dict2numpy(x) self.data_window.append(x_arr, y) return self
def predict_one(self, x: dict): x_array = dict2numpy(x) c = len(x_array) if self._stm_samples is None: self._stm_samples = np.empty(shape=(0, c)) self._ltm_samples = np.empty(shape=(0, c)) distances_stm = SAMKNNClassifier._get_distances(x_array, self._stm_samples) return self.predict_fct(x_array, None, distances_stm)
def learn_one(self, x: dict, y: base.typing.ClfTarget, **kwargs) -> base.Classifier: if self.estimator is None: self.training_samples_x.append(x) self.training_samples_y.append(y) if len(self.training_samples_x) >= self.n_training_samples and self.estimator is None: x_train = np.stack([dict2numpy(i) for i in self.training_samples_x]) self.estimator = TPOTClassifier(max_time_mins=self.max_time_mins) self.estimator.fit(x_train, self.training_samples_y) self.training_samples_y = [] self.training_samples_x = [] return self
def predict_proba_one(self, x): """Predict the probability of each label for a dictionary of features `x`. Parameters ---------- x A dictionary of features. Returns ------- proba A dictionary which associates a probability which each label. """ proba = {class_idx: 0.0 for class_idx in self.classes_} for idx, _ in enumerate(self.data_window.bufferClasses()): if self.data_window.size(0) == 0: return proba x_arr = dict2numpy(x) output = [] for i, label in enumerate(self.data_window.bufferClasses()): if self.data_window.size(i) != 0: distance, _ = self._get_neighbors(x_arr, i) # If the closest neighbor has a distance of 0, then return it's output if distance[0][0] == 0: proba[label] = 1.0 return proba # Select only the valid neighbors if self.data_window.size(i) < self.n_neighbors: distance = [ dist for cnt, dist in enumerate(distance[0]) if cnt < self.data_window.size(i) ] # only get the valid number that are in the window else: distance = distance[0] # Getting average distance away from each class average_distance = statistics.mean(distance) output.extend([(average_distance, label)]) for index in output: # Inverse the value as the predict_one will find the max proba[index[1]] = 1.0 / index[0] return proba
def predict_proba_one(self, x): """Predict the probability of each label for a dictionary of features `x`. Parameters ---------- x A dictionary of features. Returns ------- proba A dictionary which associates a probability which each label. """ proba = {class_idx: 0.0 for class_idx in self.classes_} if self.data_window.size == 0: # The model is empty, default to None return proba x_arr = dict2numpy(x) dists, neighbor_idx = self._get_neighbors(x_arr) target_buffer = self.data_window.targets_buffer # If the closest neighbor has a distance of 0, then return it's output if dists[0][0] == 0: proba[target_buffer[neighbor_idx[0][0]]] = 1.0 return proba if self.data_window.size < self.n_neighbors: # Select only the valid neighbors neighbor_idx = [ index for cnt, index in enumerate(neighbor_idx[0]) if cnt < self.data_window.size ] dists = [ dist for cnt, dist in enumerate(dists[0]) if cnt < self.data_window.size ] else: neighbor_idx = neighbor_idx[0] dists = dists[0] if not self.weighted: # Uniform weights for index in neighbor_idx: proba[target_buffer[index]] += 1.0 else: # Use the inverse of the distance to weight the votes for d, index in zip(dists, neighbor_idx): proba[target_buffer[index]] += 1.0 / d return softmax(proba)
def predict_one(self, x): """Predict the target value of a set of features `x`. Search the KDTree for the `n_neighbors` nearest neighbors. Parameters ---------- x A dictionary of features. Returns ------- The prediction. """ if self.data_window.size == 0: # Not enough information available, return default prediction return 0.0 x_arr = dict2numpy(x) dists, neighbor_idx = self._get_neighbors(x_arr) target_buffer = self.data_window.targets_buffer # If the closest neighbor has a distance of 0, then return it's output if dists[0][0] == 0: return target_buffer[neighbor_idx[0][0]] if self.data_window.size < self.n_neighbors: # Select only the valid neighbors neighbor_vals = [ target_buffer[index] for cnt, index in enumerate(neighbor_idx[0]) if cnt < self.data_window.size ] dists = [ dist for cnt, dist in enumerate(dists[0]) if cnt < self.data_window.size ] else: neighbor_vals = [target_buffer[index] for index in neighbor_idx[0]] dists = dists[0] if self.aggregation_method == self._MEAN: return np.mean(neighbor_vals) elif self.aggregation_method == self._MEDIAN: return np.median(neighbor_vals) else: # weighted mean return sum(y / d for y, d in zip(neighbor_vals, dists)) / sum( 1 / d for d in dists)
def learn_one(self, x, y) -> 'Classifier': """Update the model with a set of features `x` and a label `y`. Parameters ---------- x The sample's features y The sample's class label. Returns ------- self """ x_array = dict2numpy(x) c = len(x_array) if self._stm_samples is None: self._stm_samples = np.empty(shape=(0, c)) self._ltm_samples = np.empty(shape=(0, c)) self._learn_one(x_array, y) return self
def learn_one(self, x, y): """Update the model with a set of features `x` and a label `y`. Parameters ---------- x A dictionary of features. y The class label. Returns ------- self Notes ----- 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. """ self.classes_.add(y) self.data_window.append(dict2numpy(x), y) if self.data_window.size >= self.n_neighbors: correctly_classifies = int(self.predict_one(x) == y) self.adwin.update(correctly_classifies) else: self.adwin.update(0) if self.data_window.size >= self.n_neighbors: if self.adwin.change_detected: if self.adwin.width < self.data_window.size: for i in range(self.data_window.size, self.adwin.width, -1): self.data_window.popleft() return self