(self, x)
| 18 | return predictions |
| 19 | |
| 20 | def _predict(self, x): |
| 21 | # compute the distance |
| 22 | distances = [euclidean_distance(x, x_train) for x_train in self.X_train] |
| 23 | |
| 24 | # get the closest k |
| 25 | k_indices = np.argsort(distances)[:self.k] |
| 26 | k_nearest_labels = [self.y_train[i] for i in k_indices] |
| 27 | |
| 28 | # majority voye |
| 29 | most_common = Counter(k_nearest_labels).most_common() |
| 30 | return most_common[0][0] |
no test coverage detected