def predict_probability(self, X): """Predict the probability of a target given features.""" if self.parameters is None or self.labels is None: raise NotFitted("the model must be fitted before usage") X = features_reshape(X) if self.standardize is not None: X = self.standardize(X) X = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1) return self.hypothesis(X, self.parameters)
def score_samples(self, X): """Compute the log-likelihood of all samples.""" if self._U_reduced is None or self._mean is None: raise NotFitted("the model must be fitted before usage") X = features_reshape(X) n_features = X.shape[1] precision = np.linalg.inv(self._covariance_matrix) residuals = X - self._mean return -(1 / 2) * (-np.log(np.linalg.det(precision)) + np.sum( (residuals * np.dot(residuals, precision)), axis=1) + n_features * np.log(2 * np.pi))
def predict(self, X): """Predict a target given features.""" if self.X_fit is None or self.y_fit is None: raise NotFitted("the model must be fitted before usage") labels = [] for x in X: distances_labels = [ (self.euclidian(x, x_fit), y_fit) for x_fit, y_fit in zip(self.X_fit, self.y_fit) ] neighbors = sorted(distances_labels, key=lambda d: d[0])[: self.n_neighbors] neighbors_labels = [neighbor[1][0] for neighbor in neighbors] labels.append( sorted( neighbors_labels, key=Counter(neighbors_labels).get, reverse=True )[0] ) return np.array(labels).reshape(-1, 1)
def score(self, X, y): """Score of the model.""" if self.parameters is None or self.labels is None: raise NotFitted("the model must be fitted before usage") return accuracy_score(self.predict(X), y)
def transform(self, X): """Transform the input.""" if self._U_reduced is None or self._mean is None: raise NotFitted("the model must be fitted before usage") return (X - self._mean).dot(self._U_reduced)