def predict(self, X):
X = self._pre_processing_x(X)
Y = np.zeros((X.shape[0], self.n_class))
A = self.A
# because X is (N x p), A is (K x p), we can to get the X_star (NxK)
X_star = X @ A.T
for k in range(self.n_class):
# mu_s_star shape is (p,)
mu_k_star = A @ self.Mu[k]
# Ref: http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html
# Ref: http://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy
Y[:, k] = LA.norm(X_star - mu_k_star, axis=1) * 0.5 - log(self.Pi[k])
# Python index start from 0, transform to start with 1
y_hat = Y.argmin(axis=1).reshape((-1, 1)) + 1
return y_hat
评论列表
文章目录