def fit(self, fit_X): fit_features = validate(fit_X) n_samples, n_features = fit_features.shape cluster_centers = fit_features[np.random.randint(n_samples, size=self.n_clusters)] clusters = np.zeros(shape=(n_samples, n_features + 1)) # come up with better names for the counter variables for i in range(self.n_iters): temp = np.copy(cluster_centers) distances_from_centroid = compute_distances( cluster_centers, fit_features) for j, points in enumerate(distances_from_centroid): clusters[j] = np.append(fit_features[j], np.argmin(points)) for k in range(self.n_clusters): x = np.argwhere(clusters[:, -1] == k) points_in_cluster = np.reshape( clusters[x], newshape=(clusters[x].shape[0], clusters[x].shape[-1])) for l in range(n_features): cluster_centers[k, l] = np.mean(points_in_cluster[:, l]) if np.all(cluster_centers == temp): break self.labels = clusters[:, -1] self.cluster_centers = cluster_centers
def predict(self, x): x = validate(x) predictions = np.zeros((x.shape[0])) array_of_distances = compute_distances(self.X, x) for i, distances in enumerate(array_of_distances): values = np.take(self.y, np.argpartition(distances, self.n_neighbors)[:self.n_neighbors]) predictions[i] = self._getpredictions(values) return predictions
def fit(self, fit_x, fit_y): x = validate(fit_x) y = validate(fit_y) n_samples, n_features = x.shape self.slope = np.zeros((n_features, 1)) self.intercept = 0 for i in range(self.n_iters): y_predicted = self._approximation(x, self.slope, self.intercept) d_slope = (1/n_samples) * -2 * (np.dot(x.T, (y - y_predicted))) d_intercept = (1/n_samples) * np.sum(-2 * (y - y_predicted)) self.slope -= d_slope*self.learning_rate self.intercept -= d_intercept*self.learning_rate
def predict(self, predict_x): prediction_features = validate(predict_x) n_samples, n_features = prediction_features.shape clusters = np.zeros(shape=(n_samples, n_features + 1)) distances_from_centroid = compute_distances(self.cluster_centers, prediction_features) for j, points in enumerate(distances_from_centroid): clusters[j] = np.append(prediction_features[j], np.argmin(points)) return clusters[:, -1]
def fit(self, fit_x, fit_y): self.X = validate(fit_x) self.y = validate(fit_y)
def predict(self, x): x = validate(x) return self._predict(x, self.slope, self.intercept)